soundcloud.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. import mongoose from "mongoose";
  2. import async from "async";
  3. import config from "config";
  4. import sckey from "soundcloud-key-fetch";
  5. import * as rax from "retry-axios";
  6. import axios from "axios";
  7. import CoreClass from "../core";
  8. let SoundCloudModule;
  9. let DBModule;
  10. let CacheModule;
  11. let MediaModule;
  12. const soundcloudTrackObjectToMusareTrackObject = soundcloudTrackObject => {
  13. const {
  14. id,
  15. title,
  16. artwork_url: artworkUrl,
  17. created_at: createdAt,
  18. duration,
  19. genre,
  20. kind,
  21. license,
  22. likes_count: likesCount,
  23. playback_count: playbackCount,
  24. public: _public,
  25. tag_list: tagList,
  26. user_id: userId,
  27. user,
  28. track_format: trackFormat,
  29. permalink,
  30. monetization_model: monetizationModel,
  31. policy,
  32. streamable,
  33. sharing,
  34. state,
  35. embeddable_by: embeddableBy
  36. } = soundcloudTrackObject;
  37. return {
  38. trackId: id,
  39. title,
  40. artworkUrl,
  41. soundcloudCreatedAt: new Date(createdAt),
  42. duration: duration / 1000,
  43. genre,
  44. kind,
  45. license,
  46. likesCount,
  47. playbackCount,
  48. public: _public,
  49. tagList,
  50. userId,
  51. username: user.username,
  52. userPermalink: user.permalink,
  53. trackFormat,
  54. permalink,
  55. monetizationModel,
  56. policy,
  57. streamable,
  58. sharing,
  59. state,
  60. embeddableBy
  61. };
  62. };
  63. class RateLimitter {
  64. /**
  65. * Constructor
  66. *
  67. * @param {number} timeBetween - The time between each allowed YouTube request
  68. */
  69. constructor(timeBetween) {
  70. this.dateStarted = Date.now();
  71. this.timeBetween = timeBetween;
  72. }
  73. /**
  74. * Returns a promise that resolves whenever the ratelimit of a YouTube request is done
  75. *
  76. * @returns {Promise} - promise that gets resolved when the rate limit allows it
  77. */
  78. continue() {
  79. return new Promise(resolve => {
  80. if (Date.now() - this.dateStarted >= this.timeBetween) resolve();
  81. else setTimeout(resolve, this.dateStarted + this.timeBetween - Date.now());
  82. });
  83. }
  84. /**
  85. * Restart the rate limit timer
  86. */
  87. restart() {
  88. this.dateStarted = Date.now();
  89. }
  90. }
  91. class _SoundCloudModule extends CoreClass {
  92. // eslint-disable-next-line require-jsdoc
  93. constructor() {
  94. super("soundcloud");
  95. SoundCloudModule = this;
  96. }
  97. /**
  98. * Initialises the soundcloud module
  99. *
  100. * @returns {Promise} - returns promise (reject, resolve)
  101. */
  102. async initialize() {
  103. DBModule = this.moduleManager.modules.db;
  104. CacheModule = this.moduleManager.modules.cache;
  105. MediaModule = this.moduleManager.modules.media;
  106. // this.youtubeApiRequestModel = this.YoutubeApiRequestModel = await DBModule.runJob("GET_MODEL", {
  107. // modelName: "youtubeApiRequest"
  108. // });
  109. this.soundcloudTrackModel = this.SoundCloudTrackModel = await DBModule.runJob("GET_MODEL", {
  110. modelName: "soundcloudTrack"
  111. });
  112. return new Promise((resolve, reject) => {
  113. this.rateLimiter = new RateLimitter(config.get("apis.soundcloud.rateLimit"));
  114. this.requestTimeout = config.get("apis.soundcloud.requestTimeout");
  115. this.axios = axios.create();
  116. this.axios.defaults.raxConfig = {
  117. instance: this.axios,
  118. retry: config.get("apis.soundcloud.retryAmount"),
  119. noResponseRetries: config.get("apis.soundcloud.retryAmount")
  120. };
  121. rax.attach(this.axios);
  122. this.apiKey = null;
  123. SoundCloudModule.runJob("TEST_SOUNDCLOUD_API_KEY", {}, null, -1)
  124. .then(result => {
  125. if (result) {
  126. resolve();
  127. return;
  128. }
  129. SoundCloudModule.runJob("GENERATE_SOUNDCLOUD_API_KEY", {}, null, -1)
  130. .then(() => {
  131. resolve();
  132. })
  133. .catch(reject);
  134. })
  135. .catch(reject);
  136. });
  137. }
  138. /**
  139. *
  140. * @returns
  141. */
  142. GENERATE_SOUNDCLOUD_API_KEY() {
  143. return new Promise((resolve, reject) => {
  144. this.log("INFO", "Fetching new SoundCloud API key.");
  145. sckey
  146. .fetchKey()
  147. .then(soundcloudApiKey => {
  148. if (!soundcloudApiKey) {
  149. this.log("ERROR", "Couldn't fetch new SoundCloud API key.");
  150. reject(new Error("Couldn't fetch SoundCloud key."));
  151. return;
  152. }
  153. SoundCloudModule.soundcloudApiKey = soundcloudApiKey;
  154. CacheModule.runJob("SET", { key: "soundcloudApiKey", value: soundcloudApiKey }, this)
  155. .then(() => {
  156. SoundCloudModule.runJob("TEST_SOUNDCLOUD_API_KEY", {}, this).then(result => {
  157. if (!result) {
  158. this.log("ERROR", "Fetched SoundCloud API key is invalid.");
  159. reject(new Error("SoundCloud key isn't valid."));
  160. } else {
  161. this.log("INFO", "Fetched new valid SoundCloud API key.");
  162. resolve();
  163. }
  164. });
  165. })
  166. .catch(err => {
  167. this.log("ERROR", `Couldn't set new SoundCloud API key in cache. Error: ${err.message}`);
  168. reject(err);
  169. });
  170. })
  171. .catch(err => {
  172. this.log("ERROR", `Couldn't fetch new SoundCloud API key. Error: ${err.message}`);
  173. reject(new Error("Couldn't fetch SoundCloud key."));
  174. });
  175. });
  176. }
  177. /**
  178. *
  179. * @returns
  180. */
  181. TEST_SOUNDCLOUD_API_KEY() {
  182. return new Promise((resolve, reject) => {
  183. this.log("INFO", "Testing SoundCloud API key.");
  184. CacheModule.runJob("GET", { key: "soundcloudApiKey" }, this).then(soundcloudApiKey => {
  185. if (!soundcloudApiKey) {
  186. this.log("ERROR", "No SoundCloud API key found in cache.");
  187. return resolve(false);
  188. }
  189. SoundCloudModule.soundcloudApiKey = soundcloudApiKey;
  190. sckey
  191. .testKey(soundcloudApiKey)
  192. .then(res => {
  193. this.log("INFO", `Tested SoundCloud API key. Result: ${res}`);
  194. resolve(res);
  195. })
  196. .catch(err => {
  197. this.log("ERROR", `Testing SoundCloud API key error: ${err.message}`);
  198. reject(err);
  199. });
  200. });
  201. });
  202. }
  203. /**
  204. * Perform SoundCloud API get track request
  205. *
  206. * @param {object} payload - object that contains the payload
  207. * @param {object} payload.params - request parameters
  208. * @returns {Promise} - returns promise (reject, resolve)
  209. */
  210. API_GET_TRACK(payload) {
  211. return new Promise((resolve, reject) => {
  212. const { trackId } = payload;
  213. SoundCloudModule.runJob(
  214. "API_CALL",
  215. {
  216. url: `https://api-v2.soundcloud.com/tracks/${trackId}`
  217. },
  218. this
  219. )
  220. .then(response => {
  221. resolve(response);
  222. })
  223. .catch(err => {
  224. reject(err);
  225. });
  226. });
  227. }
  228. /**
  229. * Perform SoundCloud API call
  230. *
  231. * @param {object} payload - object that contains the payload
  232. * @param {object} payload.url - request url
  233. * @param {object} payload.params - request parameters
  234. * @param {object} payload.quotaCost - request quotaCost
  235. * @returns {Promise} - returns promise (reject, resolve)
  236. */
  237. API_CALL(payload) {
  238. return new Promise((resolve, reject) => {
  239. // const { url, params, quotaCost } = payload;
  240. const { url } = payload;
  241. const { soundcloudApiKey } = SoundCloudModule;
  242. const params = {
  243. client_id: soundcloudApiKey
  244. };
  245. SoundCloudModule.axios
  246. .get(url, {
  247. params,
  248. timeout: SoundCloudModule.requestTimeout
  249. })
  250. .then(response => {
  251. if (response.data.error) {
  252. reject(new Error(response.data.error));
  253. } else {
  254. resolve({ response });
  255. }
  256. })
  257. .catch(err => {
  258. reject(err);
  259. });
  260. // }
  261. });
  262. }
  263. /**
  264. * Create SoundCloud track
  265. *
  266. * @param {object} payload - an object containing the payload
  267. * @param {string} payload.soundcloudTrack - the soundcloudTrack object
  268. * @returns {Promise} - returns a promise (resolve, reject)
  269. */
  270. CREATE_TRACK(payload) {
  271. return new Promise((resolve, reject) => {
  272. async.waterfall(
  273. [
  274. next => {
  275. const { soundcloudTrack } = payload;
  276. if (typeof soundcloudTrack !== "object") next("Invalid soundcloudTrack type");
  277. else {
  278. SoundCloudModule.soundcloudTrackModel.insertMany(soundcloudTrack, next);
  279. }
  280. },
  281. (soundcloudTracks, next) => {
  282. const mediaSources = soundcloudTracks.map(
  283. soundcloudTrack => `soundcloud:${soundcloudTrack.trackId}`
  284. );
  285. async.eachLimit(
  286. mediaSources,
  287. 2,
  288. (mediaSource, next) => {
  289. MediaModule.runJob("RECALCULATE_RATINGS", { mediaSource }, this)
  290. .then(() => next())
  291. .catch(next);
  292. },
  293. err => {
  294. if (err) next(err);
  295. else next(null, soundcloudTracks);
  296. }
  297. );
  298. }
  299. ],
  300. (err, soundcloudTracks) => {
  301. if (err) reject(new Error(err));
  302. else resolve({ soundcloudTracks });
  303. }
  304. );
  305. });
  306. }
  307. /**
  308. * Get SoundCloud track
  309. *
  310. * @param {object} payload - an object containing the payload
  311. * @param {string} payload.identifier - the soundcloud track ObjectId or track id
  312. * @param {string} payload.createMissing - attempt to fetch and create track if not in db
  313. * @returns {Promise} - returns a promise (resolve, reject)
  314. */
  315. GET_TRACK(payload) {
  316. return new Promise((resolve, reject) => {
  317. async.waterfall(
  318. [
  319. next => {
  320. const query = mongoose.isObjectIdOrHexString(payload.identifier)
  321. ? { _id: payload.identifier }
  322. : { trackId: payload.identifier };
  323. return SoundCloudModule.soundcloudTrackModel.findOne(query, next);
  324. },
  325. (track, next) => {
  326. if (track) return next(null, track, false);
  327. if (mongoose.isObjectIdOrHexString(payload.identifier) || !payload.createMissing)
  328. return next("SoundCloud track not found.");
  329. return SoundCloudModule.runJob("API_GET_TRACK", { trackId: payload.identifier }, this)
  330. .then(({ response }) => {
  331. const { data } = response;
  332. if (!data || !data.id)
  333. return next("The specified track does not exist or cannot be publicly accessed.");
  334. const soundcloudTrack = soundcloudTrackObjectToMusareTrackObject(data);
  335. return next(null, false, soundcloudTrack);
  336. })
  337. .catch(next);
  338. },
  339. (track, soundcloudTrack, next) => {
  340. if (track) return next(null, track, true);
  341. return SoundCloudModule.runJob("CREATE_TRACK", { soundcloudTrack }, this)
  342. .then(res => {
  343. if (res.soundcloudTracks.length === 1) next(null, res.soundcloudTracks[0], false);
  344. else next("SoundCloud track not found.");
  345. })
  346. .catch(next);
  347. }
  348. ],
  349. (err, track, existing) => {
  350. if (err) reject(new Error(err));
  351. else if (track.policy === "SNIP") reject(new Error("Track is premium-only."));
  352. else resolve({ track, existing });
  353. }
  354. );
  355. });
  356. }
  357. /**
  358. * Gets a track from a SoundCloud URL
  359. *
  360. * @param {*} payload
  361. * @returns {Promise}
  362. */
  363. GET_TRACK_FROM_URL(payload) {
  364. return new Promise((resolve, reject) => {
  365. const scRegex =
  366. /soundcloud\.com\/(?<userPermalink>[A-Za-z0-9]+([-_][A-Za-z0-9]+)*)\/(?<permalink>[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*)/;
  367. async.waterfall(
  368. [
  369. next => {
  370. const match = scRegex.exec(payload.identifier);
  371. if (!match || !match.groups) return next("Invalid SoundCloud URL.");
  372. const { userPermalink, permalink } = match.groups;
  373. SoundCloudModule.soundcloudTrackModel.findOne({ userPermalink, permalink }, next);
  374. },
  375. (_dbTrack, next) => {
  376. if (_dbTrack) return next(null, _dbTrack, true);
  377. SoundCloudModule.runJob("API_RESOLVE", { url: payload.identifier }, this)
  378. .then(({ response }) => {
  379. const { data } = response;
  380. if (!data || !data.id)
  381. return next("The provided URL does not exist or cannot be accessed.");
  382. if (data.kind !== "track") return next(`Invalid URL provided. Kind got: ${data.kind}.`);
  383. // TODO get more data here
  384. const { id: trackId } = data;
  385. SoundCloudModule.soundcloudTrackModel.findOne({ trackId }, (err, dbTrack) => {
  386. if (err) next(err);
  387. else if (dbTrack) {
  388. next(null, dbTrack, true);
  389. } else {
  390. const soundcloudTrack = soundcloudTrackObjectToMusareTrackObject(data);
  391. SoundCloudModule.runJob("CREATE_TRACK", { soundcloudTrack }, this)
  392. .then(res => {
  393. if (res.soundcloudTracks.length === 1)
  394. next(null, res.soundcloudTracks[0], false);
  395. else next("SoundCloud track not found.");
  396. })
  397. .catch(next);
  398. }
  399. });
  400. })
  401. .catch(next);
  402. }
  403. ],
  404. (err, track, existing) => {
  405. if (err) reject(new Error(err));
  406. else if (track.policy === "SNIP") reject(new Error("Track is premium-only."));
  407. else resolve({ track, existing });
  408. }
  409. );
  410. });
  411. }
  412. /**
  413. * Returns an array of songs taken from a SoundCloud playlist
  414. *
  415. * @param {object} payload - object that contains the payload
  416. * @param {string} payload.url - the url of the SoundCloud playlist
  417. * @returns {Promise} - returns promise (reject, resolve)
  418. */
  419. GET_PLAYLIST(payload) {
  420. return new Promise((resolve, reject) => {
  421. async.waterfall(
  422. [
  423. next => {
  424. SoundCloudModule.runJob("API_RESOLVE", { url: payload.url }, this)
  425. .then(({ response }) => {
  426. const { data } = response;
  427. if (!data || !data.id)
  428. return next("The provided URL does not exist or cannot be accessed.");
  429. if (data.kind !== "playlist" && data.kind !== "system-playlist")
  430. return next(`Invalid URL provided. Kind got: ${data.kind}.`);
  431. const { tracks } = data;
  432. // TODO get more data here
  433. const soundcloudTrackIds = tracks.map(track => track.id);
  434. return next(null, soundcloudTrackIds);
  435. })
  436. .catch(next);
  437. }
  438. ],
  439. (err, soundcloudTrackIds) => {
  440. if (err && err !== true) {
  441. SoundCloudModule.log(
  442. "ERROR",
  443. "GET_PLAYLIST",
  444. "Some error has occurred.",
  445. typeof err === "string" ? err : err.message
  446. );
  447. reject(new Error(typeof err === "string" ? err : err.message));
  448. } else {
  449. resolve({ songs: soundcloudTrackIds });
  450. }
  451. }
  452. );
  453. // kind;
  454. });
  455. }
  456. /**
  457. * @param {object} payload - object that contains the payload
  458. * @param {string} payload.url - the url of the SoundCloud resource
  459. */
  460. API_RESOLVE(payload) {
  461. return new Promise((resolve, reject) => {
  462. const { url } = payload;
  463. SoundCloudModule.runJob(
  464. "API_CALL",
  465. {
  466. url: `https://api-v2.soundcloud.com/resolve?url=${encodeURIComponent(url)}`
  467. },
  468. this
  469. )
  470. .then(response => {
  471. resolve(response);
  472. })
  473. .catch(err => {
  474. reject(err);
  475. });
  476. });
  477. }
  478. }
  479. export default new _SoundCloudModule();