apis.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. import config from "config";
  2. import async from "async";
  3. import axios from "axios";
  4. import isLoginRequired from "../hooks/loginRequired";
  5. import { hasPermission, useHasPermission } from "../hooks/hasPermission";
  6. // eslint-disable-next-line
  7. import moduleManager from "../../index";
  8. const UtilsModule = moduleManager.modules.utils;
  9. const WSModule = moduleManager.modules.ws;
  10. const YouTubeModule = moduleManager.modules.youtube;
  11. const SpotifyModule = moduleManager.modules.spotify;
  12. const CacheModule = moduleManager.modules.cache;
  13. export default {
  14. /**
  15. * Fetches a list of songs from Youtube's API
  16. *
  17. * @param {object} session - user session
  18. * @param {string} query - the query we'll pass to youtubes api
  19. * @param {Function} cb - callback
  20. * @returns {{status: string, data: object}} - returns an object
  21. */
  22. searchYoutube: isLoginRequired(function searchYoutube(session, query, cb) {
  23. return YouTubeModule.runJob("SEARCH", { query }, this)
  24. .then(data => {
  25. this.log("SUCCESS", "APIS_SEARCH_YOUTUBE", `Searching YouTube successful with query "${query}".`);
  26. return cb({ status: "success", data });
  27. })
  28. .catch(async err => {
  29. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  30. this.log("ERROR", "APIS_SEARCH_YOUTUBE", `Searching youtube failed with query "${query}". "${err}"`);
  31. return cb({ status: "error", message: err });
  32. });
  33. }),
  34. /**
  35. * Fetches a specific page of search results from Youtube's API
  36. *
  37. * @param {object} session - user session
  38. * @param {string} query - the query we'll pass to youtubes api
  39. * @param {string} pageToken - identifies a specific page in the result set that should be retrieved
  40. * @param {Function} cb - callback
  41. * @returns {{status: string, data: object}} - returns an object
  42. */
  43. searchYoutubeForPage: isLoginRequired(function searchYoutubeForPage(session, query, pageToken, cb) {
  44. return YouTubeModule.runJob("SEARCH", { query, pageToken }, this)
  45. .then(data => {
  46. this.log(
  47. "SUCCESS",
  48. "APIS_SEARCH_YOUTUBE_FOR_PAGE",
  49. `Searching YouTube successful with query "${query}".`
  50. );
  51. return cb({ status: "success", data });
  52. })
  53. .catch(async err => {
  54. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  55. this.log(
  56. "ERROR",
  57. "APIS_SEARCH_YOUTUBE_FOR_PAGE",
  58. `Searching youtube failed with query "${query}". "${err}"`
  59. );
  60. return cb({ status: "error", message: err });
  61. });
  62. }),
  63. /**
  64. * Gets Discogs data
  65. *
  66. * @param session
  67. * @param query - the query
  68. * @param {Function} cb
  69. */
  70. searchDiscogs: useHasPermission("apis.searchDiscogs", function searchDiscogs(session, query, page, cb) {
  71. async.waterfall(
  72. [
  73. next => {
  74. const options = {
  75. params: { q: query, per_page: 20, page },
  76. headers: {
  77. "User-Agent": "Request",
  78. Authorization: `Discogs key=${config.get("apis.discogs.client")}, secret=${config.get(
  79. "apis.discogs.secret"
  80. )}`
  81. }
  82. };
  83. axios
  84. .get("https://api.discogs.com/database/search", options)
  85. .then(res => next(null, res.data))
  86. .catch(err => next(err));
  87. }
  88. ],
  89. async (err, body) => {
  90. if (err) {
  91. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  92. this.log(
  93. "ERROR",
  94. "APIS_SEARCH_DISCOGS",
  95. `Searching discogs failed with query "${query}". "${err}"`
  96. );
  97. return cb({ status: "error", message: err });
  98. }
  99. this.log(
  100. "SUCCESS",
  101. "APIS_SEARCH_DISCOGS",
  102. `User "${session.userId}" searched Discogs succesfully for query "${query}".`
  103. );
  104. return cb({
  105. status: "success",
  106. data: {
  107. results: body.results,
  108. pages: body.pagination.pages
  109. }
  110. });
  111. }
  112. );
  113. }),
  114. // /**
  115. // *
  116. // *
  117. // * @param session
  118. // * @param ISRC - the ISRC
  119. // * @param {Function} cb
  120. // */
  121. // searchMusicBrainzISRC: useHasPermission("admin.view.spotify", function searchMusicBrainzISRC(session, ISRC, cb) {
  122. // async.waterfall(
  123. // [
  124. // next => {
  125. // if (!ISRC) {
  126. // next("Invalid ISRC provided.");
  127. // return;
  128. // }
  129. // CacheModule.runJob("HGET", { table: "musicbrainz-isrc-2", key: ISRC })
  130. // .then(response => {
  131. // if (response) next(null, response);
  132. // else next(null, null);
  133. // })
  134. // .catch(err => {
  135. // next(err);
  136. // });
  137. // },
  138. // (body, next) => {
  139. // if (body) {
  140. // next(null, body);
  141. // return;
  142. // }
  143. // const options = {
  144. // params: { fmt: "json", inc: "url-rels+work-rels" },
  145. // headers: {
  146. // "User-Agent": "Musare/3.9.0-fork ( https://git.kvos.dev/kris/MusareFork )" // TODO set this in accordance to https://musicbrainz.org/doc/MusicBrainz_API/Rate_Limiting
  147. // }
  148. // };
  149. // console.log("KRIS101", options, `https://musicbrainz.org/ws/2/isrc/${ISRC}`);
  150. // axios
  151. // .get(`https://musicbrainz.org/ws/2/isrc/${ISRC}`, options)
  152. // .then(res => next(null, res.data))
  153. // .catch(err => next(err));
  154. // },
  155. // (body, next) => {
  156. // console.log("KRIS222", body);
  157. // CacheModule.runJob("HSET", { table: "musicbrainz-isrc-2", key: ISRC, value: body })
  158. // .then(() => {})
  159. // .catch(() => {});
  160. // next(null, body);
  161. // },
  162. // (body, next) => {
  163. // const response = {};
  164. // const recordingUrls = Array.from(
  165. // new Set(
  166. // body.recordings
  167. // .map(recording =>
  168. // recording.relations
  169. // .filter(
  170. // relation =>
  171. // relation["target-type"] === "url" &&
  172. // relation.url &&
  173. // // relation["type-id"] === "7e41ef12-a124-4324-afdb-fdbae687a89c" &&
  174. // (relation.url.resource.indexOf("youtu.be") !== -1 ||
  175. // relation.url.resource.indexOf("youtube.com") !== -1 ||
  176. // relation.url.resource.indexOf("soundcloud.com") !== -1)
  177. // )
  178. // .map(relation => relation.url.resource)
  179. // )
  180. // .flat()
  181. // )
  182. // );
  183. // const workIds = Array.from(
  184. // new Set(
  185. // body.recordings
  186. // .map(recording =>
  187. // recording.relations
  188. // .filter(relation => relation["target-type"] === "work" && relation.work)
  189. // .map(relation => relation.work.id)
  190. // )
  191. // .flat()
  192. // )
  193. // );
  194. // response.recordingUrls = recordingUrls;
  195. // response.workIds = workIds;
  196. // response.raw = body;
  197. // next(null, response);
  198. // }
  199. // ],
  200. // async (err, response) => {
  201. // if (err && err !== true) {
  202. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  203. // this.log(
  204. // "ERROR",
  205. // "APIS_SEARCH_MUSICBRAINZ_ISRC",
  206. // `Searching MusicBrainz ISRC failed with ISRC "${ISRC}". "${err}"`
  207. // );
  208. // return cb({ status: "error", message: err });
  209. // }
  210. // this.log(
  211. // "SUCCESS",
  212. // "APIS_SEARCH_MUSICBRAINZ_ISRC",
  213. // `User "${session.userId}" searched MusicBrainz ISRC succesfully for ISRC "${ISRC}".`
  214. // );
  215. // return cb({
  216. // status: "success",
  217. // data: {
  218. // response
  219. // }
  220. // });
  221. // }
  222. // );
  223. // }),
  224. // /**
  225. // *
  226. // *
  227. // * @param session
  228. // * @param trackId - the trackId
  229. // * @param {Function} cb
  230. // */
  231. // searchWikidataBySpotifyTrackId: useHasPermission(
  232. // "admin.view.spotify",
  233. // function searchWikidataBySpotifyTrackId(session, trackId, cb) {
  234. // async.waterfall(
  235. // [
  236. // next => {
  237. // if (!trackId) {
  238. // next("Invalid trackId provided.");
  239. // return;
  240. // }
  241. // CacheModule.runJob("HGET", { table: "wikidata-spotify-track", key: trackId })
  242. // .then(response => {
  243. // if (response) next(null, response);
  244. // else next(null, null);
  245. // })
  246. // .catch(err => {
  247. // console.log("WOW", err);
  248. // next(err);
  249. // });
  250. // },
  251. // (body, next) => {
  252. // if (body) {
  253. // next(null, body);
  254. // return;
  255. // }
  256. // // const options = {
  257. // // params: { fmt: "json", inc: "url-rels" },
  258. // // headers: {
  259. // // "User-Agent": "Musare/3.9.0-fork ( https://git.kvos.dev/kris/MusareFork )" // TODO set this in accordance to https://musicbrainz.org/doc/MusicBrainz_API/Rate_Limiting
  260. // // }
  261. // // };
  262. // // axios
  263. // // .get(`https://musicbrainz.org/ws/2/isrc/${ISRC}`, options)
  264. // // .then(res => next(null, res.data))
  265. // // .catch(err => next(err));
  266. // },
  267. // (body, next) => {
  268. // CacheModule.runJob("HSET", { table: "musicbrainz-isrc", key: ISRC, value: body })
  269. // .then(() => {})
  270. // .catch(() => {});
  271. // next(null, body);
  272. // },
  273. // (body, next) => {
  274. // const response = {};
  275. // const recordingUrls = Array.from(
  276. // new Set(
  277. // body.recordings
  278. // .map(recording =>
  279. // recording.relations
  280. // .filter(
  281. // relation =>
  282. // relation["target-type"] === "url" &&
  283. // relation.url &&
  284. // // relation["type-id"] === "7e41ef12-a124-4324-afdb-fdbae687a89c" &&
  285. // (relation.url.resource.indexOf("youtu.be") !== -1 ||
  286. // relation.url.resource.indexOf("youtube.com") !== -1 ||
  287. // relation.url.resource.indexOf("soundcloud.com") !== -1)
  288. // )
  289. // .map(relation => relation.url.resource)
  290. // )
  291. // .flat()
  292. // )
  293. // );
  294. // response.recordingUrls = recordingUrls;
  295. // response.raw = body;
  296. // next(null, response);
  297. // }
  298. // ],
  299. // async (err, response) => {
  300. // if (err && err !== true) {
  301. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  302. // this.log(
  303. // "ERROR",
  304. // "APIS_SEARCH_TODO",
  305. // `Searching MusicBrainz ISRC failed with ISRC "${ISRC}". "${err}"`
  306. // );
  307. // return cb({ status: "error", message: err });
  308. // }
  309. // this.log(
  310. // "SUCCESS",
  311. // "APIS_SEARCH_TODO",
  312. // `User "${session.userId}" searched MusicBrainz ISRC succesfully for ISRC "${ISRC}".`
  313. // );
  314. // return cb({
  315. // status: "success",
  316. // data: {
  317. // response
  318. // }
  319. // });
  320. // }
  321. // );
  322. // }
  323. // ),
  324. // /**
  325. // *
  326. // *
  327. // * @param session
  328. // * @param trackId - the trackId
  329. // * @param {Function} cb
  330. // */
  331. // searchWikidataByMusicBrainzWorkId: useHasPermission(
  332. // "admin.view.spotify",
  333. // function searchWikidataByMusicBrainzWorkId(session, workId, cb) {
  334. // async.waterfall(
  335. // [
  336. // next => {
  337. // if (!workId) {
  338. // next("Invalid workId provided.");
  339. // return;
  340. // }
  341. // CacheModule.runJob("HGET", { table: "wikidata-musicbrainz-work", key: workId })
  342. // .then(response => {
  343. // if (response) next(null, response);
  344. // else next(null, null);
  345. // })
  346. // .catch(err => {
  347. // next(err);
  348. // });
  349. // },
  350. // (body, next) => {
  351. // if (body) {
  352. // next(null, body);
  353. // return;
  354. // }
  355. // const endpointUrl = "https://query.wikidata.org/sparql";
  356. // const sparqlQuery = `SELECT DISTINCT ?item ?itemLabel ?YouTube_video_ID WHERE {
  357. // SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
  358. // {
  359. // SELECT DISTINCT ?item WHERE {
  360. // ?item p:P435 ?statement0.
  361. // ?statement0 ps:P435 "${workId}".
  362. // }
  363. // LIMIT 100
  364. // }
  365. // OPTIONAL { ?item wdt:P1651 ?YouTube_video_ID. }
  366. // }`;
  367. // // OPTIONAL { ?item wdt:P3040 ?SoundCloud_track_ID. }
  368. // const options = {
  369. // params: { query: sparqlQuery },
  370. // headers: {
  371. // Accept: "application/sparql-results+json"
  372. // }
  373. // };
  374. // axios
  375. // .get(endpointUrl, options)
  376. // .then(res => next(null, res.data))
  377. // .catch(err => next(err));
  378. // },
  379. // (body, next) => {
  380. // CacheModule.runJob("HSET", { table: "wikidata-musicbrainz-work", key: workId, value: body })
  381. // .then(() => {})
  382. // .catch(() => {});
  383. // next(null, body);
  384. // },
  385. // (body, next) => {
  386. // const response = {};
  387. // const youtubeIds = Array.from(
  388. // new Set(
  389. // body.results.bindings
  390. // .filter(binding => !!binding.YouTube_video_ID)
  391. // .map(binding => binding.YouTube_video_ID.value)
  392. // )
  393. // );
  394. // // const soundcloudIds = Array.from(new Set(body.results.bindings.filter(binding => !!binding["SoundCloud_track_ID"]).map(binding => binding["SoundCloud_track_ID"].value)))
  395. // response.youtubeIds = youtubeIds;
  396. // response.raw = body;
  397. // next(null, response);
  398. // }
  399. // ],
  400. // async (err, response) => {
  401. // if (err && err !== true) {
  402. // err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  403. // this.log(
  404. // "ERROR",
  405. // "APIS_SEARCH_TODO",
  406. // `Searching MusicBrainz ISRC failed with ISRC "${workId}". "${err}"`
  407. // );
  408. // return cb({ status: "error", message: err });
  409. // }
  410. // this.log(
  411. // "SUCCESS",
  412. // "APIS_SEARCH_TODO",
  413. // `User "${session.userId}" searched MusicBrainz ISRC succesfully for ISRC "${workId}".`
  414. // );
  415. // return cb({
  416. // status: "success",
  417. // data: {
  418. // response
  419. // }
  420. // });
  421. // }
  422. // );
  423. // }
  424. // ),
  425. /**
  426. *
  427. *
  428. * @param session
  429. * @param trackId - the trackId
  430. * @param {Function} cb
  431. */
  432. getAlternativeMediaSourcesForTracks: useHasPermission(
  433. "admin.view.spotify",
  434. function getAlternativeMediaSourcesForTracks(session, mediaSources, cb) {
  435. async.waterfall(
  436. [
  437. next => {
  438. if (!mediaSources) {
  439. next("Invalid mediaSources provided.");
  440. return;
  441. }
  442. next();
  443. },
  444. async () => {
  445. this.keepLongJob();
  446. this.publishProgress({
  447. status: "started",
  448. title: "Getting alternative media sources for Spotify tracks",
  449. message: "Starting up",
  450. id: this.toString()
  451. });
  452. console.log("KRIS@4", this.toString());
  453. // await CacheModule.runJob(
  454. // "RPUSH",
  455. // { key: `longJobs.${session.userId}`, value: this.toString() },
  456. // this
  457. // );
  458. SpotifyModule.runJob("GET_ALTERNATIVE_MEDIA_SOURCES_FOR_TRACKS", { mediaSources }, this);
  459. }
  460. ],
  461. async err => {
  462. if (err) {
  463. err = await UtilsModule.runJob("GET_ERROR", { error: err }, this);
  464. this.log(
  465. "ERROR",
  466. "APIS_GET_ALTERNATIVE_SOURCES",
  467. `Getting alternative sources failed for "${mediaSources.join(", ")}". "${err}"`
  468. );
  469. return cb({ status: "error", message: err });
  470. }
  471. this.log(
  472. "SUCCESS",
  473. "APIS_GET_ALTERNATIVE_SOURCES",
  474. `User "${session.userId}" started getting alternatives for "${mediaSources.join(", ")}".`
  475. );
  476. return cb({
  477. status: "success"
  478. });
  479. }
  480. );
  481. }
  482. ),
  483. /**
  484. * Joins a room
  485. *
  486. * @param {object} session - user session
  487. * @param {string} room - the room to join
  488. * @param {Function} cb - callback
  489. */
  490. joinRoom(session, room, cb) {
  491. const roomName = room.split(".")[0];
  492. // const roomId = room.split(".")[1];
  493. const rooms = {
  494. home: null,
  495. news: null,
  496. profile: null,
  497. "view-youtube-video": null,
  498. "manage-station": null,
  499. // "manage-station": "stations.view",
  500. "edit-song": "songs.update",
  501. "edit-songs": "songs.update",
  502. "import-album": "songs.update",
  503. // "edit-playlist": "playlists.update",
  504. "view-report": "reports.get",
  505. "edit-user": "users.update",
  506. "view-api-request": "youtube.getApiRequest",
  507. "view-punishment": "punishments.get"
  508. };
  509. const join = (status, error) => {
  510. if (status === "success")
  511. WSModule.runJob("SOCKET_JOIN_ROOM", {
  512. socketId: session.socketId,
  513. room
  514. })
  515. .then(() => cb({ status: "success", message: "Successfully joined room." }))
  516. .catch(err => join("error", err.message));
  517. else {
  518. this.log("ERROR", `Joining room failed: ${error}`);
  519. cb({ status: "error", message: error });
  520. }
  521. };
  522. if (rooms[roomName] === null) join("success");
  523. else if (rooms[roomName])
  524. hasPermission(rooms[roomName], session)
  525. .then(() => join("success"))
  526. .catch(err => join("error", err));
  527. else join("error", "Room not found");
  528. },
  529. /**
  530. * Leaves a room
  531. *
  532. * @param {object} session - user session
  533. * @param {string} room - the room to leave
  534. * @param {Function} cb - callback
  535. */
  536. leaveRoom(session, room, cb) {
  537. if (
  538. room === "home" ||
  539. room.startsWith("profile.") ||
  540. room.startsWith("manage-station.") ||
  541. room.startsWith("edit-song.") ||
  542. room.startsWith("view-report.") ||
  543. room === "import-album" ||
  544. room === "edit-songs"
  545. ) {
  546. WSModule.runJob("SOCKET_LEAVE_ROOM", {
  547. socketId: session.socketId,
  548. room
  549. })
  550. .then(() => {})
  551. .catch(err => {
  552. this.log("ERROR", `Leaving room failed: ${err.message}`);
  553. });
  554. }
  555. cb({ status: "success", message: "Successfully left room." });
  556. },
  557. /**
  558. * Joins an admin room
  559. *
  560. * @param {object} session - user session
  561. * @param {string} page - the admin room to join
  562. * @param {Function} cb - callback
  563. */
  564. joinAdminRoom(session, page, cb) {
  565. if (
  566. page === "songs" ||
  567. page === "stations" ||
  568. page === "reports" ||
  569. page === "news" ||
  570. page === "playlists" ||
  571. page === "users" ||
  572. page === "statistics" ||
  573. page === "punishments" ||
  574. page === "youtube" ||
  575. page === "youtubeVideos" ||
  576. page === "soundcloud" ||
  577. page === "soundcloudTracks" ||
  578. page === "import" ||
  579. page === "dataRequests"
  580. ) {
  581. hasPermission(`admin.view.${page}`, session.userId)
  582. .then(() =>
  583. WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session.socketId }).then(() => {
  584. WSModule.runJob(
  585. "SOCKET_JOIN_ROOM",
  586. {
  587. socketId: session.socketId,
  588. room: `admin.${page}`
  589. },
  590. this
  591. ).then(() => cb({ status: "success", message: "Successfully joined admin room." }));
  592. })
  593. )
  594. .catch(() => cb({ status: "error", message: "Failed to join admin room." }));
  595. }
  596. },
  597. /**
  598. * Leaves all rooms
  599. *
  600. * @param {object} session - user session
  601. * @param {Function} cb - callback
  602. */
  603. leaveRooms(session, cb) {
  604. WSModule.runJob("SOCKET_LEAVE_ROOMS", { socketId: session.socketId });
  605. cb({ status: "success", message: "Successfully left all rooms." });
  606. },
  607. /**
  608. * Returns current date
  609. *
  610. * @param {object} session - user session
  611. * @param {Function} cb - callback
  612. */
  613. ping(session, cb) {
  614. cb({ status: "success", message: "Successfully pinged.", data: { date: Date.now() } });
  615. }
  616. };