Browse Source

feat: worked more on Spotify song converting

Kristian Vos 2 years ago
parent
commit
bd8b2de26c
2 changed files with 214 additions and 10 deletions
  1. 174 4
      backend/logic/spotify.js
  2. 40 6
      backend/logic/wikidata.js

+ 174 - 4
backend/logic/spotify.js

@@ -715,8 +715,6 @@ class _SpotifyModule extends CoreClass {
 				}
 
 				if (relation["target-type"] === "work") {
-					console.log(relation, "GET WORK HERE");
-
 					const promise = new Promise(resolve => {
 						WikiDataModule.runJob("API_GET_DATA_FROM_MUSICBRAINZ_WORK", { workId: relation.work.id }, this)
 							.then(resultBody => {
@@ -734,6 +732,13 @@ class _SpotifyModule extends CoreClass {
 								// 			.map(binding => binding["SoundCloud_track_ID"].value)
 								// 	)
 								// );
+								const musicVideoEntityUrls = Array.from(
+									new Set(
+										resultBody.results.bindings
+											.filter(binding => !!binding.Music_video_entity_URL)
+											.map(binding => binding.Music_video_entity_URL.value)
+									)
+								);
 
 								youtubeIds.forEach(youtubeId => {
 									const mediaSource = `youtube:${youtubeId}`;
@@ -783,7 +788,86 @@ class _SpotifyModule extends CoreClass {
 								// 	}
 								// });
 
-								resolve();
+								const promisesToRun2 = [];
+
+								musicVideoEntityUrls.forEach(musicVideoEntityUrl => {
+									promisesToRun2.push(
+										new Promise(resolve => {
+											WikiDataModule.runJob(
+												"API_GET_DATA_FROM_ENTITY_URL",
+												{ entityUrl: musicVideoEntityUrl },
+												this
+											).then(resultBody => {
+												const youtubeIds = Array.from(
+													new Set(
+														resultBody.results.bindings
+															.filter(binding => !!binding.YouTube_video_ID)
+															.map(binding => binding.YouTube_video_ID.value)
+													)
+												);
+												// const soundcloudIds = Array.from(
+												// 	new Set(
+												// 		resultBody.results.bindings
+												// 			.filter(binding => !!binding["SoundCloud_track_ID"])
+												// 			.map(binding => binding["SoundCloud_track_ID"].value)
+												// 	)
+												// );
+
+												youtubeIds.forEach(youtubeId => {
+													const mediaSource = `youtube:${youtubeId}`;
+
+													mediaSources.add(mediaSource);
+
+													// if (collectAlternativeMediaSourcesOrigins) {
+													// 	const mediaSourceOrigins = [
+													// 		`Spotify track ${spotifyTrackId}`,
+													// 		`ISRC ${ISRC}`,
+													// 		`MusicBrainz recordings`,
+													// 		`MusicBrainz recording ${recording.id}`,
+													// 		`MusicBrainz relations`,
+													// 		`MusicBrainz relation target-type work`,
+													// 		`MusicBrainz relation work id ${relation.work.id}`,
+													// 		`WikiData select from MusicBrainz work id ${relation.work.id}`,
+													// 		`YouTube ID ${youtubeId}`
+													// 	];
+
+													// 	if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
+
+													// 	mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
+													// }
+												});
+
+												// soundcloudIds.forEach(soundcloudId => {
+												// 	const mediaSource = `soundcloud:${soundcloudId}`;
+
+												// 	mediaSources.add(mediaSource);
+
+												// 	// if (collectAlternativeMediaSourcesOrigins) {
+												// 	// 	const mediaSourceOrigins = [
+												// 	// 		`Spotify track ${spotifyTrackId}`,
+												// 	// 		`ISRC ${ISRC}`,
+												// 	// 		`MusicBrainz recordings`,
+												// 	// 		`MusicBrainz recording ${recording.id}`,
+												// 	// 		`MusicBrainz relations`,
+												// 	// 		`MusicBrainz relation target-type work`,
+												// 	// 		`MusicBrainz relation work id ${relation.work.id}`,
+												// 	// 		`WikiData select from MusicBrainz work id ${relation.work.id}`,
+												// 	// 		`SoundCloud ID ${soundcloudId}`
+												// 	// 	];
+
+												// 	// 	if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
+
+												// 	// 	mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
+												// 	// }
+												// });
+
+												resolve();
+											});
+										})
+									);
+								});
+
+								Promise.allSettled(promisesToRun2).then(resolve);
 							})
 							.catch(err => {
 								console.log("KRISWORKERR", err);
@@ -860,6 +944,13 @@ class _SpotifyModule extends CoreClass {
 						// 			.map(binding => binding["SoundCloud_track_ID"].value)
 						// 	)
 						// );
+						const musicVideoEntityUrls = Array.from(
+							new Set(
+								resultBody.results.bindings
+									.filter(binding => !!binding.Music_video_entity_URL)
+									.map(binding => binding.Music_video_entity_URL.value)
+							)
+						);
 
 						youtubeIds.forEach(youtubeId => {
 							const mediaSource = `youtube:${youtubeId}`;
@@ -909,7 +1000,86 @@ class _SpotifyModule extends CoreClass {
 						// 	// }
 						// });
 
-						resolve();
+						const promisesToRun2 = [];
+
+						musicVideoEntityUrls.forEach(musicVideoEntityUrl => {
+							promisesToRun2.push(
+								new Promise(resolve => {
+									WikiDataModule.runJob(
+										"API_GET_DATA_FROM_ENTITY_URL",
+										{ entityUrl: musicVideoEntityUrl },
+										this
+									).then(resultBody => {
+										const youtubeIds = Array.from(
+											new Set(
+												resultBody.results.bindings
+													.filter(binding => !!binding.YouTube_video_ID)
+													.map(binding => binding.YouTube_video_ID.value)
+											)
+										);
+										// const soundcloudIds = Array.from(
+										// 	new Set(
+										// 		resultBody.results.bindings
+										// 			.filter(binding => !!binding["SoundCloud_track_ID"])
+										// 			.map(binding => binding["SoundCloud_track_ID"].value)
+										// 	)
+										// );
+
+										youtubeIds.forEach(youtubeId => {
+											const mediaSource = `youtube:${youtubeId}`;
+
+											mediaSources.add(mediaSource);
+
+											// if (collectAlternativeMediaSourcesOrigins) {
+											// 	const mediaSourceOrigins = [
+											// 		`Spotify track ${spotifyTrackId}`,
+											// 		`ISRC ${ISRC}`,
+											// 		`MusicBrainz recordings`,
+											// 		`MusicBrainz recording ${recording.id}`,
+											// 		`MusicBrainz relations`,
+											// 		`MusicBrainz relation target-type work`,
+											// 		`MusicBrainz relation work id ${relation.work.id}`,
+											// 		`WikiData select from MusicBrainz work id ${relation.work.id}`,
+											// 		`YouTube ID ${youtubeId}`
+											// 	];
+
+											// 	if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
+
+											// 	mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
+											// }
+										});
+
+										// soundcloudIds.forEach(soundcloudId => {
+										// 	const mediaSource = `soundcloud:${soundcloudId}`;
+
+										// 	mediaSources.add(mediaSource);
+
+										// 	// if (collectAlternativeMediaSourcesOrigins) {
+										// 	// 	const mediaSourceOrigins = [
+										// 	// 		`Spotify track ${spotifyTrackId}`,
+										// 	// 		`ISRC ${ISRC}`,
+										// 	// 		`MusicBrainz recordings`,
+										// 	// 		`MusicBrainz recording ${recording.id}`,
+										// 	// 		`MusicBrainz relations`,
+										// 	// 		`MusicBrainz relation target-type work`,
+										// 	// 		`MusicBrainz relation work id ${relation.work.id}`,
+										// 	// 		`WikiData select from MusicBrainz work id ${relation.work.id}`,
+										// 	// 		`SoundCloud ID ${soundcloudId}`
+										// 	// 	];
+
+										// 	// 	if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
+
+										// 	// 	mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
+										// 	// }
+										// });
+
+										resolve();
+									});
+								})
+							);
+						});
+
+						Promise.allSettled(promisesToRun2).then(resolve);
 					})
 					.catch(err => {
 						console.log("KRISWORKERR", err);

+ 40 - 6
backend/logic/wikidata.js

@@ -148,6 +148,36 @@ class _WikiDataModule extends CoreClass {
 		// });
 	}
 
+	/**
+	 * Get WikiData data from entity url
+	 *
+	 * @param {object} payload - object that contains the payload
+	 * @param {object} payload.entityUrl - entity url
+	 * @returns {Promise} - returns promise (reject, resolve)
+	 */
+	async API_GET_DATA_FROM_ENTITY_URL(payload) {
+		const { entityUrl } = payload;
+
+		const sparqlQuery = `PREFIX entity_url: <${entityUrl}>
+							SELECT DISTINCT ?YouTube_video_ID ?SoundCloud_track_ID WHERE {
+								OPTIONAL { entity_url: wdt:P1651 ?YouTube_video_ID. }
+								OPTIONAL { entity_url: wdt:P3040 ?SoundCloud_track_ID. }
+							}`
+			.replaceAll("\n", "")
+			.replaceAll("\t", "");
+
+		return WikiDataModule.runJob(
+			"API_CALL",
+			{
+				url: "https://query.wikidata.org/sparql",
+				params: {
+					query: sparqlQuery
+				}
+			},
+			this
+		);
+	}
+
 	/**
 	 * Get WikiData data from work id
 	 *
@@ -158,7 +188,8 @@ class _WikiDataModule extends CoreClass {
 	async API_GET_DATA_FROM_MUSICBRAINZ_WORK(payload) {
 		const { workId } = payload;
 
-		const sparqlQuery = `SELECT DISTINCT ?item ?itemLabel ?YouTube_video_ID ?SoundCloud_track_ID WHERE {
+		const sparqlQuery =
+			`SELECT DISTINCT ?item ?itemLabel ?YouTube_video_ID ?SoundCloud_track_ID ?Music_video_entity_URL WHERE {
 								SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
 								{
 									SELECT DISTINCT ?item WHERE {
@@ -169,9 +200,10 @@ class _WikiDataModule extends CoreClass {
 								}
 								OPTIONAL { ?item wdt:P1651 ?YouTube_video_ID. }
 								OPTIONAL { ?item wdt:P3040 ?SoundCloud_track_ID. }
+								OPTIONAL { ?item wdt:P6718 ?Music_video_entity_URL. }
 							}`
-			.replaceAll("\n", "")
-			.replaceAll("\t", "");
+				.replaceAll("\n", "")
+				.replaceAll("\t", "");
 
 		return WikiDataModule.runJob(
 			"API_CALL",
@@ -195,7 +227,8 @@ class _WikiDataModule extends CoreClass {
 	async API_GET_DATA_FROM_MUSICBRAINZ_RELEASE_GROUP(payload) {
 		const { releaseGroupId } = payload;
 
-		const sparqlQuery = `SELECT DISTINCT ?item ?itemLabel ?YouTube_video_ID ?SoundCloud_track_ID WHERE {
+		const sparqlQuery =
+			`SELECT DISTINCT ?item ?itemLabel ?YouTube_video_ID ?SoundCloud_track_ID ?Music_video_entity_URL WHERE {
 								SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
 								{
 									SELECT DISTINCT ?item WHERE {
@@ -206,9 +239,10 @@ class _WikiDataModule extends CoreClass {
 								}
 								OPTIONAL { ?item wdt:P1651 ?YouTube_video_ID. }
 								OPTIONAL { ?item wdt:P3040 ?SoundCloud_track_ID. }
+								OPTIONAL { ?item wdt:P6718 ?Music_video_entity_URL. }
 							}`
-			.replaceAll("\n", "")
-			.replaceAll("\t", "");
+				.replaceAll("\n", "")
+				.replaceAll("\t", "");
 
 		return WikiDataModule.runJob(
 			"API_CALL",