Advertisement
Guest User

Untitled

a guest
Sep 25th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   spotifyApi(endpoint) {
  2.     return new Promise(resolve => {
  3.       require("https").request({
  4.         hostname: "api.spotify.com",
  5.         path: endpoint,
  6.         headers: {
  7.           "Authorization": `Bearer ${this.client.config.spotifyAccessToken}`
  8.         }
  9.       }, response => {
  10.         let data = "";
  11.         response.on("data", chunk => data += chunk);
  12.         response.on("end", () => resolve(JSON.parse(data)));
  13.       }).end();
  14.     });
  15.   }
  16.   spotifyURI(tag) {
  17.     return new Promise(res => {
  18.       tag = tag.replace(/spotify:|user:/g, "");
  19.       tag = tag.split(":").slice(-2);
  20.       const type = tag[0];
  21.       const id = tag[1];
  22.       this.spotifyApi(`/v1/${type}s/${id}`).then(async response => {
  23.         while (response.tracks.next) {
  24.           const res2 = await this.spotifyApi(response.tracks.next.slice(23));
  25.           response.tracks.items = response.tracks.items.concat(res2.items);
  26.           response.tracks.next = res2.next;
  27.  
  28.         }
  29.         let Tracks = [];
  30.         if (type === "track") {
  31.           Tracks.push(response);
  32.           Tracks.metadata = {
  33.             type: "track"
  34.           };
  35.         } else {
  36.           // This else statement is actually only considering 'playlist' and 'album' types
  37.           if (type !== "album") response.tracks.items = response.tracks.items.map(i => i.track);
  38.           Tracks = Tracks.concat(response.tracks.items);
  39.           delete response.tracks.items;
  40.         }
  41.         Promise.all(Tracks.map(async track => {
  42.           const results = await this.youtubeAPI.search.list({
  43.             part: "id, snippet",
  44.             q: `${type === "album" ? Tracks.metadata.name : track.artists[0].name} ${track.name}`,
  45.             type: "video",
  46.             safeSearch: "none"
  47.           });
  48.           return track = {
  49.             track: track,
  50.             video: results.data.items[0]
  51.           };
  52.         })).then(async Tracks => {
  53.           let ytDetails = [];
  54.           while (ytDetails.length < Tracks.length) {
  55.             const response = await this.youtubeAPI.videos.list({
  56.               part: "contentDetails",
  57.               id: Tracks.map(i => i.video.id.videoId).slice(ytDetails.length, ytDetails.length + 50).join(",")
  58.             });
  59.             ytDetails = ytDetails.concat(response.data.items);
  60.           }
  61.           ytDetails.forEach(details => Tracks.find(i => i.video.id.videoId === details.id).video.contentDetails = details.contentDetails);
  62.           Tracks.metadata = response;
  63.           res(Tracks);
  64.         });
  65.       });
  66.     });
  67.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement