utils.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. const CoreClass = require("../core.js");
  2. const config = require("config");
  3. const async = require("async");
  4. const request = require("request");
  5. const crypto = require("crypto");
  6. let youtubeRequestCallbacks = [];
  7. let youtubeRequestsPending = 0;
  8. let youtubeRequestsActive = false;
  9. class UtilsModule extends CoreClass {
  10. constructor() {
  11. super("utils");
  12. }
  13. initialize() {
  14. return new Promise((resolve, reject) => {
  15. this.io = this.moduleManager.modules["io"];
  16. this.db = this.moduleManager.modules["db"];
  17. this.spotify = this.moduleManager.modules["spotify"];
  18. this.cache = this.moduleManager.modules["cache"];
  19. resolve();
  20. });
  21. }
  22. PARSE_COOKIES(payload) {
  23. //cookieString
  24. return new Promise((resolve, reject) => {
  25. let cookies = {};
  26. payload.cookieString.split("; ").map((cookie) => {
  27. cookies[
  28. cookie.substring(0, cookie.indexOf("="))
  29. ] = cookie.substring(cookie.indexOf("=") + 1, cookie.length);
  30. });
  31. resolve(cookies);
  32. });
  33. }
  34. // COOKIES_TO_STRING() {//cookies
  35. // return new Promise((resolve, reject) => {
  36. // let newCookie = [];
  37. // for (let prop in cookie) {
  38. // newCookie.push(prop + "=" + cookie[prop]);
  39. // }
  40. // return newCookie.join("; ");
  41. // });
  42. // }
  43. REMOVE_COOKIE(payload) {
  44. //cookieString, cookieName
  45. return new Promise(async (resolve, reject) => {
  46. var cookies = await this.runJob("PARSE_COOKIES", {
  47. cookieString: payload.cookieString,
  48. });
  49. delete cookies[payload.cookieName];
  50. resolve(this.toString(cookies));
  51. });
  52. }
  53. HTML_ENTITIES(payload) {
  54. //str
  55. return new Promise((resolve, reject) => {
  56. resolve(
  57. String(payload.str)
  58. .replace(/&/g, "&")
  59. .replace(/</g, "&lt;")
  60. .replace(/>/g, "&gt;")
  61. .replace(/"/g, "&quot;")
  62. );
  63. });
  64. }
  65. GENERATE_RANDOM_STRING(payload) {
  66. //length
  67. return new Promise(async (resolve, reject) => {
  68. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split(
  69. ""
  70. );
  71. let result = [];
  72. for (let i = 0; i < payload.length; i++) {
  73. result.push(
  74. chars[
  75. await this.runJob("GET_RANDOM_NUMBER", {
  76. min: 0,
  77. max: chars.length - 1,
  78. })
  79. ]
  80. );
  81. }
  82. resolve(result.join(""));
  83. });
  84. }
  85. GET_SOCKET_FROM_ID(payload) {
  86. //socketId
  87. return new Promise(async (resolve, reject) => {
  88. let io = await this.io.runJob("IO", {});
  89. resolve(io.sockets.sockets[payload.socketId]);
  90. });
  91. }
  92. GET_RANDOM_NUMBER(payload) {
  93. //min, max
  94. return new Promise((resolve, reject) => {
  95. resolve(
  96. Math.floor(Math.random() * (payload.max - payload.min + 1)) +
  97. payload.min
  98. );
  99. });
  100. }
  101. CONVERT_TIME(payload) {
  102. //duration
  103. return new Promise((resolve, reject) => {
  104. let duration = payload.duration;
  105. let a = duration.match(/\d+/g);
  106. if (
  107. duration.indexOf("M") >= 0 &&
  108. duration.indexOf("H") == -1 &&
  109. duration.indexOf("S") == -1
  110. ) {
  111. a = [0, a[0], 0];
  112. }
  113. if (duration.indexOf("H") >= 0 && duration.indexOf("M") == -1) {
  114. a = [a[0], 0, a[1]];
  115. }
  116. if (
  117. duration.indexOf("H") >= 0 &&
  118. duration.indexOf("M") == -1 &&
  119. duration.indexOf("S") == -1
  120. ) {
  121. a = [a[0], 0, 0];
  122. }
  123. duration = 0;
  124. if (a.length == 3) {
  125. duration = duration + parseInt(a[0]) * 3600;
  126. duration = duration + parseInt(a[1]) * 60;
  127. duration = duration + parseInt(a[2]);
  128. }
  129. if (a.length == 2) {
  130. duration = duration + parseInt(a[0]) * 60;
  131. duration = duration + parseInt(a[1]);
  132. }
  133. if (a.length == 1) {
  134. duration = duration + parseInt(a[0]);
  135. }
  136. let hours = Math.floor(duration / 3600);
  137. let minutes = Math.floor((duration % 3600) / 60);
  138. let seconds = Math.floor((duration % 3600) % 60);
  139. resolve(
  140. (hours < 10 ? "0" + hours + ":" : hours + ":") +
  141. (minutes < 10 ? "0" + minutes + ":" : minutes + ":") +
  142. (seconds < 10 ? "0" + seconds : seconds)
  143. );
  144. });
  145. }
  146. GUID(payload) {
  147. return new Promise((resolve, reject) => {
  148. resolve(
  149. [1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1]
  150. .map((b) =>
  151. b
  152. ? Math.floor((1 + Math.random()) * 0x10000)
  153. .toString(16)
  154. .substring(1)
  155. : "-"
  156. )
  157. .join("")
  158. );
  159. });
  160. }
  161. SOCKET_FROM_SESSION(payload) {
  162. //socketId
  163. return new Promise(async (resolve, reject) => {
  164. let io = await this.io.runJob("IO", {});
  165. let ns = io.of("/");
  166. if (ns) {
  167. resolve(ns.connected[payload.socketId]);
  168. }
  169. });
  170. }
  171. SOCKETS_FROM_SESSION_ID(payload) {
  172. //sessionId, cb
  173. return new Promise(async (resolve, reject) => {
  174. let io = await this.io.runJob("IO", {});
  175. let ns = io.of("/");
  176. let sockets = [];
  177. if (ns) {
  178. async.each(
  179. Object.keys(ns.connected),
  180. (id, next) => {
  181. let session = ns.connected[id].session;
  182. if (session.sessionId === payload.sessionId)
  183. sockets.push(session.sessionId);
  184. next();
  185. },
  186. () => {
  187. resolve({ sockets });
  188. }
  189. );
  190. }
  191. });
  192. }
  193. SOCKETS_FROM_USER(payload) {
  194. //userId, cb
  195. return new Promise(async (resolve, reject) => {
  196. let io = await this.io.runJob("IO", {});
  197. let ns = io.of("/");
  198. let sockets = [];
  199. if (ns) {
  200. async.each(
  201. Object.keys(ns.connected),
  202. (id, next) => {
  203. let session = ns.connected[id].session;
  204. this.cache
  205. .runJob("HGET", {
  206. table: "sessions",
  207. key: session.sessionId,
  208. })
  209. .then((session) => {
  210. if (
  211. session &&
  212. session.userId === payload.userId
  213. )
  214. sockets.push(ns.connected[id]);
  215. next();
  216. })
  217. .catch(() => {
  218. next();
  219. });
  220. },
  221. () => {
  222. resolve({ sockets });
  223. }
  224. );
  225. }
  226. });
  227. }
  228. SOCKETS_FROM_IP(payload) {
  229. //ip, cb
  230. return new Promise(async (resolve, reject) => {
  231. let io = await this.io.runJob("IO", {});
  232. let ns = io.of("/");
  233. let sockets = [];
  234. if (ns) {
  235. async.each(
  236. Object.keys(ns.connected),
  237. (id, next) => {
  238. let session = ns.connected[id].session;
  239. this.cache
  240. .runJob("HGET", {
  241. table: "sessions",
  242. key: session.sessionId,
  243. })
  244. .then((session) => {
  245. if (
  246. session &&
  247. ns.connected[id].ip === payload.ip
  248. )
  249. sockets.push(ns.connected[id]);
  250. next();
  251. })
  252. .catch((err) => {
  253. next();
  254. });
  255. },
  256. () => {
  257. resolve({ sockets });
  258. }
  259. );
  260. }
  261. });
  262. }
  263. SOCKETS_FROM_USER_WITHOUT_CACHE(payload) {
  264. //userId, cb
  265. return new Promise(async (resolve, reject) => {
  266. let io = await this.io.runJob("IO", {});
  267. let ns = io.of("/");
  268. let sockets = [];
  269. if (ns) {
  270. async.each(
  271. Object.keys(ns.connected),
  272. (id, next) => {
  273. let session = ns.connected[id].session;
  274. if (session.userId === payload.userId)
  275. sockets.push(ns.connected[id]);
  276. next();
  277. },
  278. () => {
  279. resolve({ sockets });
  280. }
  281. );
  282. }
  283. });
  284. }
  285. SOCKET_LEAVE_ROOMS(payload) {
  286. //socketId
  287. return new Promise(async (resolve, reject) => {
  288. let socket = await this.runJob("SOCKET_FROM_SESSION", {
  289. socketId: payload.socketId,
  290. });
  291. let rooms = socket.rooms;
  292. for (let room in rooms) {
  293. socket.leave(room);
  294. }
  295. resolve();
  296. });
  297. }
  298. SOCKET_JOIN_ROOM(payload) {
  299. //socketId, room
  300. return new Promise(async (resolve, reject) => {
  301. let socket = await this.runJob("SOCKET_FROM_SESSION", {
  302. socketId: payload.socketId,
  303. });
  304. let rooms = socket.rooms;
  305. for (let room in rooms) {
  306. socket.leave(room);
  307. }
  308. socket.join(payload.room);
  309. resolve();
  310. });
  311. }
  312. SOCKET_JOIN_SONG_ROOM(payload) {
  313. //socketId, room
  314. return new Promise(async (resolve, reject) => {
  315. let socket = await this.runJob("SOCKET_FROM_SESSION", {
  316. socketId: payload.socketId,
  317. });
  318. let rooms = socket.rooms;
  319. for (let room in rooms) {
  320. if (room.indexOf("song.") !== -1) socket.leave(rooms);
  321. }
  322. socket.join(payload.room);
  323. resolve();
  324. });
  325. }
  326. SOCKETS_JOIN_SONG_ROOM(payload) {
  327. //sockets, room
  328. return new Promise((resolve, reject) => {
  329. for (let id in payload.sockets) {
  330. let socket = payload.sockets[id];
  331. let rooms = socket.rooms;
  332. for (let room in rooms) {
  333. if (room.indexOf("song.") !== -1) socket.leave(room);
  334. }
  335. socket.join(payload.room);
  336. }
  337. resolve();
  338. });
  339. }
  340. SOCKETS_LEAVE_SONG_ROOMS(payload) {
  341. //sockets
  342. return new Promise((resolve, reject) => {
  343. for (let id in payload.sockets) {
  344. let socket = payload.sockets[id];
  345. let rooms = socket.rooms;
  346. for (let room in rooms) {
  347. if (room.indexOf("song.") !== -1) socket.leave(room);
  348. }
  349. }
  350. resolve();
  351. });
  352. }
  353. EMIT_TO_ROOM(payload) {
  354. //room, ...args
  355. return new Promise(async (resolve, reject) => {
  356. let io = await this.io.runJob("IO", {});
  357. let sockets = io.sockets.sockets;
  358. for (let id in sockets) {
  359. let socket = sockets[id];
  360. if (socket.rooms[payload.room]) {
  361. socket.emit.apply(socket, payload.args);
  362. }
  363. }
  364. resolve();
  365. });
  366. }
  367. GET_ROOM_SOCKETS(payload) {
  368. //room
  369. return new Promise(async (resolve, reject) => {
  370. let io = await this.io.runJob("IO", {});
  371. let sockets = io.sockets.sockets;
  372. let roomSockets = [];
  373. for (let id in sockets) {
  374. let socket = sockets[id];
  375. if (socket.rooms[payload.room]) roomSockets.push(socket);
  376. }
  377. resolve(roomSockets);
  378. });
  379. }
  380. GET_SONG_FROM_YOUTUBE(payload) {
  381. //songId, cb
  382. return new Promise((resolve, reject) => {
  383. youtubeRequestCallbacks.push({
  384. cb: (test) => {
  385. youtubeRequestsActive = true;
  386. const youtubeParams = [
  387. "part=snippet,contentDetails,statistics,status",
  388. `id=${encodeURIComponent(payload.songId)}`,
  389. `key=${config.get("apis.youtube.key")}`,
  390. ].join("&");
  391. request(
  392. `https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`,
  393. (err, res, body) => {
  394. youtubeRequestCallbacks.splice(0, 1);
  395. if (youtubeRequestCallbacks.length > 0) {
  396. youtubeRequestCallbacks[0].cb(
  397. youtubeRequestCallbacks[0].songId
  398. );
  399. } else youtubeRequestsActive = false;
  400. if (err) {
  401. console.error(err);
  402. return null;
  403. }
  404. body = JSON.parse(body);
  405. //TODO Clean up duration converter
  406. let dur = body.items[0].contentDetails.duration;
  407. dur = dur.replace("PT", "");
  408. let duration = 0;
  409. dur = dur.replace(/([\d]*)H/, (v, v2) => {
  410. v2 = Number(v2);
  411. duration = v2 * 60 * 60;
  412. return "";
  413. });
  414. dur = dur.replace(/([\d]*)M/, (v, v2) => {
  415. v2 = Number(v2);
  416. duration += v2 * 60;
  417. return "";
  418. });
  419. dur = dur.replace(/([\d]*)S/, (v, v2) => {
  420. v2 = Number(v2);
  421. duration += v2;
  422. return "";
  423. });
  424. let song = {
  425. songId: body.items[0].id,
  426. title: body.items[0].snippet.title,
  427. duration,
  428. };
  429. resolve({ song });
  430. }
  431. );
  432. },
  433. songId: payload.songId,
  434. });
  435. if (!youtubeRequestsActive) {
  436. youtubeRequestCallbacks[0].cb(
  437. youtubeRequestCallbacks[0].songId
  438. );
  439. }
  440. });
  441. }
  442. FILTER_MUSIC_VIDEOS_YOUTUBE(payload) {
  443. //videoIds, cb
  444. return new Promise((resolve, reject) => {
  445. function getNextPage(cb2) {
  446. let localVideoIds = payload.videoIds.splice(0, 50);
  447. const youtubeParams = [
  448. "part=topicDetails",
  449. `id=${encodeURIComponent(localVideoIds.join(","))}`,
  450. `maxResults=50`,
  451. `key=${config.get("apis.youtube.key")}`,
  452. ].join("&");
  453. request(
  454. `https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`,
  455. async (err, res, body) => {
  456. if (err) {
  457. console.error(err);
  458. return next("Failed to find playlist from YouTube");
  459. }
  460. body = JSON.parse(body);
  461. let songIds = [];
  462. body.items.forEach((item) => {
  463. const songId = item.id;
  464. if (!item.topicDetails) return;
  465. else if (
  466. item.topicDetails.relevantTopicIds.indexOf(
  467. "/m/04rlf"
  468. ) !== -1
  469. ) {
  470. songIds.push(songId);
  471. }
  472. });
  473. if (payload.videoIds.length > 0) {
  474. getNextPage((newSongIds) => {
  475. cb2(songIds.concat(newSongIds));
  476. });
  477. } else cb2(songIds);
  478. }
  479. );
  480. }
  481. if (payload.videoIds.length === 0) resolve({ songIds: [] });
  482. else
  483. getNextPage((songIds) => {
  484. resolve({ songIds });
  485. });
  486. });
  487. }
  488. GET_PLAYLIST_FROM_YOUTUBE(payload) {
  489. //url, musicOnly, cb
  490. return new Promise((resolve, reject) => {
  491. let local = this;
  492. let name = "list".replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
  493. var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
  494. let playlistId = regex.exec(payload.url)[1];
  495. function getPage(pageToken, songs) {
  496. let nextPageToken = pageToken ? `pageToken=${pageToken}` : "";
  497. const youtubeParams = [
  498. "part=contentDetails",
  499. `playlistId=${encodeURIComponent(playlistId)}`,
  500. `maxResults=50`,
  501. `key=${config.get("apis.youtube.key")}`,
  502. nextPageToken,
  503. ].join("&");
  504. request(
  505. `https://www.googleapis.com/youtube/v3/playlistItems?${youtubeParams}`,
  506. async (err, res, body) => {
  507. if (err) {
  508. console.error(err);
  509. return next("Failed to find playlist from YouTube");
  510. }
  511. body = JSON.parse(body);
  512. songs = songs.concat(body.items);
  513. if (body.nextPageToken)
  514. getPage(body.nextPageToken, songs);
  515. else {
  516. songs = songs.map(
  517. (song) => song.contentDetails.videoId
  518. );
  519. if (!payload.musicOnly) resolve({ songs });
  520. else {
  521. local
  522. .runJob("FILTER_MUSIC_VIDEOS_YOUTUBE", {
  523. videoIds: songs.slice(),
  524. })
  525. .then((filteredSongs) => {
  526. resolve({ filteredSongs, songs });
  527. });
  528. }
  529. }
  530. }
  531. );
  532. }
  533. getPage(null, []);
  534. });
  535. }
  536. GET_SONG_FROM_SPOTIFY(payload) {
  537. //song, cb
  538. return new Promise(async (resolve, reject) => {
  539. if (!config.get("apis.spotify.enabled"))
  540. return reject(new Error("Spotify is not enabled."));
  541. const song = Object.assign({}, payload.song);
  542. const spotifyParams = [
  543. `q=${encodeURIComponent(payload.song.title)}`,
  544. `type=track`,
  545. ].join("&");
  546. const token = await this.spotify.runJob("GET_TOKEN", {});
  547. const options = {
  548. url: `https://api.spotify.com/v1/search?${spotifyParams}`,
  549. headers: {
  550. Authorization: `Bearer ${token}`,
  551. },
  552. };
  553. request(options, (err, res, body) => {
  554. if (err) console.error(err);
  555. body = JSON.parse(body);
  556. if (body.error) console.error(body.error);
  557. durationArtistLoop: for (let i in body) {
  558. let items = body[i].items;
  559. for (let j in items) {
  560. let item = items[j];
  561. let hasArtist = false;
  562. for (let k = 0; k < item.artists.length; k++) {
  563. let artist = item.artists[k];
  564. if (song.title.indexOf(artist.name) !== -1)
  565. hasArtist = true;
  566. }
  567. if (hasArtist && song.title.indexOf(item.name) !== -1) {
  568. song.duration = item.duration_ms / 1000;
  569. song.artists = item.artists.map((artist) => {
  570. return artist.name;
  571. });
  572. song.title = item.name;
  573. song.explicit = item.explicit;
  574. song.thumbnail = item.album.images[1].url;
  575. break durationArtistLoop;
  576. }
  577. }
  578. }
  579. resolve({ song });
  580. });
  581. });
  582. }
  583. GET_SONGS_FROM_SPOTIFY(payload) {
  584. //title, artist, cb
  585. return new Promise(async (resolve, reject) => {
  586. if (!config.get("apis.spotify.enabled"))
  587. return reject(new Error("Spotify is not enabled."));
  588. const spotifyParams = [
  589. `q=${encodeURIComponent(payload.title)}`,
  590. `type=track`,
  591. ].join("&");
  592. const token = await this.spotify.runJob("GET_TOKEN", {});
  593. const options = {
  594. url: `https://api.spotify.com/v1/search?${spotifyParams}`,
  595. headers: {
  596. Authorization: `Bearer ${token}`,
  597. },
  598. };
  599. request(options, (err, res, body) => {
  600. if (err) return console.error(err);
  601. body = JSON.parse(body);
  602. if (body.error) return console.error(body.error);
  603. let songs = [];
  604. for (let i in body) {
  605. let items = body[i].items;
  606. for (let j in items) {
  607. let item = items[j];
  608. let hasArtist = false;
  609. for (let k = 0; k < item.artists.length; k++) {
  610. let localArtist = item.artists[k];
  611. if (
  612. payload.artist.toLowerCase() ===
  613. localArtist.name.toLowerCase()
  614. )
  615. hasArtist = true;
  616. }
  617. if (
  618. hasArtist &&
  619. (payload.title.indexOf(item.name) !== -1 ||
  620. item.name.indexOf(payload.title) !== -1)
  621. ) {
  622. let song = {};
  623. song.duration = item.duration_ms / 1000;
  624. song.artists = item.artists.map((artist) => {
  625. return artist.name;
  626. });
  627. song.title = item.name;
  628. song.explicit = item.explicit;
  629. song.thumbnail = item.album.images[1].url;
  630. songs.push(song);
  631. }
  632. }
  633. }
  634. resolve({ songs });
  635. });
  636. });
  637. }
  638. SHUFFLE(payload) {
  639. //array
  640. return new Promise((resolve, reject) => {
  641. const array = payload.array.slice();
  642. let currentIndex = payload.array.length,
  643. temporaryValue,
  644. randomIndex;
  645. // While there remain elements to shuffle...
  646. while (0 !== currentIndex) {
  647. // Pick a remaining element...
  648. randomIndex = Math.floor(Math.random() * currentIndex);
  649. currentIndex -= 1;
  650. // And swap it with the current element.
  651. temporaryValue = array[currentIndex];
  652. array[currentIndex] = array[randomIndex];
  653. array[randomIndex] = temporaryValue;
  654. }
  655. resolve({ array });
  656. });
  657. }
  658. GET_ERROR(payload) {
  659. //err
  660. return new Promise((resolve, reject) => {
  661. let error = "An error occurred.";
  662. if (typeof payload.error === "string") error = payload.error;
  663. else if (payload.error.message) {
  664. if (payload.error.message !== "Validation failed")
  665. error = payload.error.message;
  666. else
  667. error =
  668. payload.error.errors[Object.keys(payload.error.errors)]
  669. .message;
  670. }
  671. resolve(error);
  672. });
  673. }
  674. CREATE_GRAVATAR(payload) {
  675. //email
  676. return new Promise((resolve, reject) => {
  677. const hash = crypto
  678. .createHash("md5")
  679. .update(payload.email)
  680. .digest("hex");
  681. resolve(`https://www.gravatar.com/avatar/${hash}`);
  682. });
  683. }
  684. DEBUG(payload) {
  685. return new Promise((resolve, reject) => {
  686. resolve();
  687. });
  688. }
  689. }
  690. module.exports = new UtilsModule();