Просмотр исходного кода

fix(YouTube API): topicDetails Ids not being returned for some API keys

Signed-off-by: Jonathan <theflametrooper@gmail.com>
Jonathan 4 лет назад
Родитель
Сommit
7c9fb22d7a
2 измененных файлов с 16 добавлено и 43 удалено
  1. 8 13
      backend/logic/actions/playlists.js
  2. 8 30
      backend/logic/youtube.js

+ 8 - 13
backend/logic/actions/playlists.js

@@ -973,21 +973,14 @@ export default {
 		async.waterfall(
 			[
 				next => {
-					YouTubeModule.runJob(
-						"GET_PLAYLIST",
-						{
-							url,
-							musicOnly
-						},
-						this
-					).then(response => {
-						if (response.filteredSongs) {
-							videosInPlaylistTotal = response.songs.length;
-							songsInPlaylistTotal = response.filteredSongs.length;
+					YouTubeModule.runJob("GET_PLAYLIST", { url, musicOnly }, this).then(res => {
+						if (res.filteredSongs) {
+							videosInPlaylistTotal = res.songs.length;
+							songsInPlaylistTotal = res.filteredSongs.length;
 						} else {
-							songsInPlaylistTotal = videosInPlaylistTotal = response.songs.length;
+							songsInPlaylistTotal = videosInPlaylistTotal = res.songs.length;
 						}
-						next(null, response.songs);
+						next(null, res.songs);
 					});
 				},
 				(songIds, next) => {
@@ -997,6 +990,8 @@ export default {
 
 					if (songIds.length === 0) next();
 
+					console.log(songIds);
+
 					async.eachLimit(
 						songIds,
 						1,

+ 8 - 30
backend/logic/youtube.js

@@ -89,8 +89,6 @@ class _YouTubeModule extends CoreClass {
 				.get("https://www.googleapis.com/youtube/v3/videos", {
 					params,
 					timeout: 30000
-					// agent: false,
-					// pool: { maxSockets: 100 }
 				})
 				.then(res => {
 					if (res.data.error) {
@@ -152,7 +150,6 @@ class _YouTubeModule extends CoreClass {
 	 * @returns {Promise} - returns promise (reject, resolve)
 	 */
 	GET_PLAYLIST(payload) {
-		// payload includes: url, musicOnly
 		return new Promise((resolve, reject) => {
 			const name = "list".replace(/[\\[]/, "\\[").replace(/[\]]/, "\\]");
 
@@ -185,43 +182,28 @@ class _YouTubeModule extends CoreClass {
 								// Add 250ms delay between each job request
 								setTimeout(() => {
 									YouTubeModule.runJob("GET_PLAYLIST_PAGE", { playlistId, nextPageToken }, this)
-										// eslint-disable-next-line no-loop-func
 										.then(response => {
 											songs = songs.concat(response.songs);
 											nextPageToken = response.nextPageToken;
 											next();
 										})
-										// eslint-disable-next-line no-loop-func
-										.catch(err => {
-											next(err);
-										});
+										.catch(err => next(err));
 								}, 250);
 							},
-							err => {
-								next(err, songs);
-							}
+							err => next(err, songs)
 						);
 					},
 
-					(songs, next) => {
+					(songs, next) =>
 						next(
 							null,
 							songs.map(song => song.contentDetails.videoId)
-						);
-					},
+						),
 
 					(songs, next) => {
 						if (!payload.musicOnly) return next(true, { songs });
-						return YouTubeModule.runJob(
-							"FILTER_MUSIC_VIDEOS",
-							{
-								videoIds: songs.slice()
-							},
-							this
-						)
-							.then(filteredSongs => {
-								next(null, { filteredSongs, songs });
-							})
+						return YouTubeModule.runJob("FILTER_MUSIC_VIDEOS", { videoIds: songs.slice() }, this)
+							.then(filteredSongs => next(null, { filteredSongs, songs }))
 							.catch(next);
 					}
 				],
@@ -261,8 +243,6 @@ class _YouTubeModule extends CoreClass {
 				.get("https://www.googleapis.com/youtube/v3/playlistItems", {
 					params,
 					timeout: 30000
-					// agent: false,
-					// pool: { maxSockets: 100 }
 				})
 				.then(res => {
 					if (res.data.err) {
@@ -314,8 +294,6 @@ class _YouTubeModule extends CoreClass {
 				.get("https://www.googleapis.com/youtube/v3/videos", {
 					params,
 					timeout: 30000
-					// agent: false,
-					// pool: { maxSockets: 100 }
 				})
 				.then(res => {
 					if (res.data.err) {
@@ -327,10 +305,10 @@ class _YouTubeModule extends CoreClass {
 
 					res.data.items.forEach(item => {
 						const songId = item.id;
+
 						if (!item.topicDetails) return;
-						if (item.topicDetails.relevantTopicIds.indexOf("/m/04rlf") !== -1) {
+						if (item.topicDetails.topicCategories.indexOf("https://en.wikipedia.org/wiki/Music") !== -1)
 							songIds.push(songId);
-						}
 					});
 
 					return YouTubeModule.runJob(