Skip to content

Commit

Permalink
spotify updates
Browse files Browse the repository at this point in the history
  • Loading branch information
michelle0927 committed Sep 22, 2020
1 parent ece136f commit 204c700
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 51 deletions.
68 changes: 55 additions & 13 deletions components/spotify/new-playlists.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,51 @@
const spotify = require("https://github.com/PipedreamHQ/pipedream/components/spotify/spotify.app.js");
const axios = require("axios");

const spotify = {
type: "app",
app: "spotify",
methods: {
async _getBaseUrl() {
return "https://api.spotify.com/v1"
},
async _getHeaders() {
return {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
},
async getPlaylistItems(playlist_id, params) {
return await axios.get(
`${await this._getBaseUrl()}/playlists/${playlist_id}/tracks`,
{
headers: await this._getHeaders(),
params,
}
);
},

async getPlaylists(params) {
return await axios.get(
`${await this._getBaseUrl()}/me/playlists`,
{
headers: await this._getHeaders(),
params,
}
);
},

async getTracks(params) {
return await axios.get(
`${await this._getBaseUrl()}/me/tracks`,
{
headers: await this._getHeaders(),
params,
}
);
},
},
};


//const spotify = require("https://github.com/PipedreamHQ/pipedream/components/spotify/spotify.app.js");

module.exports = {
name: "New Playlists",
Expand All @@ -17,7 +64,6 @@ module.exports = {
},

async run(event) {
let playlists = [];
let results;
let total = 1;
let count = 0;
Expand All @@ -32,19 +78,15 @@ module.exports = {
while (count < total) {
results = await this.spotify.getPlaylists(params);
total = results.data.total;
results.data.items.forEach(function (playlist) {
playlists.push(playlist);
for (const playlist of results.data.items) {
this.$emit(playlist, {
id: playlist.id,
summary: playlist.name,
ts: Date.now(),
});
count++;
});
}
params.offset += limit;
}

for (const playlist of playlists) {
this.$emit(playlist, {
id: playlist.id,
summary: playlist.name,
ts: Date.now(),
});
}
},
};
67 changes: 54 additions & 13 deletions components/spotify/new-saved-tracks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
const spotify = require("https://github.com/PipedreamHQ/pipedream/components/spotify/spotify.app.js");
const axios = require("axios");

const spotify = {
type: "app",
app: "spotify",
methods: {
async _getBaseUrl() {
return "https://api.spotify.com/v1"
},
async _getHeaders() {
return {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
},
async getPlaylistItems(playlist_id, params) {
return await axios.get(
`${await this._getBaseUrl()}/playlists/${playlist_id}/tracks`,
{
headers: await this._getHeaders(),
params,
}
);
},

async getPlaylists(params) {
return await axios.get(
`${await this._getBaseUrl()}/me/playlists`,
{
headers: await this._getHeaders(),
params,
}
);
},

async getTracks(params) {
return await axios.get(
`${await this._getBaseUrl()}/me/tracks`,
{
headers: await this._getHeaders(),
params,
}
);
},
},
};

//const spotify = require("https://github.com/PipedreamHQ/pipedream/components/spotify/spotify.app.js");

module.exports = {
name: "New Saved Tracks",
Expand All @@ -18,7 +64,6 @@ module.exports = {
},

async run(event) {
let tracks = [];
let results;
let addedAt;
let total = 1;
Expand All @@ -40,24 +85,20 @@ module.exports = {
while (count < total) {
results = await this.spotify.getTracks(params);
total = results.data.total;
results.data.items.forEach(function (track) {
for (const track of results.data.items) {
addedAt = new Date(track.added_at);
if (addedAt.getTime() > lastEvent.getTime()) {
tracks.push(track);
this.$emit(track, {
id: track.track.id,
summary: track.track.name,
ts: track.added_at,
});
}
count++;
});
}
params.offset += limit;
}

this.db.set("lastEvent", now);

for (const track of tracks) {
this.$emit(track, {
id: track.track.id,
summary: track.track.name,
ts: track.added_at,
});
}
},
};
67 changes: 54 additions & 13 deletions components/spotify/new-tracks-in-playlist.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,50 @@
const spotify = require("https://github.com/PipedreamHQ/pipedream/components/spotify/spotify.app.js");
const axios = require("axios");

const spotify = {
type: "app",
app: "spotify",
methods: {
async _getBaseUrl() {
return "https://api.spotify.com/v1"
},
async _getHeaders() {
return {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
},
async getPlaylistItems(playlist_id, params) {
return await axios.get(
`${await this._getBaseUrl()}/playlists/${playlist_id}/tracks`,
{
headers: await this._getHeaders(),
params,
}
);
},

async getPlaylists(params) {
return await axios.get(
`${await this._getBaseUrl()}/me/playlists`,
{
headers: await this._getHeaders(),
params,
}
);
},

async getTracks(params) {
return await axios.get(
`${await this._getBaseUrl()}/me/tracks`,
{
headers: await this._getHeaders(),
params,
}
);
},
},
};

//const spotify = require("https://github.com/PipedreamHQ/pipedream/components/spotify/spotify.app.js");

module.exports = {
name: "New Tracks in Playlist",
Expand Down Expand Up @@ -35,7 +81,6 @@ module.exports = {
},

async run(event) {
let tracks = [];
let results;
let addedAt;
let total = 1;
Expand All @@ -60,13 +105,17 @@ module.exports = {
while (count < total && i < total) {
results = await this.spotify.getPlaylistItems(playlistId, params);
total = results.data.total;
results.data.items.forEach(function (track) {
for (const track of results.data.items) {
addedAt = new Date(track.added_at);
if (addedAt.getTime() > lastEvent.getTime()) {
tracks.push(track);
this.$emit(track, {
id: track.track.id,
summary: track.track.name,
ts: track.added_at,
});
}
count++;
});
}
i++;
params.offset += limit;
}
Expand All @@ -75,13 +124,5 @@ module.exports = {
}

this.db.set("lastEvent", now);

for (const track of tracks) {
this.$emit(track, {
id: track.track.id,
summary: track.track.name,
ts: track.added_at,
});
}
},
};
26 changes: 14 additions & 12 deletions components/spotify/spotify.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@ module.exports = {
type: "app",
app: "spotify",
methods: {
async _getBaseUrl() {
return "https://api.spotify.com/v1"
},
async _getHeaders() {
return {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
};
},
async getPlaylistItems(playlist_id, params) {
return await axios.get(
`https://api.spotify.com/v1/playlists/${playlist_id}/tracks`,
`${await this._getBaseUrl()}/playlists/${playlist_id}/tracks`,
{
headers: {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
headers: await this._getHeaders(),
params,
}
);
},

async getPlaylists(params) {
return await axios.get(
"https://api.spotify.com/v1/me/playlists",
`${await this._getBaseUrl()}/me/playlists`,
{
headers: {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
headers: await this._getHeaders(),
params,
}
);
},

async getTracks(params) {
return await axios.get(
"https://api.spotify.com/v1/me/tracks",
`${await this._getBaseUrl()}/me/tracks`,
{
headers: {
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
},
headers: await this._getHeaders(),
params,
}
);
Expand Down

0 comments on commit 204c700

Please sign in to comment.