spotify.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503
  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 spotifyTrackObjectToMusareTrackObject = spotifyTrackObject => ({
  18. trackId: spotifyTrackObject.id,
  19. name: spotifyTrackObject.name,
  20. albumId: spotifyTrackObject.album.id,
  21. albumTitle: spotifyTrackObject.album.title,
  22. albumImageUrl: spotifyTrackObject.album.images[0].url,
  23. artists: spotifyTrackObject.artists.map(artist => artist.name),
  24. artistIds: spotifyTrackObject.artists.map(artist => artist.id),
  25. duration: spotifyTrackObject.duration_ms / 1000,
  26. explicit: spotifyTrackObject.explicit,
  27. externalIds: spotifyTrackObject.external_ids,
  28. popularity: spotifyTrackObject.popularity,
  29. isLocal: spotifyTrackObject.is_local
  30. });
  31. class RateLimitter {
  32. /**
  33. * Constructor
  34. *
  35. * @param {number} timeBetween - The time between each allowed YouTube request
  36. */
  37. constructor(timeBetween) {
  38. this.dateStarted = Date.now();
  39. this.timeBetween = timeBetween;
  40. }
  41. /**
  42. * Returns a promise that resolves whenever the ratelimit of a YouTube request is done
  43. *
  44. * @returns {Promise} - promise that gets resolved when the rate limit allows it
  45. */
  46. continue() {
  47. return new Promise(resolve => {
  48. if (Date.now() - this.dateStarted >= this.timeBetween) resolve();
  49. else setTimeout(resolve, this.dateStarted + this.timeBetween - Date.now());
  50. });
  51. }
  52. /**
  53. * Restart the rate limit timer
  54. */
  55. restart() {
  56. this.dateStarted = Date.now();
  57. }
  58. }
  59. class _SpotifyModule extends CoreClass {
  60. // eslint-disable-next-line require-jsdoc
  61. constructor() {
  62. super("spotify");
  63. SpotifyModule = this;
  64. }
  65. /**
  66. * Initialises the spotify module
  67. *
  68. * @returns {Promise} - returns promise (reject, resolve)
  69. */
  70. async initialize() {
  71. DBModule = this.moduleManager.modules.db;
  72. CacheModule = this.moduleManager.modules.cache;
  73. MediaModule = this.moduleManager.modules.media;
  74. MusicBrainzModule = this.moduleManager.modules.musicbrainz;
  75. SoundcloudModule = this.moduleManager.modules.soundcloud;
  76. WikiDataModule = this.moduleManager.modules.wikidata;
  77. this.spotifyTrackModel = this.SpotifyTrackModel = await DBModule.runJob("GET_MODEL", {
  78. modelName: "spotifyTrack"
  79. });
  80. this.spotifyAlbumModel = this.SpotifyAlbumModel = await DBModule.runJob("GET_MODEL", {
  81. modelName: "spotifyAlbum"
  82. });
  83. this.spotifyArtistModel = this.SpotifyArtistModel = await DBModule.runJob("GET_MODEL", {
  84. modelName: "spotifyArtist"
  85. });
  86. return new Promise((resolve, reject) => {
  87. if (!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. * Fetches a Spotify API token from either the cache, or Spotify using the client id and secret from the config
  105. *
  106. * @returns {Promise} - returns promise (reject, resolve)
  107. */
  108. GET_API_TOKEN() {
  109. return new Promise((resolve, reject) => {
  110. CacheModule.runJob("GET", { key: "spotifyApiKey" }, this).then(spotifyApiKey => {
  111. if (spotifyApiKey) {
  112. resolve(spotifyApiKey);
  113. return;
  114. }
  115. this.log("INFO", `No Spotify API token stored in cache, requesting new token.`);
  116. const clientId = config.get("apis.spotify.clientId");
  117. const clientSecret = config.get("apis.spotify.clientSecret");
  118. const unencoded = `${clientId}:${clientSecret}`;
  119. const encoded = Buffer.from(unencoded).toString("base64");
  120. const params = new url.URLSearchParams({ grant_type: "client_credentials" });
  121. SpotifyModule.axios
  122. .post("https://accounts.spotify.com/api/token", params.toString(), {
  123. headers: {
  124. Authorization: `Basic ${encoded}`,
  125. "Content-Type": "application/x-www-form-urlencoded"
  126. }
  127. })
  128. .then(res => {
  129. const { access_token: accessToken, expires_in: expiresIn } = res.data;
  130. // TODO TTL can be later if stuck in queue
  131. CacheModule.runJob(
  132. "SET",
  133. { key: "spotifyApiKey", value: accessToken, ttl: expiresIn - 30 },
  134. this
  135. )
  136. .then(spotifyApiKey => {
  137. this.log(
  138. "SUCCESS",
  139. `Stored new Spotify API token in cache. Expires in ${expiresIn - 30}`
  140. );
  141. resolve(spotifyApiKey);
  142. })
  143. .catch(err => {
  144. this.log(
  145. "ERROR",
  146. `Failed to store new Spotify API token in cache.`,
  147. typeof err === "string" ? err : err.message
  148. );
  149. reject(err);
  150. });
  151. })
  152. .catch(err => {
  153. this.log(
  154. "ERROR",
  155. `Failed to get new Spotify API token.`,
  156. typeof err === "string" ? err : err.message
  157. );
  158. reject(err);
  159. });
  160. });
  161. });
  162. }
  163. /**
  164. * Perform Spotify API get albums request
  165. *
  166. * @param {object} payload - object that contains the payload
  167. * @param {array} payload.albumIds - the album ids to get
  168. * @returns {Promise} - returns promise (reject, resolve)
  169. */
  170. API_GET_ALBUMS(payload) {
  171. return new Promise((resolve, reject) => {
  172. const { albumIds } = payload;
  173. SpotifyModule.runJob(
  174. "API_CALL",
  175. {
  176. url: `https://api.spotify.com/v1/albums`,
  177. params: {
  178. ids: albumIds.join(",")
  179. }
  180. },
  181. this
  182. )
  183. .then(response => {
  184. resolve(response);
  185. })
  186. .catch(err => {
  187. reject(err);
  188. });
  189. });
  190. }
  191. /**
  192. * Perform Spotify API get artists request
  193. *
  194. * @param {object} payload - object that contains the payload
  195. * @param {array} payload.artistIds - the artist ids to get
  196. * @returns {Promise} - returns promise (reject, resolve)
  197. */
  198. API_GET_ARTISTS(payload) {
  199. return new Promise((resolve, reject) => {
  200. const { artistIds } = payload;
  201. SpotifyModule.runJob(
  202. "API_CALL",
  203. {
  204. url: `https://api.spotify.com/v1/artists`,
  205. params: {
  206. ids: artistIds.join(",")
  207. }
  208. },
  209. this
  210. )
  211. .then(response => {
  212. resolve(response);
  213. })
  214. .catch(err => {
  215. reject(err);
  216. });
  217. });
  218. }
  219. /**
  220. * Perform Spotify API get track request
  221. *
  222. * @param {object} payload - object that contains the payload
  223. * @param {string} payload.trackId - the Spotify track id to get
  224. * @returns {Promise} - returns promise (reject, resolve)
  225. */
  226. API_GET_TRACK(payload) {
  227. return new Promise((resolve, reject) => {
  228. const { trackId } = payload;
  229. SpotifyModule.runJob(
  230. "API_CALL",
  231. {
  232. url: `https://api.spotify.com/v1/tracks/${trackId}`
  233. },
  234. this
  235. )
  236. .then(response => {
  237. resolve(response);
  238. })
  239. .catch(err => {
  240. reject(err);
  241. });
  242. });
  243. }
  244. /**
  245. * Perform Spotify API get playlist request
  246. *
  247. * @param {object} payload - object that contains the payload
  248. * @param {string} payload.playlistId - the Spotify playlist id to get songs from
  249. * @param {string} payload.nextUrl - the next URL to use
  250. * @returns {Promise} - returns promise (reject, resolve)
  251. */
  252. API_GET_PLAYLIST(payload) {
  253. return new Promise((resolve, reject) => {
  254. const { playlistId, nextUrl } = payload;
  255. SpotifyModule.runJob(
  256. "API_CALL",
  257. {
  258. url: nextUrl || `https://api.spotify.com/v1/playlists/${playlistId}/tracks`
  259. },
  260. this
  261. )
  262. .then(response => {
  263. resolve(response);
  264. })
  265. .catch(err => {
  266. reject(err);
  267. });
  268. });
  269. }
  270. /**
  271. * Perform Spotify API call
  272. *
  273. * @param {object} payload - object that contains the payload
  274. * @param {string} payload.url - request url
  275. * @param {object} payload.params - request parameters
  276. * @returns {Promise} - returns promise (reject, resolve)
  277. */
  278. API_CALL(payload) {
  279. return new Promise((resolve, reject) => {
  280. const { url, params } = payload;
  281. SpotifyModule.runJob("GET_API_TOKEN", {}, this)
  282. .then(spotifyApiToken => {
  283. SpotifyModule.axios
  284. .get(url, {
  285. headers: {
  286. Authorization: `Bearer ${spotifyApiToken}`
  287. },
  288. timeout: SpotifyModule.requestTimeout,
  289. params
  290. })
  291. .then(response => {
  292. if (response.data.error) {
  293. reject(new Error(response.data.error));
  294. } else {
  295. resolve({ response });
  296. }
  297. })
  298. .catch(err => {
  299. reject(err);
  300. });
  301. })
  302. .catch(err => {
  303. this.log(
  304. "ERROR",
  305. `Spotify API call failed as an error occured whilst getting the API token`,
  306. typeof err === "string" ? err : err.message
  307. );
  308. resolve(err);
  309. });
  310. });
  311. }
  312. /**
  313. * Create Spotify track
  314. *
  315. * @param {object} payload - an object containing the payload
  316. * @param {array} payload.spotifyTracks - the spotifyTracks
  317. * @returns {Promise} - returns a promise (resolve, reject)
  318. */
  319. CREATE_TRACKS(payload) {
  320. return new Promise((resolve, reject) => {
  321. async.waterfall(
  322. [
  323. next => {
  324. const { spotifyTracks } = payload;
  325. if (!Array.isArray(spotifyTracks)) next("Invalid spotifyTracks type");
  326. else {
  327. const trackIds = spotifyTracks.map(spotifyTrack => spotifyTrack.trackId);
  328. SpotifyModule.spotifyTrackModel.find({ trackId: trackIds }, (err, existingTracks) => {
  329. if (err) {
  330. next(err);
  331. return;
  332. }
  333. const existingTrackIds = existingTracks.map(existingTrack => existingTrack.trackId);
  334. const newSpotifyTracks = spotifyTracks.filter(
  335. spotifyTrack => existingTrackIds.indexOf(spotifyTrack.trackId) === -1
  336. );
  337. SpotifyModule.spotifyTrackModel.insertMany(newSpotifyTracks, next);
  338. });
  339. }
  340. },
  341. (spotifyTracks, next) => {
  342. const mediaSources = spotifyTracks.map(spotifyTrack => `spotify:${spotifyTrack.trackId}`);
  343. async.eachLimit(
  344. mediaSources,
  345. 2,
  346. (mediaSource, next) => {
  347. MediaModule.runJob("RECALCULATE_RATINGS", { mediaSource }, this)
  348. .then(() => next())
  349. .catch(next);
  350. },
  351. err => {
  352. if (err) next(err);
  353. else next(null, spotifyTracks);
  354. }
  355. );
  356. }
  357. ],
  358. (err, spotifyTracks) => {
  359. if (err) reject(new Error(err));
  360. else resolve({ spotifyTracks });
  361. }
  362. );
  363. });
  364. }
  365. /**
  366. * Create Spotify albums
  367. *
  368. * @param {object} payload - an object containing the payload
  369. * @param {array} payload.spotifyAlbums - the Spotify albums
  370. * @returns {Promise} - returns a promise (resolve, reject)
  371. */
  372. async CREATE_ALBUMS(payload) {
  373. const { spotifyAlbums } = payload;
  374. if (!Array.isArray(spotifyAlbums)) throw new Error("Invalid spotifyAlbums type");
  375. const albumIds = spotifyAlbums.map(spotifyAlbum => spotifyAlbum.albumId);
  376. const existingAlbums = (await SpotifyModule.spotifyAlbumModel.find({ albumId: albumIds })).map(
  377. album => album._doc
  378. );
  379. const existingAlbumIds = existingAlbums.map(existingAlbum => existingAlbum.albumId);
  380. const newSpotifyAlbums = spotifyAlbums.filter(
  381. spotifyAlbum => existingAlbumIds.indexOf(spotifyAlbum.albumId) === -1
  382. );
  383. if (newSpotifyAlbums.length === 0) return existingAlbums;
  384. await SpotifyModule.spotifyAlbumModel.insertMany(newSpotifyAlbums);
  385. return existingAlbums.concat(newSpotifyAlbums);
  386. }
  387. /**
  388. * Create Spotify artists
  389. *
  390. * @param {object} payload - an object containing the payload
  391. * @param {array} payload.spotifyArtists - the Spotify artists
  392. * @returns {Promise} - returns a promise (resolve, reject)
  393. */
  394. async CREATE_ARTISTS(payload) {
  395. const { spotifyArtists } = payload;
  396. if (!Array.isArray(spotifyArtists)) throw new Error("Invalid spotifyArtists type");
  397. const artistIds = spotifyArtists.map(spotifyArtist => spotifyArtist.artistId);
  398. const existingArtists = (await SpotifyModule.spotifyArtistModel.find({ artistId: artistIds })).map(
  399. artist => artist._doc
  400. );
  401. const existingArtistIds = existingArtists.map(existingArtist => existingArtist.artistId);
  402. const newSpotifyArtists = spotifyArtists.filter(
  403. spotifyArtist => existingArtistIds.indexOf(spotifyArtist.artistId) === -1
  404. );
  405. if (newSpotifyArtists.length === 0) return existingArtists;
  406. await SpotifyModule.spotifyArtistModel.insertMany(newSpotifyArtists);
  407. return existingArtists.concat(newSpotifyArtists);
  408. }
  409. /**
  410. * Gets tracks from media sources
  411. *
  412. * @param {object} payload - object that contains the payload
  413. * @param {array} payload.mediaSources - the media sources to get tracks from
  414. * @returns {Promise} - returns promise (reject, resolve)
  415. */
  416. async GET_TRACKS_FROM_MEDIA_SOURCES(payload) {
  417. return new Promise((resolve, reject) => {
  418. const { mediaSources } = payload;
  419. const responses = {};
  420. const promises = [];
  421. mediaSources.forEach(mediaSource => {
  422. promises.push(
  423. new Promise(resolve => {
  424. const trackId = mediaSource.split(":")[1];
  425. SpotifyModule.runJob("GET_TRACK", { identifier: trackId, createMissing: true }, this)
  426. .then(({ track }) => {
  427. responses[mediaSource] = track;
  428. })
  429. .catch(err => {
  430. SpotifyModule.log(
  431. "ERROR",
  432. `Getting tracked with media source ${mediaSource} failed.`,
  433. typeof err === "string" ? err : err.message
  434. );
  435. responses[mediaSource] = typeof err === "string" ? err : err.message;
  436. })
  437. .finally(() => {
  438. resolve();
  439. });
  440. })
  441. );
  442. });
  443. Promise.all(promises)
  444. .then(() => {
  445. SpotifyModule.log("SUCCESS", `Got all tracks.`);
  446. resolve({ tracks: responses });
  447. })
  448. .catch(reject);
  449. });
  450. }
  451. /**
  452. * Gets albums from Spotify album ids
  453. *
  454. * @param {object} payload - object that contains the payload
  455. * @param {array} payload.albumIds - the Spotify album ids
  456. * @returns {Promise} - returns promise (reject, resolve)
  457. */
  458. async GET_ALBUMS_FROM_IDS(payload) {
  459. const { albumIds } = payload;
  460. const existingAlbums = (await SpotifyModule.spotifyAlbumModel.find({ albumId: albumIds })).map(
  461. album => album._doc
  462. );
  463. const existingAlbumIds = existingAlbums.map(existingAlbum => existingAlbum.albumId);
  464. const missingAlbumIds = albumIds.filter(albumId => existingAlbumIds.indexOf(albumId) === -1);
  465. if (missingAlbumIds.length === 0) return existingAlbums;
  466. const jobsToRun = [];
  467. const chunkSize = 2;
  468. while (missingAlbumIds.length > 0) {
  469. const chunkedMissingAlbumIds = missingAlbumIds.splice(0, chunkSize);
  470. jobsToRun.push(SpotifyModule.runJob("API_GET_ALBUMS", { albumIds: chunkedMissingAlbumIds }, this));
  471. }
  472. const jobResponses = await Promise.all(jobsToRun);
  473. const newAlbums = jobResponses
  474. .map(jobResponse => jobResponse.response.data.albums)
  475. .flat()
  476. .map(album => ({
  477. albumId: album.id,
  478. rawData: album
  479. }));
  480. await SpotifyModule.runJob("CREATE_ALBUMS", { spotifyAlbums: newAlbums }, this);
  481. return existingAlbums.concat(newAlbums);
  482. }
  483. /**
  484. * Gets Spotify artists from Spotify artist ids
  485. *
  486. * @param {object} payload - object that contains the payload
  487. * @param {array} payload.artistIds - the Spotify artist ids
  488. * @returns {Promise} - returns promise (reject, resolve)
  489. */
  490. async GET_ARTISTS_FROM_IDS(payload) {
  491. const { artistIds } = payload;
  492. const existingArtists = (await SpotifyModule.spotifyArtistModel.find({ artistId: artistIds })).map(
  493. artist => artist._doc
  494. );
  495. const existingArtistIds = existingArtists.map(existingArtist => existingArtist.artistId);
  496. const missingArtistIds = artistIds.filter(artistId => existingArtistIds.indexOf(artistId) === -1);
  497. if (missingArtistIds.length === 0) return existingArtists;
  498. const jobsToRun = [];
  499. const chunkSize = 50;
  500. while (missingArtistIds.length > 0) {
  501. const chunkedMissingArtistIds = missingArtistIds.splice(0, chunkSize);
  502. jobsToRun.push(SpotifyModule.runJob("API_GET_ARTISTS", { artistIds: chunkedMissingArtistIds }, this));
  503. }
  504. const jobResponses = await Promise.all(jobsToRun);
  505. const newArtists = jobResponses
  506. .map(jobResponse => jobResponse.response.data.artists)
  507. .flat()
  508. .map(artist => ({
  509. artistId: artist.id,
  510. rawData: artist
  511. }));
  512. await SpotifyModule.runJob("CREATE_ARTISTS", { spotifyArtists: newArtists }, this);
  513. return existingArtists.concat(newArtists);
  514. }
  515. /**
  516. * Get Spotify track
  517. *
  518. * @param {object} payload - an object containing the payload
  519. * @param {string} payload.identifier - the spotify track ObjectId or track id
  520. * @param {boolean} payload.createMissing - attempt to fetch and create track if not in db
  521. * @returns {Promise} - returns a promise (resolve, reject)
  522. */
  523. GET_TRACK(payload) {
  524. return new Promise((resolve, reject) => {
  525. async.waterfall(
  526. [
  527. next => {
  528. const query = mongoose.isObjectIdOrHexString(payload.identifier)
  529. ? { _id: payload.identifier }
  530. : { trackId: payload.identifier };
  531. return SpotifyModule.spotifyTrackModel.findOne(query, next);
  532. },
  533. (track, next) => {
  534. if (track) return next(null, track, false);
  535. if (mongoose.isObjectIdOrHexString(payload.identifier) || !payload.createMissing)
  536. return next("Spotify track not found.");
  537. return SpotifyModule.runJob("API_GET_TRACK", { trackId: payload.identifier }, this)
  538. .then(({ response }) => {
  539. const { data } = response;
  540. if (!data || !data.id)
  541. return next("The specified track does not exist or cannot be publicly accessed.");
  542. const spotifyTrack = spotifyTrackObjectToMusareTrackObject(data);
  543. return next(null, false, spotifyTrack);
  544. })
  545. .catch(next);
  546. },
  547. (track, spotifyTrack, next) => {
  548. if (track) return next(null, track, true);
  549. return SpotifyModule.runJob("CREATE_TRACKS", { spotifyTracks: [spotifyTrack] }, this)
  550. .then(res => {
  551. if (res.spotifyTracks.length === 1) next(null, res.spotifyTracks[0], false);
  552. else next("Spotify track not found.");
  553. })
  554. .catch(next);
  555. }
  556. ],
  557. (err, track, existing) => {
  558. if (err) reject(new Error(err));
  559. else if (track.isLocal) reject(new Error("Track is local."));
  560. else resolve({ track, existing });
  561. }
  562. );
  563. });
  564. }
  565. /**
  566. * Get Spotify album
  567. *
  568. * @param {object} payload - an object containing the payload
  569. * @param {string} payload.identifier - the spotify album ObjectId or track id
  570. * @returns {Promise} - returns a promise (resolve, reject)
  571. */
  572. async GET_ALBUM(payload) {
  573. const query = mongoose.isObjectIdOrHexString(payload.identifier)
  574. ? { _id: payload.identifier }
  575. : { albumId: payload.identifier };
  576. const album = await SpotifyModule.spotifyAlbumModel.findOne(query);
  577. if (album) return album._doc;
  578. return null;
  579. }
  580. /**
  581. * Returns an array of songs taken from a Spotify playlist
  582. *
  583. * @param {object} payload - object that contains the payload
  584. * @param {string} payload.url - the id of the Spotify playlist
  585. * @returns {Promise} - returns promise (reject, resolve)
  586. */
  587. GET_PLAYLIST(payload) {
  588. return new Promise((resolve, reject) => {
  589. const spotifyPlaylistUrlRegex = /.+open\.spotify\.com\/playlist\/(?<playlistId>[A-Za-z0-9]+)/;
  590. const match = spotifyPlaylistUrlRegex.exec(payload.url);
  591. if (!match || !match.groups) {
  592. SpotifyModule.log("ERROR", "GET_PLAYLIST", "Invalid Spotify playlist URL query.");
  593. reject(new Error("Invalid playlist URL."));
  594. return;
  595. }
  596. const { playlistId } = match.groups;
  597. async.waterfall(
  598. [
  599. next => {
  600. let spotifyTracks = [];
  601. let total = -1;
  602. let nextUrl = "";
  603. async.whilst(
  604. next => {
  605. SpotifyModule.log(
  606. "INFO",
  607. `Getting playlist progress for job (${this.toString()}): ${
  608. spotifyTracks.length
  609. } tracks gotten so far. Total tracks: ${total}.`
  610. );
  611. next(null, nextUrl !== null);
  612. },
  613. next => {
  614. // Add 250ms delay between each job request
  615. setTimeout(() => {
  616. SpotifyModule.runJob("API_GET_PLAYLIST", { playlistId, nextUrl }, this)
  617. .then(({ response }) => {
  618. const { data } = response;
  619. if (!data) {
  620. next("The provided URL does not exist or cannot be accessed.");
  621. return;
  622. }
  623. total = data.total;
  624. nextUrl = data.next;
  625. const { items } = data;
  626. const trackObjects = items.map(item => item.track);
  627. const newSpotifyTracks = trackObjects.map(trackObject =>
  628. spotifyTrackObjectToMusareTrackObject(trackObject)
  629. );
  630. spotifyTracks = spotifyTracks.concat(newSpotifyTracks);
  631. next();
  632. })
  633. .catch(err => next(err));
  634. }, 1000);
  635. },
  636. err => {
  637. if (err) next(err);
  638. else
  639. SpotifyModule.runJob("CREATE_TRACKS", { spotifyTracks }, this)
  640. .then(() => {
  641. next(
  642. null,
  643. spotifyTracks.map(spotifyTrack => spotifyTrack.trackId)
  644. );
  645. })
  646. .catch(next);
  647. }
  648. );
  649. }
  650. ],
  651. (err, soundcloudTrackIds) => {
  652. if (err && err !== true) {
  653. SpotifyModule.log(
  654. "ERROR",
  655. "GET_PLAYLIST",
  656. "Some error has occurred.",
  657. typeof err === "string" ? err : err.message
  658. );
  659. reject(new Error(typeof err === "string" ? err : err.message));
  660. } else {
  661. resolve({ songs: soundcloudTrackIds });
  662. }
  663. }
  664. );
  665. });
  666. }
  667. /**
  668. * Tries to get alternative artists sources for a list of Spotify artist ids
  669. *
  670. * @param {object} payload - object that contains the payload
  671. * @param {string} payload.artistIds - the Spotify artist ids to try and get alternative artist sources for
  672. * @param {boolean} payload.collectAlternativeArtistSourcesOrigins - whether to collect the origin of any alternative artist sources found
  673. * @returns {Promise} - returns promise (reject, resolve)
  674. */
  675. async GET_ALTERNATIVE_ARTIST_SOURCES_FOR_ARTISTS(payload) {
  676. const { artistIds, collectAlternativeArtistSourcesOrigins } = payload;
  677. await async.eachLimit(artistIds, 1, async artistId => {
  678. try {
  679. const result = await SpotifyModule.runJob(
  680. "GET_ALTERNATIVE_ARTIST_SOURCES_FOR_ARTIST",
  681. { artistId, collectAlternativeArtistSourcesOrigins },
  682. this
  683. );
  684. this.publishProgress({
  685. status: "working",
  686. message: `Got alternative artist source for ${artistId}`,
  687. data: {
  688. artistId,
  689. status: "success",
  690. result
  691. }
  692. });
  693. } catch (err) {
  694. this.publishProgress({
  695. status: "working",
  696. message: `Failed to get alternative artist source for ${artistId}`,
  697. data: {
  698. artistId,
  699. status: "error"
  700. }
  701. });
  702. }
  703. });
  704. this.publishProgress({
  705. status: "finished",
  706. message: `Finished getting alternative artist sources`
  707. });
  708. }
  709. /**
  710. * Tries to get alternative artist sources for a Spotify artist id
  711. *
  712. * @param {object} payload - object that contains the payload
  713. * @param {string} payload.artistId - the Spotify artist id to try and get alternative artist sources for
  714. * @param {boolean} payload.collectAlternativeArtistSourcesOrigins - whether to collect the origin of any alternative artist sources found
  715. * @returns {Promise} - returns promise (reject, resolve)
  716. */
  717. async GET_ALTERNATIVE_ARTIST_SOURCES_FOR_ARTIST(payload) {
  718. const { artistId /* , collectAlternativeArtistSourcesOrigins */ } = payload;
  719. if (!artistId) throw new Error("Artist id provided is not valid.");
  720. const wikiDataResponse = await WikiDataModule.runJob(
  721. "API_GET_DATA_FROM_SPOTIFY_ARTIST",
  722. { spotifyArtistId: artistId },
  723. this
  724. );
  725. const youtubeChannelIds = Array.from(
  726. new Set(
  727. wikiDataResponse.results.bindings
  728. .filter(binding => !!binding.YouTube_channel_ID)
  729. .map(binding => binding.YouTube_channel_ID.value)
  730. )
  731. );
  732. // const soundcloudIds = Array.from(
  733. // new Set(
  734. // wikiDataResponse.results.bindings
  735. // .filter(binding => !!binding.SoundCloud_ID)
  736. // .map(binding => binding.SoundCloud_ID.value)
  737. // )
  738. // );
  739. // const musicbrainzArtistIds = Array.from(
  740. // new Set(
  741. // wikiDataResponse.results.bindings
  742. // .filter(binding => !!binding.MusicBrainz_artist_ID)
  743. // .map(binding => binding.MusicBrainz_artist_ID.value)
  744. // )
  745. // );
  746. return youtubeChannelIds;
  747. }
  748. /**
  749. * Tries to get alternative album sources for a list of Spotify album ids
  750. *
  751. * @param {object} payload - object that contains the payload
  752. * @param {string} payload.albumIds - the Spotify album ids to try and get alternative album sources for
  753. * @param {boolean} payload.collectAlternativeAlbumSourcesOrigins - whether to collect the origin of any alternative album sources found
  754. * @returns {Promise} - returns promise (reject, resolve)
  755. */
  756. async GET_ALTERNATIVE_ALBUM_SOURCES_FOR_ALBUMS(payload) {
  757. const { albumIds, collectAlternativeAlbumSourcesOrigins } = payload;
  758. await async.eachLimit(albumIds, 1, async albumId => {
  759. try {
  760. const result = await SpotifyModule.runJob(
  761. "GET_ALTERNATIVE_ALBUM_SOURCES_FOR_ALBUM",
  762. { albumId, collectAlternativeAlbumSourcesOrigins },
  763. this
  764. );
  765. this.publishProgress({
  766. status: "working",
  767. message: `Got alternative album source for ${albumId}`,
  768. data: {
  769. albumId,
  770. status: "success",
  771. result
  772. }
  773. });
  774. } catch (err) {
  775. this.publishProgress({
  776. status: "working",
  777. message: `Failed to get alternative album source for ${albumId}`,
  778. data: {
  779. albumId,
  780. status: "error"
  781. }
  782. });
  783. }
  784. });
  785. this.publishProgress({
  786. status: "finished",
  787. message: `Finished getting alternative album sources`
  788. });
  789. }
  790. /**
  791. * Tries to get alternative album sources for a Spotify album id
  792. *
  793. * @param {object} payload - object that contains the payload
  794. * @param {string} payload.albumId - the Spotify album id to try and get alternative album sources for
  795. * @param {boolean} payload.collectAlternativeAlbumSourcesOrigins - whether to collect the origin of any alternative album sources found
  796. * @returns {Promise} - returns promise (reject, resolve)
  797. */
  798. async GET_ALTERNATIVE_ALBUM_SOURCES_FOR_ALBUM(payload) {
  799. const { albumId /* , collectAlternativeAlbumSourcesOrigins */ } = payload;
  800. if (!albumId) throw new Error("Album id provided is not valid.");
  801. const wikiDataResponse = await WikiDataModule.runJob(
  802. "API_GET_DATA_FROM_SPOTIFY_ALBUM",
  803. { spotifyAlbumId: albumId },
  804. this
  805. );
  806. const youtubePlaylistIds = Array.from(
  807. new Set(
  808. wikiDataResponse.results.bindings
  809. .filter(binding => !!binding.YouTube_playlist_ID)
  810. .map(binding => binding.YouTube_playlist_ID.value)
  811. )
  812. );
  813. return youtubePlaylistIds;
  814. }
  815. /**
  816. * Tries to get alternative track sources for a list of Spotify track media sources
  817. *
  818. * @param {object} payload - object that contains the payload
  819. * @param {string} payload.mediaSources - the Spotify media sources to try and get alternative track sources for
  820. * @param {boolean} payload.collectAlternativeMediaSourcesOrigins - whether to collect the origin of any alternative track sources found
  821. * @returns {Promise} - returns promise (reject, resolve)
  822. */
  823. async GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACKS(payload) {
  824. const { mediaSources, collectAlternativeMediaSourcesOrigins } = payload;
  825. await async.eachLimit(mediaSources, 1, async mediaSource => {
  826. try {
  827. const result = await SpotifyModule.runJob(
  828. "GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACK",
  829. { mediaSource, collectAlternativeMediaSourcesOrigins },
  830. this
  831. );
  832. this.publishProgress({
  833. status: "working",
  834. message: `Got alternative media for ${mediaSource}`,
  835. data: {
  836. mediaSource,
  837. status: "success",
  838. result
  839. }
  840. });
  841. } catch (err) {
  842. this.publishProgress({
  843. status: "working",
  844. message: `Failed to get alternative media for ${mediaSource}`,
  845. data: {
  846. mediaSource,
  847. status: "error"
  848. }
  849. });
  850. }
  851. });
  852. this.publishProgress({
  853. status: "finished",
  854. message: `Finished getting alternative media`
  855. });
  856. }
  857. /**
  858. * Tries to get alternative track sources for a Spotify track media source
  859. *
  860. * @param {object} payload - object that contains the payload
  861. * @param {string} payload.mediaSource - the Spotify media source to try and get alternative track sources for
  862. * @param {boolean} payload.collectAlternativeMediaSourcesOrigins - whether to collect the origin of any alternative track sources found
  863. * @returns {Promise} - returns promise (reject, resolve)
  864. */
  865. async GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACK(payload) {
  866. const { mediaSource, collectAlternativeMediaSourcesOrigins } = payload;
  867. if (!mediaSource || !mediaSource.startsWith("spotify:"))
  868. throw new Error("Media source provided is not a valid Spotify media source.");
  869. const spotifyTrackId = mediaSource.split(":")[1];
  870. const { track: spotifyTrack } = await SpotifyModule.runJob(
  871. "GET_TRACK",
  872. {
  873. identifier: spotifyTrackId,
  874. createMissing: true
  875. },
  876. this
  877. );
  878. const ISRC = spotifyTrack.externalIds.isrc;
  879. if (!ISRC) throw new Error(`ISRC not found for Spotify track ${mediaSource}.`);
  880. const mediaSources = new Set();
  881. const mediaSourcesOrigins = {};
  882. const jobsToRun = [];
  883. try {
  884. const ISRCApiResponse = await MusicBrainzModule.runJob(
  885. "API_CALL",
  886. {
  887. url: `https://musicbrainz.org/ws/2/isrc/${ISRC}`,
  888. params: {
  889. fmt: "json",
  890. inc: "url-rels+work-rels"
  891. }
  892. },
  893. this
  894. );
  895. ISRCApiResponse.recordings.forEach(recording => {
  896. recording.relations.forEach(relation => {
  897. if (relation["target-type"] === "url" && relation.url) {
  898. // relation["type-id"] === "7e41ef12-a124-4324-afdb-fdbae687a89c"
  899. const { resource } = relation.url;
  900. if (config.get("experimental.soundcloud") && resource.indexOf("soundcloud.com") !== -1) {
  901. const promise = new Promise(resolve => {
  902. SoundcloudModule.runJob(
  903. "GET_TRACK_FROM_URL",
  904. { identifier: resource, createMissing: true },
  905. this
  906. )
  907. .then(response => {
  908. const { trackId } = response.track;
  909. const mediaSource = `soundcloud:${trackId}`;
  910. mediaSources.add(mediaSource);
  911. if (collectAlternativeMediaSourcesOrigins) {
  912. const mediaSourceOrigins = [
  913. `Spotify track ${spotifyTrackId}`,
  914. `ISRC ${ISRC}`,
  915. `MusicBrainz recordings`,
  916. `MusicBrainz recording ${recording.id}`,
  917. `MusicBrainz relations`,
  918. `MusicBrainz relation target-type url`,
  919. `MusicBrainz relation resource ${resource}`,
  920. `SoundCloud ID ${trackId}`
  921. ];
  922. if (!mediaSourcesOrigins[mediaSource])
  923. mediaSourcesOrigins[mediaSource] = [];
  924. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  925. }
  926. resolve();
  927. })
  928. .catch(() => {
  929. resolve();
  930. });
  931. });
  932. jobsToRun.push(promise);
  933. return;
  934. }
  935. if (resource.indexOf("youtube.com") !== -1 || resource.indexOf("youtu.be") !== -1) {
  936. const match = youtubeVideoUrlRegex.exec(resource);
  937. if (!match) throw new Error(`Unable to parse YouTube resource ${resource}.`);
  938. const { youtubeId } = match.groups;
  939. if (!youtubeId) throw new Error(`Unable to parse YouTube resource ${resource}.`);
  940. const mediaSource = `youtube:${youtubeId}`;
  941. mediaSources.add(mediaSource);
  942. if (collectAlternativeMediaSourcesOrigins) {
  943. const mediaSourceOrigins = [
  944. `Spotify track ${spotifyTrackId}`,
  945. `ISRC ${ISRC}`,
  946. `MusicBrainz recordings`,
  947. `MusicBrainz recording ${recording.id}`,
  948. `MusicBrainz relations`,
  949. `MusicBrainz relation target-type url`,
  950. `MusicBrainz relation resource ${resource}`,
  951. `YouTube ID ${youtubeId}`
  952. ];
  953. if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  954. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  955. }
  956. return;
  957. }
  958. return;
  959. }
  960. if (relation["target-type"] === "work") {
  961. const promise = new Promise(resolve => {
  962. WikiDataModule.runJob(
  963. "API_GET_DATA_FROM_MUSICBRAINZ_WORK",
  964. { workId: relation.work.id },
  965. this
  966. )
  967. .then(resultBody => {
  968. const youtubeIds = Array.from(
  969. new Set(
  970. resultBody.results.bindings
  971. .filter(binding => !!binding.YouTube_video_ID)
  972. .map(binding => binding.YouTube_video_ID.value)
  973. )
  974. );
  975. // const soundcloudIds = Array.from(
  976. // new Set(
  977. // resultBody.results.bindings
  978. // .filter(binding => !!binding["SoundCloud_track_ID"])
  979. // .map(binding => binding["SoundCloud_track_ID"].value)
  980. // )
  981. // );
  982. const musicVideoEntityUrls = Array.from(
  983. new Set(
  984. resultBody.results.bindings
  985. .filter(binding => !!binding.Music_video_entity_URL)
  986. .map(binding => binding.Music_video_entity_URL.value)
  987. )
  988. );
  989. youtubeIds.forEach(youtubeId => {
  990. const mediaSource = `youtube:${youtubeId}`;
  991. mediaSources.add(mediaSource);
  992. if (collectAlternativeMediaSourcesOrigins) {
  993. const mediaSourceOrigins = [
  994. `Spotify track ${spotifyTrackId}`,
  995. `ISRC ${ISRC}`,
  996. `MusicBrainz recordings`,
  997. `MusicBrainz recording ${recording.id}`,
  998. `MusicBrainz relations`,
  999. `MusicBrainz relation target-type work`,
  1000. `MusicBrainz relation work id ${relation.work.id}`,
  1001. `WikiData select from MusicBrainz work id ${relation.work.id}`,
  1002. `YouTube ID ${youtubeId}`
  1003. ];
  1004. if (!mediaSourcesOrigins[mediaSource])
  1005. mediaSourcesOrigins[mediaSource] = [];
  1006. mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  1007. }
  1008. });
  1009. // soundcloudIds.forEach(soundcloudId => {
  1010. // const mediaSource = `soundcloud:${soundcloudId}`;
  1011. // mediaSources.add(mediaSource);
  1012. // if (collectAlternativeMediaSourcesOrigins) {
  1013. // const mediaSourceOrigins = [
  1014. // `Spotify track ${spotifyTrackId}`,
  1015. // `ISRC ${ISRC}`,
  1016. // `MusicBrainz recordings`,
  1017. // `MusicBrainz recording ${recording.id}`,
  1018. // `MusicBrainz relations`,
  1019. // `MusicBrainz relation target-type work`,
  1020. // `MusicBrainz relation work id ${relation.work.id}`,
  1021. // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  1022. // `SoundCloud ID ${soundcloudId}`
  1023. // ];
  1024. // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  1025. // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  1026. // }
  1027. // });
  1028. const promisesToRun2 = [];
  1029. musicVideoEntityUrls.forEach(musicVideoEntityUrl => {
  1030. promisesToRun2.push(
  1031. new Promise(resolve => {
  1032. WikiDataModule.runJob(
  1033. "API_GET_DATA_FROM_ENTITY_URL",
  1034. { entityUrl: musicVideoEntityUrl },
  1035. this
  1036. ).then(resultBody => {
  1037. const youtubeIds = Array.from(
  1038. new Set(
  1039. resultBody.results.bindings
  1040. .filter(binding => !!binding.YouTube_video_ID)
  1041. .map(binding => binding.YouTube_video_ID.value)
  1042. )
  1043. );
  1044. // const soundcloudIds = Array.from(
  1045. // new Set(
  1046. // resultBody.results.bindings
  1047. // .filter(binding => !!binding["SoundCloud_track_ID"])
  1048. // .map(binding => binding["SoundCloud_track_ID"].value)
  1049. // )
  1050. // );
  1051. youtubeIds.forEach(youtubeId => {
  1052. const mediaSource = `youtube:${youtubeId}`;
  1053. mediaSources.add(mediaSource);
  1054. // if (collectAlternativeMediaSourcesOrigins) {
  1055. // const mediaSourceOrigins = [
  1056. // `Spotify track ${spotifyTrackId}`,
  1057. // `ISRC ${ISRC}`,
  1058. // `MusicBrainz recordings`,
  1059. // `MusicBrainz recording ${recording.id}`,
  1060. // `MusicBrainz relations`,
  1061. // `MusicBrainz relation target-type work`,
  1062. // `MusicBrainz relation work id ${relation.work.id}`,
  1063. // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  1064. // `YouTube ID ${youtubeId}`
  1065. // ];
  1066. // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  1067. // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  1068. // }
  1069. });
  1070. // soundcloudIds.forEach(soundcloudId => {
  1071. // const mediaSource = `soundcloud:${soundcloudId}`;
  1072. // mediaSources.add(mediaSource);
  1073. // // if (collectAlternativeMediaSourcesOrigins) {
  1074. // // const mediaSourceOrigins = [
  1075. // // `Spotify track ${spotifyTrackId}`,
  1076. // // `ISRC ${ISRC}`,
  1077. // // `MusicBrainz recordings`,
  1078. // // `MusicBrainz recording ${recording.id}`,
  1079. // // `MusicBrainz relations`,
  1080. // // `MusicBrainz relation target-type work`,
  1081. // // `MusicBrainz relation work id ${relation.work.id}`,
  1082. // // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  1083. // // `SoundCloud ID ${soundcloudId}`
  1084. // // ];
  1085. // // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  1086. // // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  1087. // // }
  1088. // });
  1089. resolve();
  1090. });
  1091. })
  1092. );
  1093. });
  1094. Promise.allSettled(promisesToRun2).then(resolve);
  1095. })
  1096. .catch(err => {
  1097. console.log(err);
  1098. resolve();
  1099. });
  1100. });
  1101. jobsToRun.push(promise);
  1102. // WikiDataModule.runJob("API_GET_DATA_FROM_MUSICBRAINZ_WORK", { workId: relation.work.id }, this));
  1103. }
  1104. });
  1105. });
  1106. } catch (err) {
  1107. console.log("Error during initial ISRC getting/parsing", err);
  1108. }
  1109. try {
  1110. const RecordingApiResponse = await MusicBrainzModule.runJob(
  1111. "API_CALL",
  1112. {
  1113. url: `https://musicbrainz.org/ws/2/recording/`,
  1114. params: {
  1115. fmt: "json",
  1116. query: `isrc:${ISRC}`
  1117. }
  1118. },
  1119. this
  1120. );
  1121. const releaseIds = new Set();
  1122. const releaseGroupIds = new Set();
  1123. RecordingApiResponse.recordings.forEach(recording => {
  1124. // const recordingId = recording.id;
  1125. // console.log("Recording:", recording.id);
  1126. recording.releases.forEach(release => {
  1127. const releaseId = release.id;
  1128. // console.log("Release:", releaseId);
  1129. const releaseGroupId = release["release-group"].id;
  1130. // console.log("Release group:", release["release-group"]);
  1131. // console.log("Release group id:", release["release-group"].id);
  1132. // console.log("Release group type id:", release["release-group"]["type-id"]);
  1133. // console.log("Release group primary type id:", release["release-group"]["primary-type-id"]);
  1134. // console.log("Release group primary type:", release["release-group"]["primary-type"]);
  1135. // d6038452-8ee0-3f68-affc-2de9a1ede0b9 = single
  1136. // 6d0c5bf6-7a33-3420-a519-44fc63eedebf = EP
  1137. if (
  1138. release["release-group"]["type-id"] === "d6038452-8ee0-3f68-affc-2de9a1ede0b9" ||
  1139. release["release-group"]["type-id"] === "6d0c5bf6-7a33-3420-a519-44fc63eedebf"
  1140. ) {
  1141. releaseIds.add(releaseId);
  1142. releaseGroupIds.add(releaseGroupId);
  1143. }
  1144. });
  1145. });
  1146. Array.from(releaseGroupIds).forEach(releaseGroupId => {
  1147. const promise = new Promise(resolve => {
  1148. WikiDataModule.runJob("API_GET_DATA_FROM_MUSICBRAINZ_RELEASE_GROUP", { releaseGroupId }, this)
  1149. .then(resultBody => {
  1150. const youtubeIds = Array.from(
  1151. new Set(
  1152. resultBody.results.bindings
  1153. .filter(binding => !!binding.YouTube_video_ID)
  1154. .map(binding => binding.YouTube_video_ID.value)
  1155. )
  1156. );
  1157. // const soundcloudIds = Array.from(
  1158. // new Set(
  1159. // resultBody.results.bindings
  1160. // .filter(binding => !!binding["SoundCloud_track_ID"])
  1161. // .map(binding => binding["SoundCloud_track_ID"].value)
  1162. // )
  1163. // );
  1164. const musicVideoEntityUrls = Array.from(
  1165. new Set(
  1166. resultBody.results.bindings
  1167. .filter(binding => !!binding.Music_video_entity_URL)
  1168. .map(binding => binding.Music_video_entity_URL.value)
  1169. )
  1170. );
  1171. youtubeIds.forEach(youtubeId => {
  1172. const mediaSource = `youtube:${youtubeId}`;
  1173. mediaSources.add(mediaSource);
  1174. // if (collectAlternativeMediaSourcesOrigins) {
  1175. // const mediaSourceOrigins = [
  1176. // `Spotify track ${spotifyTrackId}`,
  1177. // `ISRC ${ISRC}`,
  1178. // `MusicBrainz recordings`,
  1179. // `MusicBrainz recording ${recording.id}`,
  1180. // `MusicBrainz relations`,
  1181. // `MusicBrainz relation target-type work`,
  1182. // `MusicBrainz relation work id ${relation.work.id}`,
  1183. // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  1184. // `YouTube ID ${youtubeId}`
  1185. // ];
  1186. // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  1187. // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  1188. // }
  1189. });
  1190. // soundcloudIds.forEach(soundcloudId => {
  1191. // const mediaSource = `soundcloud:${soundcloudId}`;
  1192. // mediaSources.add(mediaSource);
  1193. // // if (collectAlternativeMediaSourcesOrigins) {
  1194. // // const mediaSourceOrigins = [
  1195. // // `Spotify track ${spotifyTrackId}`,
  1196. // // `ISRC ${ISRC}`,
  1197. // // `MusicBrainz recordings`,
  1198. // // `MusicBrainz recording ${recording.id}`,
  1199. // // `MusicBrainz relations`,
  1200. // // `MusicBrainz relation target-type work`,
  1201. // // `MusicBrainz relation work id ${relation.work.id}`,
  1202. // // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  1203. // // `SoundCloud ID ${soundcloudId}`
  1204. // // ];
  1205. // // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  1206. // // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  1207. // // }
  1208. // });
  1209. const promisesToRun2 = [];
  1210. musicVideoEntityUrls.forEach(musicVideoEntityUrl => {
  1211. promisesToRun2.push(
  1212. new Promise(resolve => {
  1213. WikiDataModule.runJob(
  1214. "API_GET_DATA_FROM_ENTITY_URL",
  1215. { entityUrl: musicVideoEntityUrl },
  1216. this
  1217. ).then(resultBody => {
  1218. const youtubeIds = Array.from(
  1219. new Set(
  1220. resultBody.results.bindings
  1221. .filter(binding => !!binding.YouTube_video_ID)
  1222. .map(binding => binding.YouTube_video_ID.value)
  1223. )
  1224. );
  1225. // const soundcloudIds = Array.from(
  1226. // new Set(
  1227. // resultBody.results.bindings
  1228. // .filter(binding => !!binding["SoundCloud_track_ID"])
  1229. // .map(binding => binding["SoundCloud_track_ID"].value)
  1230. // )
  1231. // );
  1232. youtubeIds.forEach(youtubeId => {
  1233. const mediaSource = `youtube:${youtubeId}`;
  1234. mediaSources.add(mediaSource);
  1235. // if (collectAlternativeMediaSourcesOrigins) {
  1236. // const mediaSourceOrigins = [
  1237. // `Spotify track ${spotifyTrackId}`,
  1238. // `ISRC ${ISRC}`,
  1239. // `MusicBrainz recordings`,
  1240. // `MusicBrainz recording ${recording.id}`,
  1241. // `MusicBrainz relations`,
  1242. // `MusicBrainz relation target-type work`,
  1243. // `MusicBrainz relation work id ${relation.work.id}`,
  1244. // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  1245. // `YouTube ID ${youtubeId}`
  1246. // ];
  1247. // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  1248. // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  1249. // }
  1250. });
  1251. // soundcloudIds.forEach(soundcloudId => {
  1252. // const mediaSource = `soundcloud:${soundcloudId}`;
  1253. // mediaSources.add(mediaSource);
  1254. // // if (collectAlternativeMediaSourcesOrigins) {
  1255. // // const mediaSourceOrigins = [
  1256. // // `Spotify track ${spotifyTrackId}`,
  1257. // // `ISRC ${ISRC}`,
  1258. // // `MusicBrainz recordings`,
  1259. // // `MusicBrainz recording ${recording.id}`,
  1260. // // `MusicBrainz relations`,
  1261. // // `MusicBrainz relation target-type work`,
  1262. // // `MusicBrainz relation work id ${relation.work.id}`,
  1263. // // `WikiData select from MusicBrainz work id ${relation.work.id}`,
  1264. // // `SoundCloud ID ${soundcloudId}`
  1265. // // ];
  1266. // // if (!mediaSourcesOrigins[mediaSource]) mediaSourcesOrigins[mediaSource] = [];
  1267. // // mediaSourcesOrigins[mediaSource].push(mediaSourceOrigins);
  1268. // // }
  1269. // });
  1270. resolve();
  1271. });
  1272. })
  1273. );
  1274. });
  1275. Promise.allSettled(promisesToRun2).then(resolve);
  1276. })
  1277. .catch(err => {
  1278. console.log(err);
  1279. resolve();
  1280. });
  1281. });
  1282. jobsToRun.push(promise);
  1283. });
  1284. } catch (err) {
  1285. console.log("Error during getting releases from ISRC", err);
  1286. }
  1287. await Promise.allSettled(jobsToRun);
  1288. return {
  1289. mediaSources: Array.from(mediaSources),
  1290. mediaSourcesOrigins
  1291. };
  1292. }
  1293. }
  1294. export default new _SpotifyModule();