spotify.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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, collectAlternativeMediaSourcesOrigins } = 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, collectAlternativeMediaSourcesOrigins },
  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, collectAlternativeMediaSourcesOrigins } = 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 mediaSources = new Set();
  546. const mediaSourcesOrigins = {};
  547. const jobsToRun = [];
  548. const ISRCApiResponse = await MusicBrainzModule.runJob(
  549. "API_CALL",
  550. {
  551. url: `https://musicbrainz.org/ws/2/isrc/${ISRC}`,
  552. params: {
  553. fmt: "json",
  554. inc: "url-rels+work-rels"
  555. }
  556. },
  557. this
  558. );
  559. // console.log("ISRCApiResponse");
  560. // console.dir(ISRCApiResponse, { depth: 5 });
  561. ISRCApiResponse.recordings.forEach(recording => {
  562. recording.relations.forEach(relation => {
  563. if (relation["target-type"] === "url" && relation.url) {
  564. // relation["type-id"] === "7e41ef12-a124-4324-afdb-fdbae687a89c"
  565. const { resource } = relation.url;
  566. if (resource.indexOf("soundcloud.com") !== -1) {
  567. // throw new Error(`Unable to parse SoundCloud resource ${resource}.`);
  568. const promise = new Promise(resolve => {
  569. SoundcloudModule.runJob(
  570. "GET_TRACK_FROM_URL",
  571. { identifier: resource, createMissing: true },
  572. this
  573. )
  574. .then(response => {
  575. const { trackId } = response.track;
  576. const mediaSource = `soundcloud:${trackId}`;
  577. mediaSources.add(mediaSource);
  578. if (collectAlternativeMediaSourcesOrigins) {
  579. const mediaSourceOrigins = [
  580. `Spotify track ${spotifyTrackId}`,
  581. `ISRC ${ISRC}`,
  582. `MusicBrainz recordings`,
  583. `MusicBrainz recording ${recording.id}`,
  584. `MusicBrainz relations`,
  585. `MusicBrainz relation target-type url`,
  586. `MusicBrainz relation resource ${resource}`,
  587. `SoundCloud ID ${trackId}`
  588. ];
  589. if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  590. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  591. }
  592. resolve();
  593. })
  594. .catch(() => {
  595. resolve();
  596. });
  597. });
  598. jobsToRun.push(promise);
  599. return;
  600. }
  601. if (resource.indexOf("youtube.com") !== -1 || resource.indexOf("youtu.be") !== -1) {
  602. const match = youtubeVideoUrlRegex.exec(resource);
  603. if (!match) throw new Error(`Unable to parse YouTube resource ${resource}.`);
  604. const { youtubeId } = match.groups;
  605. if (!youtubeId) throw new Error(`Unable to parse YouTube resource ${resource}.`);
  606. const mediaSource = `youtube:${youtubeId}`;
  607. mediaSources.add(mediaSource);
  608. if (collectAlternativeMediaSourcesOrigins) {
  609. const mediaSourceOrigins = [
  610. `Spotify track ${spotifyTrackId}`,
  611. `ISRC ${ISRC}`,
  612. `MusicBrainz recordings`,
  613. `MusicBrainz recording ${recording.id}`,
  614. `MusicBrainz relations`,
  615. `MusicBrainz relation target-type url`,
  616. `MusicBrainz relation resource ${resource}`,
  617. `YouTube ID ${youtubeId}`
  618. ];
  619. if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  620. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  621. }
  622. return;
  623. }
  624. return;
  625. }
  626. if (relation["target-type"] === "work") {
  627. console.log(relation, "GET WORK HERE");
  628. const promise = new Promise(resolve => {
  629. WikiDataModule.runJob("API_GET_DATA_FROM_MUSICBRAINZ_WORK", { workId: relation.work.id }, this)
  630. .then(resultBody => {
  631. const youtubeIds = Array.from(
  632. new Set(
  633. resultBody.results.bindings
  634. .filter(binding => !!binding.YouTube_video_ID)
  635. .map(binding => binding.YouTube_video_ID.value)
  636. )
  637. );
  638. // const soundcloudIds = Array.from(
  639. // new Set(
  640. // resultBody.results.bindings
  641. // .filter(binding => !!binding["SoundCloud_track_ID"])
  642. // .map(binding => binding["SoundCloud_track_ID"].value)
  643. // )
  644. // );
  645. youtubeIds.forEach(youtubeId => {
  646. const mediaSource = `youtube:${youtubeId}`;
  647. mediaSources.add(mediaSource);
  648. if (collectAlternativeMediaSourcesOrigins) {
  649. const mediaSourceOrigins = [
  650. `Spotify track ${spotifyTrackId}`,
  651. `ISRC ${ISRC}`,
  652. `MusicBrainz recordings`,
  653. `MusicBrainz recording ${recording.id}`,
  654. `MusicBrainz relations`,
  655. `MusicBrainz relation target-type work`,
  656. `MusicBrainz relation work id ${relation.work.id}`,
  657. `WikiData select from MusicBrainz work id ${relation.work.id}`,
  658. `YouTube ID ${youtubeId}`
  659. ];
  660. if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  661. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  662. }
  663. });
  664. // soundcloudIds.forEach(soundcloudId => {
  665. // const mediaSource = `soundcloud:${soundcloudId}`;
  666. // mediaSources.add(mediaSource);
  667. // if (collectAlternativeMediaSourcesOrigins) {
  668. // const mediaSourceOrigins = [
  669. // `Spotify track ${spotifyTrackId}`,
  670. // `ISRC ${ISRC}`,
  671. // `MusicBrainz recordings`,
  672. // `MusicBrainz recording ${recording.id}`,
  673. // `MusicBrainz relations`,
  674. // `MusicBrainz relation target-type work`,
  675. // `MusicBrainz relation work id ${relation.work.id}`,
  676. // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  677. // `SoundCloud ID ${soundcloudId}`
  678. // ];
  679. // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  680. // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  681. // }
  682. // });
  683. resolve();
  684. })
  685. .catch(err => {
  686. console.log("KRISWORKERR", err);
  687. resolve();
  688. });
  689. });
  690. jobsToRun.push(promise);
  691. //WikiDataModule.runJob("API_GET_DATA_FROM_MUSICBRAINZ_WORK", { workId: relation.work.id }, this));
  692. return;
  693. }
  694. });
  695. });
  696. const RecordingApiResponse = await MusicBrainzModule.runJob(
  697. "API_CALL",
  698. {
  699. url: `https://musicbrainz.org/ws/2/recording/`,
  700. params: {
  701. fmt: "json",
  702. query: `isrc:${ISRC}`
  703. }
  704. },
  705. this
  706. );
  707. const releaseIds = new Set();
  708. const releaseGroupIds = new Set();
  709. RecordingApiResponse.recordings.forEach(recording => {
  710. const recordingId = recording.id;
  711. // console.log("Recording:", recording.id);
  712. recording.releases.forEach(release => {
  713. const releaseId = release.id;
  714. // console.log("Release:", releaseId);
  715. const releaseGroupId = release["release-group"].id;
  716. // console.log("Release group:", release["release-group"]);
  717. // console.log("Release group id:", release["release-group"].id);
  718. // console.log("Release group type id:", release["release-group"]["type-id"]);
  719. // console.log("Release group primary type id:", release["release-group"]["primary-type-id"]);
  720. // console.log("Release group primary type:", release["release-group"]["primary-type"]);
  721. // d6038452-8ee0-3f68-affc-2de9a1ede0b9 = single
  722. // 6d0c5bf6-7a33-3420-a519-44fc63eedebf = EP
  723. if (
  724. release["release-group"]["type-id"] === "d6038452-8ee0-3f68-affc-2de9a1ede0b9" ||
  725. release["release-group"]["type-id"] === "6d0c5bf6-7a33-3420-a519-44fc63eedebf"
  726. ) {
  727. releaseIds.add(releaseId);
  728. releaseGroupIds.add(releaseGroupId);
  729. }
  730. });
  731. });
  732. Array.from(releaseGroupIds).forEach(releaseGroupId => {
  733. const promise = new Promise(resolve => {
  734. WikiDataModule.runJob("API_GET_DATA_FROM_MUSICBRAINZ_RELEASE_GROUP", { releaseGroupId }, this)
  735. .then(resultBody => {
  736. const youtubeIds = Array.from(
  737. new Set(
  738. resultBody.results.bindings
  739. .filter(binding => !!binding.YouTube_video_ID)
  740. .map(binding => binding.YouTube_video_ID.value)
  741. )
  742. );
  743. // const soundcloudIds = Array.from(
  744. // new Set(
  745. // resultBody.results.bindings
  746. // .filter(binding => !!binding["SoundCloud_track_ID"])
  747. // .map(binding => binding["SoundCloud_track_ID"].value)
  748. // )
  749. // );
  750. youtubeIds.forEach(youtubeId => {
  751. const mediaSource = `youtube:${youtubeId}`;
  752. mediaSources.add(mediaSource);
  753. // if (collectAlternativeMediaSourcesOrigins) {
  754. // const mediaSourceOrigins = [
  755. // `Spotify track ${spotifyTrackId}`,
  756. // `ISRC ${ISRC}`,
  757. // `MusicBrainz recordings`,
  758. // `MusicBrainz recording ${recording.id}`,
  759. // `MusicBrainz relations`,
  760. // `MusicBrainz relation target-type work`,
  761. // `MusicBrainz relation work id ${relation.work.id}`,
  762. // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  763. // `YouTube ID ${youtubeId}`
  764. // ];
  765. // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  766. // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  767. // }
  768. });
  769. // soundcloudIds.forEach(soundcloudId => {
  770. // const mediaSource = `soundcloud:${soundcloudId}`;
  771. // mediaSources.add(mediaSource);
  772. // // if (collectAlternativeMediaSourcesOrigins) {
  773. // // const mediaSourceOrigins = [
  774. // // `Spotify track ${spotifyTrackId}`,
  775. // // `ISRC ${ISRC}`,
  776. // // `MusicBrainz recordings`,
  777. // // `MusicBrainz recording ${recording.id}`,
  778. // // `MusicBrainz relations`,
  779. // // `MusicBrainz relation target-type work`,
  780. // // `MusicBrainz relation work id ${relation.work.id}`,
  781. // // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  782. // // `SoundCloud ID ${soundcloudId}`
  783. // // ];
  784. // // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  785. // // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  786. // // }
  787. // });
  788. resolve();
  789. })
  790. .catch(err => {
  791. console.log("KRISWORKERR", err);
  792. resolve();
  793. });
  794. });
  795. jobsToRun.push(promise);
  796. });
  797. // console.log("RecordingApiResponse");
  798. // console.dir(RecordingApiResponse, { depth: 10 });
  799. // console.dir(RecordingApiResponse.recordings[0].releases[0], { depth: 10 });
  800. await Promise.allSettled(jobsToRun);
  801. return {
  802. mediaSources: Array.from(mediaSources),
  803. mediaSourcesOrigins
  804. };
  805. }
  806. }
  807. export default new _SpotifyModule();