浏览代码

refactor: fixed some YouTube add/import issues and added max page option for playlists to prevent infinite loop when requesting sets

Kristian Vos 2 年之前
父节点
当前提交
e32ef11502

+ 4 - 2
backend/logic/actions/playlists.js

@@ -1740,7 +1740,7 @@ export default {
 							.catch(next);
 					else next("Invalid YouTube URL.");
 				},
-				(mediaSources, next) => {
+				(youtubeIds, next) => {
 					this.publishProgress({ status: "update", message: `Importing YouTube playlist (stage 2)` });
 					let successful = 0;
 					let failed = 0;
@@ -1748,7 +1748,9 @@ export default {
 					let alreadyInLikedPlaylist = 0;
 					let alreadyInDislikedPlaylist = 0;
 
-					if (mediaSources.length === 0) next();
+					if (youtubeIds.length === 0) next();
+
+					const mediaSources = youtubeIds.map(youtubeId => `youtube:${youtubeId}`);
 
 					async.eachLimit(
 						mediaSources,

+ 1 - 1
backend/logic/spotify.js

@@ -15,7 +15,7 @@ let MediaModule;
 let MusicBrainzModule;
 
 const youtubeVideoUrlRegex =
-	/^(https?:\/\/)?(www\.)?(music\.)?(youtube\.com|youtu\.be)\/(watch\?v=)?(?<youtubeId>[\w-]{11})((&([A-Za-z0-9]+)?)*)?$/;
+	/^(https?:\/\/)?(www\.)?(m\.)?(music\.)?(youtube\.com|youtu\.be)\/(watch\?v=)?(?<youtubeId>[\w-]{11})((&([A-Za-z0-9]+)?)*)?$/;
 const youtubeVideoIdRegex = /^([\w-]{11})$/;
 
 const spotifyTrackObjectToMusareTrackObject = spotifyTrackObject => {

+ 7 - 1
backend/logic/youtube.js

@@ -620,6 +620,11 @@ class _YouTubeModule extends CoreClass {
 				return;
 			}
 			const playlistId = splitQuery[1];
+			const maxPages = config.has("apis.youtube.maxPlaylistPages")
+				? Number.parseInt(config.get("apis.youtube.maxPlaylistPages"))
+				: 20;
+
+			let currentPage = 0;
 
 			async.waterfall(
 				[
@@ -635,9 +640,10 @@ class _YouTubeModule extends CoreClass {
 										songs.length
 									} songs gotten so far. Is there a next page: ${nextPageToken !== undefined}.`
 								);
-								next(null, nextPageToken !== undefined);
+								next(null, nextPageToken !== undefined && currentPage < maxPages);
 							},
 							next => {
+								currentPage += 1;
 								// Add 250ms delay between each job request
 								setTimeout(() => {
 									YouTubeModule.runJob("GET_PLAYLIST_PAGE", { playlistId, nextPageToken }, this)

+ 5 - 5
frontend/src/composables/useYoutubeDirect.ts

@@ -4,7 +4,7 @@ import { AddSongToPlaylistResponse } from "@musare_types/actions/PlaylistsAction
 import { useWebsocketsStore } from "@/stores/websockets";
 
 const youtubeVideoUrlRegex =
-	/^(https?:\/\/)?(www\.)?(youtube\.com|youtu\.be)\/(watch\?v=)?([\w-]{11})$/;
+	/^(https?:\/\/)?(www\.)?(m\.)?(music\.)?(youtube\.com|youtu\.be)\/(watch\?v=)?(?<youtubeId>[\w-]{11})((&([A-Za-z0-9]+)?)*)?$/;
 const youtubeVideoIdRegex = /^([\w-]{11})$/;
 
 export const useYoutubeDirect = () => {
@@ -13,12 +13,12 @@ export const useYoutubeDirect = () => {
 	const { socket } = useWebsocketsStore();
 
 	const getYoutubeVideoId = () => {
-		const youtubeVideoUrlParts = youtubeVideoUrlRegex.exec(
+		const youtubeVideoUrlMatch = youtubeVideoUrlRegex.exec(
 			youtubeDirect.value.trim()
 		);
-		if (youtubeVideoUrlParts) {
+		if (youtubeVideoUrlMatch && youtubeVideoUrlMatch.groups.youtubeId) {
 			// eslint-disable-next-line prefer-destructuring
-			return youtubeVideoUrlParts[5];
+			return youtubeVideoUrlMatch.groups.youtubeId;
 		}
 
 		const youtubeVideoIdParts = youtubeVideoIdRegex.exec(
@@ -43,7 +43,7 @@ export const useYoutubeDirect = () => {
 			socket.dispatch(
 				"playlists.addSongToPlaylist",
 				false,
-				youtubeVideoId,
+				`youtube:${youtubeVideoId}`,
 				playlistId,
 				(res: AddSongToPlaylistResponse) => {
 					if (res.status !== "success")