spotify.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. import mongoose from "mongoose";
  2. import async from "async";
  3. import config from "config";
  4. import * as rax from "retry-axios";
  5. import axios from "axios";
  6. import url from "url";
  7. import CoreClass from "../core";
  8. let SpotifyModule;
  9. let SoundcloudModule;
  10. let DBModule;
  11. let CacheModule;
  12. let MediaModule;
  13. let MusicBrainzModule;
  14. let WikiDataModule;
  15. const youtubeVideoUrlRegex =
  16. /^(https?:\/\/)?(www\.)?(m\.)?(music\.)?(youtube\.com|youtu\.be)\/(watch\?v=)?(?<youtubeId>[\w-]{11})((&([A-Za-z0-9]+)?)*)?$/;
  17. const youtubeVideoIdRegex = /^([\w-]{11})$/;
  18. const spotifyTrackObjectToMusareTrackObject = spotifyTrackObject => {
  19. return {
  20. trackId: spotifyTrackObject.id,
  21. name: spotifyTrackObject.name,
  22. albumId: spotifyTrackObject.album.id,
  23. albumTitle: spotifyTrackObject.album.title,
  24. albumImageUrl: spotifyTrackObject.album.images[0].url,
  25. artists: spotifyTrackObject.artists.map(artist => artist.name),
  26. artistIds: spotifyTrackObject.artists.map(artist => artist.id),
  27. duration: spotifyTrackObject.duration_ms / 1000,
  28. explicit: spotifyTrackObject.explicit,
  29. externalIds: spotifyTrackObject.external_ids,
  30. popularity: spotifyTrackObject.popularity,
  31. isLocal: spotifyTrackObject.is_local
  32. };
  33. };
  34. class RateLimitter {
  35. /**
  36. * Constructor
  37. *
  38. * @param {number} timeBetween - The time between each allowed YouTube request
  39. */
  40. constructor(timeBetween) {
  41. this.dateStarted = Date.now();
  42. this.timeBetween = timeBetween;
  43. }
  44. /**
  45. * Returns a promise that resolves whenever the ratelimit of a YouTube request is done
  46. *
  47. * @returns {Promise} - promise that gets resolved when the rate limit allows it
  48. */
  49. continue() {
  50. return new Promise(resolve => {
  51. if (Date.now() - this.dateStarted >= this.timeBetween) resolve();
  52. else setTimeout(resolve, this.dateStarted + this.timeBetween - Date.now());
  53. });
  54. }
  55. /**
  56. * Restart the rate limit timer
  57. */
  58. restart() {
  59. this.dateStarted = Date.now();
  60. }
  61. }
  62. class _SpotifyModule extends CoreClass {
  63. // eslint-disable-next-line require-jsdoc
  64. constructor() {
  65. super("spotify");
  66. SpotifyModule = this;
  67. }
  68. /**
  69. * Initialises the spotify module
  70. *
  71. * @returns {Promise} - returns promise (reject, resolve)
  72. */
  73. async initialize() {
  74. DBModule = this.moduleManager.modules.db;
  75. CacheModule = this.moduleManager.modules.cache;
  76. MediaModule = this.moduleManager.modules.media;
  77. MusicBrainzModule = this.moduleManager.modules.musicbrainz;
  78. SoundcloudModule = this.moduleManager.modules.soundcloud;
  79. WikiDataModule = this.moduleManager.modules.wikidata;
  80. // this.youtubeApiRequestModel = this.YoutubeApiRequestModel = await DBModule.runJob("GET_MODEL", {
  81. // modelName: "youtubeApiRequest"
  82. // });
  83. this.spotifyTrackModel = this.SpotifyTrackModel = await DBModule.runJob("GET_MODEL", {
  84. modelName: "spotifyTrack"
  85. });
  86. return new Promise((resolve, reject) => {
  87. if (!config.has("apis.spotify") || !config.get("apis.spotify.enabled")) {
  88. reject(new Error("Spotify is not enabled."));
  89. return;
  90. }
  91. this.rateLimiter = new RateLimitter(config.get("apis.spotify.rateLimit"));
  92. this.requestTimeout = config.get("apis.spotify.requestTimeout");
  93. this.axios = axios.create();
  94. this.axios.defaults.raxConfig = {
  95. instance: this.axios,
  96. retry: config.get("apis.spotify.retryAmount"),
  97. noResponseRetries: config.get("apis.spotify.retryAmount")
  98. };
  99. rax.attach(this.axios);
  100. resolve();
  101. });
  102. }
  103. /**
  104. *
  105. * @returns
  106. */
  107. GET_API_TOKEN() {
  108. return new Promise((resolve, reject) => {
  109. CacheModule.runJob("GET", { key: "spotifyApiKey" }, this).then(spotifyApiKey => {
  110. if (spotifyApiKey) {
  111. resolve(spotifyApiKey);
  112. return;
  113. }
  114. this.log("INFO", `No Spotify API token stored in cache, requesting new token.`);
  115. const clientId = config.get("apis.spotify.clientId");
  116. const clientSecret = config.get("apis.spotify.clientSecret");
  117. const unencoded = `${clientId}:${clientSecret}`;
  118. const encoded = Buffer.from(unencoded).toString("base64");
  119. const params = new url.URLSearchParams({ grant_type: "client_credentials" });
  120. SpotifyModule.axios
  121. .post("https://accounts.spotify.com/api/token", params.toString(), {
  122. headers: {
  123. Authorization: `Basic ${encoded}`,
  124. "Content-Type": "application/x-www-form-urlencoded"
  125. }
  126. })
  127. .then(res => {
  128. const { access_token: accessToken, expires_in: expiresIn } = res.data;
  129. // TODO TTL can be later if stuck in queue
  130. CacheModule.runJob(
  131. "SET",
  132. { key: "spotifyApiKey", value: accessToken, ttl: expiresIn - 30 },
  133. this
  134. )
  135. .then(spotifyApiKey => {
  136. this.log(
  137. "SUCCESS",
  138. `Stored new Spotify API token in cache. Expires in ${expiresIn - 30}`
  139. );
  140. resolve(spotifyApiKey);
  141. })
  142. .catch(err => {
  143. this.log(
  144. "ERROR",
  145. `Failed to store new Spotify API token in cache.`,
  146. typeof err === "string" ? err : err.message
  147. );
  148. reject(err);
  149. });
  150. })
  151. .catch(err => {
  152. this.log(
  153. "ERROR",
  154. `Failed to get new Spotify API token.`,
  155. typeof err === "string" ? err : err.message
  156. );
  157. reject(err);
  158. });
  159. });
  160. });
  161. }
  162. /**
  163. * Perform Spotify API get track request
  164. *
  165. * @param {object} payload - object that contains the payload
  166. * @param {object} payload.params - request parameters
  167. * @returns {Promise} - returns promise (reject, resolve)
  168. */
  169. API_GET_TRACK(payload) {
  170. return new Promise((resolve, reject) => {
  171. const { trackId } = payload;
  172. SpotifyModule.runJob(
  173. "API_CALL",
  174. {
  175. url: `https://api.spotify.com/v1/tracks/${trackId}`
  176. },
  177. this
  178. )
  179. .then(response => {
  180. resolve(response);
  181. })
  182. .catch(err => {
  183. reject(err);
  184. });
  185. });
  186. }
  187. /**
  188. * Perform Spotify API get playlist request
  189. *
  190. * @param {object} payload - object that contains the payload
  191. * @param {object} payload.params - request parameters
  192. * @returns {Promise} - returns promise (reject, resolve)
  193. */
  194. API_GET_PLAYLIST(payload) {
  195. return new Promise((resolve, reject) => {
  196. const { playlistId, nextUrl } = payload;
  197. SpotifyModule.runJob(
  198. "API_CALL",
  199. {
  200. url: nextUrl || `https://api.spotify.com/v1/playlists/${playlistId}/tracks`
  201. },
  202. this
  203. )
  204. .then(response => {
  205. resolve(response);
  206. })
  207. .catch(err => {
  208. reject(err);
  209. });
  210. });
  211. }
  212. /**
  213. * Perform Spotify API call
  214. *
  215. * @param {object} payload - object that contains the payload
  216. * @param {object} payload.url - request url
  217. * @param {object} payload.params - request parameters
  218. * @param {object} payload.quotaCost - request quotaCost
  219. * @returns {Promise} - returns promise (reject, resolve)
  220. */
  221. API_CALL(payload) {
  222. return new Promise((resolve, reject) => {
  223. // const { url, params, quotaCost } = payload;
  224. const { url } = payload;
  225. SpotifyModule.runJob("GET_API_TOKEN", {}, this)
  226. .then(spotifyApiToken => {
  227. SpotifyModule.axios
  228. .get(url, {
  229. headers: {
  230. Authorization: `Bearer ${spotifyApiToken}`
  231. },
  232. timeout: SpotifyModule.requestTimeout
  233. })
  234. .then(response => {
  235. if (response.data.error) {
  236. reject(new Error(response.data.error));
  237. } else {
  238. resolve({ response });
  239. }
  240. })
  241. .catch(err => {
  242. reject(err);
  243. });
  244. })
  245. .catch(err => {
  246. this.log(
  247. "ERROR",
  248. `Spotify API call failed as an error occured whilst getting the API token`,
  249. typeof err === "string" ? err : err.message
  250. );
  251. resolve(err);
  252. });
  253. });
  254. }
  255. /**
  256. * Create Spotify track
  257. *
  258. * @param {object} payload - an object containing the payload
  259. * @param {string} payload.spotifyTracks - the spotifyTracks
  260. * @returns {Promise} - returns a promise (resolve, reject)
  261. */
  262. CREATE_TRACKS(payload) {
  263. return new Promise((resolve, reject) => {
  264. async.waterfall(
  265. [
  266. next => {
  267. const { spotifyTracks } = payload;
  268. if (!Array.isArray(spotifyTracks)) next("Invalid spotifyTracks type");
  269. else {
  270. const trackIds = spotifyTracks.map(spotifyTrack => spotifyTrack.trackId);
  271. SpotifyModule.spotifyTrackModel.find({ trackId: trackIds }, (err, existingTracks) => {
  272. if (err) return next(err);
  273. const existingTrackIds = existingTracks.map(existingTrack => existingTrack.trackId);
  274. const newSpotifyTracks = spotifyTracks.filter(
  275. spotifyTrack => existingTrackIds.indexOf(spotifyTrack.trackId) === -1
  276. );
  277. SpotifyModule.spotifyTrackModel.insertMany(newSpotifyTracks, next);
  278. });
  279. }
  280. },
  281. (spotifyTracks, next) => {
  282. const mediaSources = spotifyTracks.map(spotifyTrack => `spotify:${spotifyTrack.trackId}`);
  283. async.eachLimit(
  284. mediaSources,
  285. 2,
  286. (mediaSource, next) => {
  287. MediaModule.runJob("RECALCULATE_RATINGS", { mediaSource }, this)
  288. .then(() => next())
  289. .catch(next);
  290. },
  291. err => {
  292. if (err) next(err);
  293. else next(null, spotifyTracks);
  294. }
  295. );
  296. }
  297. ],
  298. (err, spotifyTracks) => {
  299. if (err) reject(new Error(err));
  300. else resolve({ spotifyTracks });
  301. }
  302. );
  303. });
  304. }
  305. /**
  306. * Gets tracks from media sources
  307. *
  308. * @param {object} payload
  309. * @returns {Promise}
  310. */
  311. async GET_TRACKS_FROM_MEDIA_SOURCES(payload) {
  312. return new Promise((resolve, reject) => {
  313. const { mediaSources } = payload;
  314. const responses = {};
  315. const promises = [];
  316. mediaSources.forEach(mediaSource => {
  317. promises.push(
  318. new Promise(resolve => {
  319. const trackId = mediaSource.split(":")[1];
  320. SpotifyModule.runJob("GET_TRACK", { identifier: trackId, createMissing: true }, this)
  321. .then(({ track }) => {
  322. responses[mediaSource] = track;
  323. })
  324. .catch(err => {
  325. SpotifyModule.log(
  326. "ERROR",
  327. `Getting tracked with media source ${mediaSource} failed.`,
  328. typeof err === "string" ? err : err.message
  329. );
  330. responses[mediaSource] = typeof err === "string" ? err : err.message;
  331. })
  332. .finally(() => {
  333. resolve();
  334. });
  335. })
  336. );
  337. });
  338. Promise.all(promises)
  339. .then(() => {
  340. SpotifyModule.log("SUCCESS", `Got all tracks.`);
  341. resolve({ tracks: responses });
  342. })
  343. .catch(reject);
  344. });
  345. }
  346. /**
  347. * Get Spotify track
  348. *
  349. * @param {object} payload - an object containing the payload
  350. * @param {string} payload.identifier - the spotify track ObjectId or track id
  351. * @param {string} payload.createMissing - attempt to fetch and create track if not in db
  352. * @returns {Promise} - returns a promise (resolve, reject)
  353. */
  354. GET_TRACK(payload) {
  355. return new Promise((resolve, reject) => {
  356. async.waterfall(
  357. [
  358. next => {
  359. const query = mongoose.isObjectIdOrHexString(payload.identifier)
  360. ? { _id: payload.identifier }
  361. : { trackId: payload.identifier };
  362. return SpotifyModule.spotifyTrackModel.findOne(query, next);
  363. },
  364. (track, next) => {
  365. if (track) return next(null, track, false);
  366. if (mongoose.isObjectIdOrHexString(payload.identifier) || !payload.createMissing)
  367. return next("Spotify track not found.");
  368. return SpotifyModule.runJob("API_GET_TRACK", { trackId: payload.identifier }, this)
  369. .then(({ response }) => {
  370. const { data } = response;
  371. if (!data || !data.id)
  372. return next("The specified track does not exist or cannot be publicly accessed.");
  373. const spotifyTrack = spotifyTrackObjectToMusareTrackObject(data);
  374. return next(null, false, spotifyTrack);
  375. })
  376. .catch(next);
  377. },
  378. (track, spotifyTrack, next) => {
  379. if (track) return next(null, track, true);
  380. return SpotifyModule.runJob("CREATE_TRACKS", { spotifyTracks: [spotifyTrack] }, this)
  381. .then(res => {
  382. if (res.spotifyTracks.length === 1) next(null, res.spotifyTracks[0], false);
  383. else next("Spotify track not found.");
  384. })
  385. .catch(next);
  386. }
  387. ],
  388. (err, track, existing) => {
  389. if (err) reject(new Error(err));
  390. else if (track.isLocal) reject(new Error("Track is local."));
  391. else resolve({ track, existing });
  392. }
  393. );
  394. });
  395. }
  396. /**
  397. * Returns an array of songs taken from a Spotify playlist
  398. *
  399. * @param {object} payload - object that contains the payload
  400. * @param {string} payload.url - the id of the Spotify playlist
  401. * @returns {Promise} - returns promise (reject, resolve)
  402. */
  403. GET_PLAYLIST(payload) {
  404. return new Promise((resolve, reject) => {
  405. const spotifyPlaylistUrlRegex = /.+open\.spotify\.com\/playlist\/(?<playlistId>[A-Za-z0-9]+)/;
  406. const match = spotifyPlaylistUrlRegex.exec(payload.url);
  407. if (!match || !match.groups) {
  408. SpotifyModule.log("ERROR", "GET_PLAYLIST", "Invalid Spotify playlist URL query.");
  409. reject(new Error("Invalid playlist URL."));
  410. return;
  411. }
  412. const { playlistId } = match.groups;
  413. async.waterfall(
  414. [
  415. next => {
  416. let spotifyTracks = [];
  417. let total = -1;
  418. let nextUrl = "";
  419. async.whilst(
  420. next => {
  421. SpotifyModule.log(
  422. "INFO",
  423. `Getting playlist progress for job (${this.toString()}): ${
  424. spotifyTracks.length
  425. } tracks gotten so far. Total tracks: ${total}.`
  426. );
  427. next(null, nextUrl !== null);
  428. },
  429. next => {
  430. // Add 250ms delay between each job request
  431. setTimeout(() => {
  432. SpotifyModule.runJob("API_GET_PLAYLIST", { playlistId, nextUrl }, this)
  433. .then(({ response }) => {
  434. const { data } = response;
  435. if (!data)
  436. return next("The provided URL does not exist or cannot be accessed.");
  437. total = data.total;
  438. nextUrl = data.next;
  439. const { items } = data;
  440. const trackObjects = items.map(item => item.track);
  441. const newSpotifyTracks = trackObjects.map(trackObject =>
  442. spotifyTrackObjectToMusareTrackObject(trackObject)
  443. );
  444. spotifyTracks = spotifyTracks.concat(newSpotifyTracks);
  445. next();
  446. })
  447. .catch(err => next(err));
  448. }, 1000);
  449. },
  450. err => {
  451. if (err) next(err);
  452. else {
  453. return SpotifyModule.runJob("CREATE_TRACKS", { spotifyTracks }, this)
  454. .then(() => {
  455. next(
  456. null,
  457. spotifyTracks.map(spotifyTrack => spotifyTrack.trackId)
  458. );
  459. })
  460. .catch(next);
  461. }
  462. }
  463. );
  464. }
  465. ],
  466. (err, soundcloudTrackIds) => {
  467. if (err && err !== true) {
  468. SpotifyModule.log(
  469. "ERROR",
  470. "GET_PLAYLIST",
  471. "Some error has occurred.",
  472. typeof err === "string" ? err : err.message
  473. );
  474. reject(new Error(typeof err === "string" ? err : err.message));
  475. } else {
  476. resolve({ songs: soundcloudTrackIds });
  477. }
  478. }
  479. );
  480. // kind;
  481. });
  482. }
  483. /**
  484. *
  485. * @param {*} payload
  486. * @returns
  487. */
  488. async GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACKS(payload) {
  489. const { mediaSources } = payload;
  490. // console.log("KR*S94955", mediaSources);
  491. // this.pub
  492. await async.eachLimit(mediaSources, 1, async mediaSource => {
  493. try {
  494. const result = await SpotifyModule.runJob(
  495. "GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACK",
  496. { mediaSource },
  497. this
  498. );
  499. this.publishProgress({
  500. status: "working",
  501. message: `Got alternative media for ${mediaSource}`,
  502. data: {
  503. mediaSource,
  504. status: "success",
  505. result
  506. }
  507. });
  508. } catch (err) {
  509. this.publishProgress({
  510. status: "working",
  511. message: `Failed to get alternative media for ${mediaSource}`,
  512. data: {
  513. mediaSource,
  514. status: "error"
  515. }
  516. });
  517. }
  518. });
  519. console.log("Done!");
  520. this.publishProgress({
  521. status: "finished",
  522. message: `Finished getting alternative media`
  523. });
  524. }
  525. /**
  526. *
  527. * @param {*} payload
  528. * @returns
  529. */
  530. async GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACK(payload) {
  531. const { mediaSource } = payload;
  532. if (!mediaSource || !mediaSource.startsWith("spotify:"))
  533. throw new Error("Media source provided is not a valid Spotify media source.");
  534. const spotifyTrackId = mediaSource.split(":")[1];
  535. const { track: spotifyTrack } = await SpotifyModule.runJob(
  536. "GET_TRACK",
  537. {
  538. identifier: spotifyTrackId,
  539. createMissing: true
  540. },
  541. this
  542. );
  543. const ISRC = spotifyTrack.externalIds.isrc;
  544. if (!ISRC) throw new Error(`ISRC not found for Spotify track ${mediaSource}.`);
  545. const ISRCApiResponse = await MusicBrainzModule.runJob(
  546. "API_CALL",
  547. {
  548. url: `https://musicbrainz.org/ws/2/isrc/${ISRC}`,
  549. params: {
  550. fmt: "json",
  551. inc: "url-rels+work-rels"
  552. }
  553. },
  554. this
  555. );
  556. console.dir(ISRCApiResponse, { depth: 5 });
  557. const mediaSources = new Set();
  558. const mediaSourcesOrigins = {};
  559. const jobsToRun = [];
  560. ISRCApiResponse.recordings.forEach(recording => {
  561. recording.relations.forEach(relation => {
  562. if (relation["target-type"] === "url" && relation.url) {
  563. // relation["type-id"] === "7e41ef12-a124-4324-afdb-fdbae687a89c"
  564. const { resource } = relation.url;
  565. if (resource.indexOf("soundcloud.com") !== -1) {
  566. // throw new Error(`Unable to parse SoundCloud resource ${resource}.`);
  567. const promise = new Promise(resolve => {
  568. SoundcloudModule.runJob(
  569. "GET_TRACK_FROM_URL",
  570. { identifier: resource, createMissing: true },
  571. this
  572. )
  573. .then(response => {
  574. const { trackId } = response.track;
  575. const mediaSource = `soundcloud:${trackId}`;
  576. const mediaSourceOrigins = [
  577. `Spotify track ${spotifyTrackId}`,
  578. `ISRC ${ISRC}`,
  579. `MusicBrainz recordings`,
  580. `MusicBrainz recording ${recording.id}`,
  581. `MusicBrainz relations`,
  582. `MusicBrainz relation target-type url`,
  583. `MusicBrainz relation resource ${resource}`,
  584. `SoundCloud ID ${trackId}`
  585. ];
  586. mediaSources.add(mediaSource);
  587. if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  588. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  589. resolve();
  590. })
  591. .catch(() => {
  592. resolve();
  593. });
  594. });
  595. jobsToRun.push(promise);
  596. return;
  597. }
  598. if (resource.indexOf("youtube.com") !== -1 || resource.indexOf("youtu.be") !== -1) {
  599. const match = youtubeVideoUrlRegex.exec(resource);
  600. if (!match) throw new Error(`Unable to parse YouTube resource ${resource}.`);
  601. const { youtubeId } = match.groups;
  602. if (!youtubeId) throw new Error(`Unable to parse YouTube resource ${resource}.`);
  603. const mediaSource = `youtube:${youtubeId}`;
  604. const mediaSourceOrigins = [
  605. `Spotify track ${spotifyTrackId}`,
  606. `ISRC ${ISRC}`,
  607. `MusicBrainz recordings`,
  608. `MusicBrainz recording ${recording.id}`,
  609. `MusicBrainz relations`,
  610. `MusicBrainz relation target-type url`,
  611. `MusicBrainz relation resource ${resource}`,
  612. `YouTube ID ${youtubeId}`
  613. ];
  614. mediaSources.add(mediaSource);
  615. if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  616. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  617. return;
  618. }
  619. return;
  620. }
  621. if (relation["target-type"] === "work") {
  622. console.log(relation, "GET WORK HERE");
  623. const promise = new Promise(resolve => {
  624. WikiDataModule.runJob("API_GET_DATA_FROM_MUSICBRAINZ_WORK", { workId: relation.work.id }, this)
  625. .then(resultBody => {
  626. console.log("KRISWORKSUCCESS", resultBody);
  627. const youtubeIds = Array.from(
  628. new Set(
  629. resultBody.results.bindings
  630. .filter(binding => !!binding.YouTube_video_ID)
  631. .map(binding => binding.YouTube_video_ID.value)
  632. )
  633. );
  634. const soundcloudIds = Array.from(
  635. new Set(
  636. resultBody.results.bindings
  637. .filter(binding => !!binding["SoundCloud_track_ID"])
  638. .map(binding => binding["SoundCloud_track_ID"].value)
  639. )
  640. );
  641. youtubeIds.forEach(youtubeId => {
  642. const mediaSource = `youtube:${youtubeId}`;
  643. const mediaSourceOrigins = [
  644. `Spotify track ${spotifyTrackId}`,
  645. `ISRC ${ISRC}`,
  646. `MusicBrainz recordings`,
  647. `MusicBrainz recording ${recording.id}`,
  648. `MusicBrainz relations`,
  649. `MusicBrainz relation target-type work`,
  650. `MusicBrainz relation work id ${relation.work.id}`,
  651. `WikiData select from MusicBrainz work id ${relation.work.id}`,
  652. `YouTube ID ${youtubeId}`
  653. ];
  654. mediaSources.add(mediaSource);
  655. if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  656. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  657. });
  658. soundcloudIds.forEach(soundcloudId => {
  659. const mediaSource = `soundcloud:${soundcloudId}`;
  660. const mediaSourceOrigins = [
  661. `Spotify track ${spotifyTrackId}`,
  662. `ISRC ${ISRC}`,
  663. `MusicBrainz recordings`,
  664. `MusicBrainz recording ${recording.id}`,
  665. `MusicBrainz relations`,
  666. `MusicBrainz relation target-type work`,
  667. `MusicBrainz relation work id ${relation.work.id}`,
  668. `WikiData select from MusicBrainz work id ${relation.work.id}`,
  669. `SoundCloud ID ${soundcloudId}`
  670. ];
  671. mediaSources.add(mediaSource);
  672. if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  673. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  674. });
  675. console.log("KRISWORKWOW", youtubeIds, soundcloudIds);
  676. resolve();
  677. })
  678. .catch(err => {
  679. console.log("KRISWORKERR", err);
  680. resolve();
  681. });
  682. });
  683. jobsToRun.push(promise);
  684. //WikiDataModule.runJob("API_GET_DATA_FROM_MUSICBRAINZ_WORK", { workId: relation.work.id }, this));
  685. return;
  686. }
  687. });
  688. });
  689. await Promise.allSettled(jobsToRun);
  690. return {
  691. mediaSources: Array.from(mediaSources),
  692. mediaSourcesOrigins
  693. };
  694. }
  695. }
  696. export default new _SpotifyModule();