Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- spotifyApi(endpoint) {
- return new Promise(resolve => {
- require("https").request({
- hostname: "api.spotify.com",
- path: endpoint,
- headers: {
- "Authorization": `Bearer ${this.client.config.spotifyAccessToken}`
- }
- }, response => {
- let data = "";
- response.on("data", chunk => data += chunk);
- response.on("end", () => resolve(JSON.parse(data)));
- }).end();
- });
- }
- spotifyURI(tag) {
- return new Promise(res => {
- tag = tag.replace(/spotify:|user:/g, "");
- tag = tag.split(":").slice(-2);
- const type = tag[0];
- const id = tag[1];
- this.spotifyApi(`/v1/${type}s/${id}`).then(async response => {
- while (response.tracks.next) {
- const res2 = await this.spotifyApi(response.tracks.next.slice(23));
- response.tracks.items = response.tracks.items.concat(res2.items);
- response.tracks.next = res2.next;
- }
- let Tracks = [];
- if (type === "track") {
- Tracks.push(response);
- Tracks.metadata = {
- type: "track"
- };
- } else {
- // This else statement is actually only considering 'playlist' and 'album' types
- if (type !== "album") response.tracks.items = response.tracks.items.map(i => i.track);
- Tracks = Tracks.concat(response.tracks.items);
- delete response.tracks.items;
- }
- Promise.all(Tracks.map(async track => {
- const results = await this.youtubeAPI.search.list({
- part: "id, snippet",
- q: `${type === "album" ? Tracks.metadata.name : track.artists[0].name} ${track.name}`,
- type: "video",
- safeSearch: "none"
- });
- return track = {
- track: track,
- video: results.data.items[0]
- };
- })).then(async Tracks => {
- let ytDetails = [];
- while (ytDetails.length < Tracks.length) {
- const response = await this.youtubeAPI.videos.list({
- part: "contentDetails",
- id: Tracks.map(i => i.video.id.videoId).slice(ytDetails.length, ytDetails.length + 50).join(",")
- });
- ytDetails = ytDetails.concat(response.data.items);
- }
- ytDetails.forEach(details => Tracks.find(i => i.video.id.videoId === details.id).video.contentDetails = details.contentDetails);
- Tracks.metadata = response;
- res(Tracks);
- });
- });
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement