utils.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. 'use strict';
  2. const coreClass = require("../core");
  3. const config = require('config');
  4. const async = require('async');
  5. const request = require('request');
  6. const crypto = require('crypto');
  7. class Timer {
  8. constructor(callback, delay, paused) {
  9. this.callback = callback;
  10. this.timerId = undefined;
  11. this.start = undefined;
  12. this.paused = paused;
  13. this.remaining = delay;
  14. this.timeWhenPaused = 0;
  15. this.timePaused = Date.now();
  16. if (!paused) {
  17. this.resume();
  18. }
  19. }
  20. pause() {
  21. clearTimeout(this.timerId);
  22. this.remaining -= Date.now() - this.start;
  23. this.timePaused = Date.now();
  24. this.paused = true;
  25. }
  26. ifNotPaused() {
  27. if (!this.paused) {
  28. this.resume();
  29. }
  30. }
  31. resume() {
  32. this.start = Date.now();
  33. clearTimeout(this.timerId);
  34. this.timerId = setTimeout(this.callback, this.remaining);
  35. this.timeWhenPaused = Date.now() - this.timePaused;
  36. this.paused = false;
  37. }
  38. resetTimeWhenPaused() {
  39. this.timeWhenPaused = 0;
  40. }
  41. getTimePaused() {
  42. if (!this.paused) {
  43. return this.timeWhenPaused;
  44. } else {
  45. return Date.now() - this.timePaused;
  46. }
  47. }
  48. }
  49. let youtubeRequestCallbacks = [];
  50. let youtubeRequestsPending = 0;
  51. let youtubeRequestsActive = false;
  52. module.exports = class extends coreClass {
  53. initialize() {
  54. return new Promise((resolve, reject) => {
  55. this.setStage(1);
  56. this.io = this.moduleManager.modules["io"];
  57. this.db = this.moduleManager.modules["db"];
  58. this.spotify = this.moduleManager.modules["spotify"];
  59. this.cache = this.moduleManager.modules["cache"];
  60. this.Timer = Timer;
  61. resolve();
  62. });
  63. }
  64. async parseCookies(cookieString) {
  65. try { await this._validateHook(); } catch { return; }
  66. let cookies = {};
  67. if (cookieString) cookieString.split("; ").map((cookie) => {
  68. (cookies[cookie.substring(0, cookie.indexOf("="))] = cookie.substring(cookie.indexOf("=") + 1, cookie.length));
  69. });
  70. return cookies;
  71. }
  72. async cookiesToString(cookies) {
  73. try { await this._validateHook(); } catch { return; }
  74. let newCookie = [];
  75. for (let prop in cookie) {
  76. newCookie.push(prop + "=" + cookie[prop]);
  77. }
  78. return newCookie.join("; ");
  79. }
  80. async removeCookie(cookieString, cookieName) {
  81. try { await this._validateHook(); } catch { return; }
  82. var cookies = this.parseCookies(cookieString);
  83. delete cookies[cookieName];
  84. return this.toString(cookies);
  85. }
  86. async htmlEntities(str) {
  87. try { await this._validateHook(); } catch { return; }
  88. return String(str).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;')
  89. }
  90. async generateRandomString(len) {
  91. try { await this._validateHook(); } catch { return; }
  92. let chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".split("");
  93. let result = [];
  94. for (let i = 0; i < len; i++) {
  95. result.push(chars[await this.getRandomNumber(0, chars.length - 1)]);
  96. }
  97. return result.join("");
  98. }
  99. async getSocketFromId(socketId) {
  100. try { await this._validateHook(); } catch { return; }
  101. return globals.io.sockets.sockets[socketId];
  102. }
  103. async getRandomNumber(min, max) {
  104. try { await this._validateHook(); } catch { return; }
  105. return Math.floor(Math.random() * (max - min + 1)) + min
  106. }
  107. async convertTime(duration) {
  108. try { await this._validateHook(); } catch { return; }
  109. let a = duration.match(/\d+/g);
  110. if (duration.indexOf('M') >= 0 && duration.indexOf('H') == -1 && duration.indexOf('S') == -1) {
  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 (duration.indexOf('H') >= 0 && duration.indexOf('M') == -1 && duration.indexOf('S') == -1) {
  117. a = [a[0], 0, 0];
  118. }
  119. duration = 0;
  120. if (a.length == 3) {
  121. duration = duration + parseInt(a[0]) * 3600;
  122. duration = duration + parseInt(a[1]) * 60;
  123. duration = duration + parseInt(a[2]);
  124. }
  125. if (a.length == 2) {
  126. duration = duration + parseInt(a[0]) * 60;
  127. duration = duration + parseInt(a[1]);
  128. }
  129. if (a.length == 1) {
  130. duration = duration + parseInt(a[0]);
  131. }
  132. let hours = Math.floor(duration / 3600);
  133. let minutes = Math.floor(duration % 3600 / 60);
  134. let seconds = Math.floor(duration % 3600 % 60);
  135. return (hours < 10 ? ("0" + hours + ":") : (hours + ":")) + (minutes < 10 ? ("0" + minutes + ":") : (minutes + ":")) + (seconds < 10 ? ("0" + seconds) : seconds);
  136. }
  137. async guid () {
  138. try { await this._validateHook(); } catch { return; }
  139. return [1,1,0,1,0,1,0,1,0,1,1,1].map(b => b ? Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1) : '-').join('');
  140. }
  141. async socketFromSession(socketId) {
  142. try { await this._validateHook(); } catch { return; }
  143. let io = await this.io.io();
  144. let ns = io.of("/");
  145. if (ns) {
  146. return ns.connected[socketId];
  147. }
  148. }
  149. async socketsFromSessionId(sessionId, cb) {
  150. try { await this._validateHook(); } catch { return; }
  151. let io = await this.io.io();
  152. let ns = io.of("/");
  153. let sockets = [];
  154. if (ns) {
  155. async.each(Object.keys(ns.connected), (id, next) => {
  156. let session = ns.connected[id].session;
  157. if (session.sessionId === sessionId) sockets.push(session.sessionId);
  158. next();
  159. }, () => {
  160. cb(sockets);
  161. });
  162. }
  163. }
  164. async socketsFromUser(userId, cb) {
  165. try { await this._validateHook(); } catch { return; }
  166. let io = await this.io.io();
  167. let ns = io.of("/");
  168. let sockets = [];
  169. if (ns) {
  170. async.each(Object.keys(ns.connected), (id, next) => {
  171. let session = ns.connected[id].session;
  172. this.cache.hget('sessions', session.sessionId, (err, session) => {
  173. if (!err && session && session.userId === userId) sockets.push(ns.connected[id]);
  174. next();
  175. });
  176. }, () => {
  177. cb(sockets);
  178. });
  179. }
  180. }
  181. async socketsFromIP(ip, cb) {
  182. try { await this._validateHook(); } catch { return; }
  183. let io = await this.io.io();
  184. let ns = io.of("/");
  185. let sockets = [];
  186. if (ns) {
  187. async.each(Object.keys(ns.connected), (id, next) => {
  188. let session = ns.connected[id].session;
  189. this.cache.hget('sessions', session.sessionId, (err, session) => {
  190. if (!err && session && ns.connected[id].ip === ip) sockets.push(ns.connected[id]);
  191. next();
  192. });
  193. }, () => {
  194. cb(sockets);
  195. });
  196. }
  197. }
  198. async socketsFromUserWithoutCache(userId, cb) {
  199. try { await this._validateHook(); } catch { return; }
  200. let io = await this.io.io();
  201. let ns = io.of("/");
  202. let sockets = [];
  203. if (ns) {
  204. async.each(Object.keys(ns.connected), (id, next) => {
  205. let session = ns.connected[id].session;
  206. if (session.userId === userId) sockets.push(ns.connected[id]);
  207. next();
  208. }, () => {
  209. cb(sockets);
  210. });
  211. }
  212. }
  213. async socketLeaveRooms(socketid) {
  214. try { await this._validateHook(); } catch { return; }
  215. let socket = await this.socketFromSession(socketid);
  216. let rooms = socket.rooms;
  217. for (let room in rooms) {
  218. socket.leave(room);
  219. }
  220. }
  221. async socketJoinRoom(socketId, room) {
  222. try { await this._validateHook(); } catch { return; }
  223. let socket = await this.socketFromSession(socketId);
  224. let rooms = socket.rooms;
  225. for (let room in rooms) {
  226. socket.leave(room);
  227. }
  228. socket.join(room);
  229. }
  230. async socketJoinSongRoom(socketId, room) {
  231. try { await this._validateHook(); } catch { return; }
  232. let socket = await this.socketFromSession(socketId);
  233. let rooms = socket.rooms;
  234. for (let room in rooms) {
  235. if (room.indexOf('song.') !== -1) socket.leave(rooms);
  236. }
  237. socket.join(room);
  238. }
  239. async socketsJoinSongRoom(sockets, room) {
  240. try { await this._validateHook(); } catch { return; }
  241. for (let id in sockets) {
  242. let socket = sockets[id];
  243. let rooms = socket.rooms;
  244. for (let room in rooms) {
  245. if (room.indexOf('song.') !== -1) socket.leave(room);
  246. }
  247. socket.join(room);
  248. }
  249. }
  250. async socketsLeaveSongRooms(sockets) {
  251. try { await this._validateHook(); } catch { return; }
  252. for (let id in sockets) {
  253. let socket = sockets[id];
  254. let rooms = socket.rooms;
  255. for (let room in rooms) {
  256. if (room.indexOf('song.') !== -1) socket.leave(room);
  257. }
  258. }
  259. }
  260. async emitToRoom(room, ...args) {
  261. try { await this._validateHook(); } catch { return; }
  262. let io = await this.io.io();
  263. let sockets = io.sockets.sockets;
  264. for (let id in sockets) {
  265. let socket = sockets[id];
  266. if (socket.rooms[room]) {
  267. socket.emit.apply(socket, args);
  268. }
  269. }
  270. }
  271. async getRoomSockets(room) {
  272. try { await this._validateHook(); } catch { return; }
  273. let io = await this.io.io();
  274. let sockets = io.sockets.sockets;
  275. let roomSockets = [];
  276. for (let id in sockets) {
  277. let socket = sockets[id];
  278. if (socket.rooms[room]) roomSockets.push(socket);
  279. }
  280. return roomSockets;
  281. }
  282. async getSongFromYouTube(songId, cb) {
  283. try { await this._validateHook(); } catch { return; }
  284. youtubeRequestCallbacks.push({cb: (test) => {
  285. youtubeRequestsActive = true;
  286. const youtubeParams = [
  287. 'part=snippet,contentDetails,statistics,status',
  288. `id=${encodeURIComponent(songId)}`,
  289. `key=${config.get('apis.youtube.key')}`
  290. ].join('&');
  291. request(`https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`, (err, res, body) => {
  292. youtubeRequestCallbacks.splice(0, 1);
  293. if (youtubeRequestCallbacks.length > 0) {
  294. youtubeRequestCallbacks[0].cb(youtubeRequestCallbacks[0].songId);
  295. } else youtubeRequestsActive = false;
  296. if (err) {
  297. console.error(err);
  298. return null;
  299. }
  300. body = JSON.parse(body);
  301. //TODO Clean up duration converter
  302. let dur = body.items[0].contentDetails.duration;
  303. dur = dur.replace('PT', '');
  304. let duration = 0;
  305. dur = dur.replace(/([\d]*)H/, (v, v2) => {
  306. v2 = Number(v2);
  307. duration = (v2 * 60 * 60);
  308. return '';
  309. });
  310. dur = dur.replace(/([\d]*)M/, (v, v2) => {
  311. v2 = Number(v2);
  312. duration += (v2 * 60);
  313. return '';
  314. });
  315. dur = dur.replace(/([\d]*)S/, (v, v2) => {
  316. v2 = Number(v2);
  317. duration += v2;
  318. return '';
  319. });
  320. let song = {
  321. songId: body.items[0].id,
  322. title: body.items[0].snippet.title,
  323. duration
  324. };
  325. cb(song);
  326. });
  327. }, songId});
  328. if (!youtubeRequestsActive) {
  329. youtubeRequestCallbacks[0].cb(youtubeRequestCallbacks[0].songId);
  330. }
  331. }
  332. async filterMusicVideosYouTube(videoIds, cb) {
  333. try { await this._validateHook(); } catch { return; }
  334. function getNextPage(cb2) {
  335. let localVideoIds = videoIds.splice(0, 50);
  336. const youtubeParams = [
  337. 'part=topicDetails',
  338. `id=${encodeURIComponent(localVideoIds.join(","))}`,
  339. `maxResults=50`,
  340. `key=${config.get('apis.youtube.key')}`
  341. ].join('&');
  342. request(`https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`, async (err, res, body) => {
  343. if (err) {
  344. console.error(err);
  345. return next('Failed to find playlist from YouTube');
  346. }
  347. body = JSON.parse(body);
  348. let songIds = [];
  349. body.items.forEach(item => {
  350. const songId = item.id;
  351. if (!item.topicDetails) return;
  352. else if (item.topicDetails.topicIds.indexOf("/m/04rlf") !== -1) {
  353. songIds.push(songId);
  354. }
  355. });
  356. if (videoIds.length > 0) {
  357. getNextPage(newSongIds => {
  358. cb2(songIds.concat(newSongIds));
  359. });
  360. } else cb2(songIds);
  361. });
  362. }
  363. if (videoIds.length === 0) cb([]);
  364. else getNextPage(songIds => {
  365. cb(songIds);
  366. });
  367. }
  368. async getPlaylistFromYouTube(url, musicOnly, cb) {
  369. try { await this._validateHook(); } catch { return; }
  370. let local = this;
  371. let name = 'list'.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  372. var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
  373. let playlistId = regex.exec(url)[1];
  374. function getPage(pageToken, songs) {
  375. let nextPageToken = (pageToken) ? `pageToken=${pageToken}` : '';
  376. const youtubeParams = [
  377. 'part=contentDetails',
  378. `playlistId=${encodeURIComponent(playlistId)}`,
  379. `maxResults=50`,
  380. `key=${config.get('apis.youtube.key')}`,
  381. nextPageToken
  382. ].join('&');
  383. request(`https://www.googleapis.com/youtube/v3/playlistItems?${youtubeParams}`, async (err, res, body) => {
  384. if (err) {
  385. console.error(err);
  386. return next('Failed to find playlist from YouTube');
  387. }
  388. body = JSON.parse(body);
  389. songs = songs.concat(body.items);
  390. if (body.nextPageToken) getPage(body.nextPageToken, songs);
  391. else {
  392. songs = songs.map(song => song.contentDetails.videoId);
  393. if (!musicOnly) cb(songs);
  394. else {
  395. local.filterMusicVideosYouTube(songs.slice(), (filteredSongs) => {
  396. cb(filteredSongs, songs);
  397. });
  398. }
  399. }
  400. });
  401. }
  402. getPage(null, []);
  403. }
  404. async getSongFromSpotify(song, cb) {
  405. try { await this._validateHook(); } catch { return; }
  406. if (!config.get("apis.spotify.enabled")) return cb("Spotify is not enabled", null);
  407. const spotifyParams = [
  408. `q=${encodeURIComponent(song.title)}`,
  409. `type=track`
  410. ].join('&');
  411. const token = await this.spotify.getToken();
  412. const options = {
  413. url: `https://api.spotify.com/v1/search?${spotifyParams}`,
  414. headers: {
  415. Authorization: `Bearer ${token}`
  416. }
  417. };
  418. request(options, (err, res, body) => {
  419. if (err) console.error(err);
  420. body = JSON.parse(body);
  421. if (body.error) console.error(body.error);
  422. durationArtistLoop:
  423. for (let i in body) {
  424. let items = body[i].items;
  425. for (let j in items) {
  426. let item = items[j];
  427. let hasArtist = false;
  428. for (let k = 0; k < item.artists.length; k++) {
  429. let artist = item.artists[k];
  430. if (song.title.indexOf(artist.name) !== -1) hasArtist = true;
  431. }
  432. if (hasArtist && song.title.indexOf(item.name) !== -1) {
  433. song.duration = item.duration_ms / 1000;
  434. song.artists = item.artists.map(artist => {
  435. return artist.name;
  436. });
  437. song.title = item.name;
  438. song.explicit = item.explicit;
  439. song.thumbnail = item.album.images[1].url;
  440. break durationArtistLoop;
  441. }
  442. }
  443. }
  444. cb(null, song);
  445. });
  446. }
  447. async getSongsFromSpotify(title, artist, cb) {
  448. try { await this._validateHook(); } catch { return; }
  449. if (!config.get("apis.spotify.enabled")) return cb([]);
  450. const spotifyParams = [
  451. `q=${encodeURIComponent(title)}`,
  452. `type=track`
  453. ].join('&');
  454. const token = await this.spotify.getToken();
  455. const options = {
  456. url: `https://api.spotify.com/v1/search?${spotifyParams}`,
  457. headers: {
  458. Authorization: `Bearer ${token}`
  459. }
  460. };
  461. request(options, (err, res, body) => {
  462. if (err) return console.error(err);
  463. body = JSON.parse(body);
  464. if (body.error) return console.error(body.error);
  465. let songs = [];
  466. for (let i in body) {
  467. let items = body[i].items;
  468. for (let j in items) {
  469. let item = items[j];
  470. let hasArtist = false;
  471. for (let k = 0; k < item.artists.length; k++) {
  472. let localArtist = item.artists[k];
  473. if (artist.toLowerCase() === localArtist.name.toLowerCase()) hasArtist = true;
  474. }
  475. if (hasArtist && (title.indexOf(item.name) !== -1 || item.name.indexOf(title) !== -1)) {
  476. let song = {};
  477. song.duration = item.duration_ms / 1000;
  478. song.artists = item.artists.map(artist => {
  479. return artist.name;
  480. });
  481. song.title = item.name;
  482. song.explicit = item.explicit;
  483. song.thumbnail = item.album.images[1].url;
  484. songs.push(song);
  485. }
  486. }
  487. }
  488. cb(songs);
  489. });
  490. }
  491. async shuffle(array) {
  492. try { await this._validateHook(); } catch { return; }
  493. let currentIndex = array.length, temporaryValue, randomIndex;
  494. // While there remain elements to shuffle...
  495. while (0 !== currentIndex) {
  496. // Pick a remaining element...
  497. randomIndex = Math.floor(Math.random() * currentIndex);
  498. currentIndex -= 1;
  499. // And swap it with the current element.
  500. temporaryValue = array[currentIndex];
  501. array[currentIndex] = array[randomIndex];
  502. array[randomIndex] = temporaryValue;
  503. }
  504. return array;
  505. }
  506. async getError(err) {
  507. try { await this._validateHook(); } catch { return; }
  508. let error = 'An error occurred.';
  509. if (typeof err === "string") error = err;
  510. else if (err.message) {
  511. if (err.message !== 'Validation failed') error = err.message;
  512. else error = err.errors[Object.keys(err.errors)].message;
  513. }
  514. return error;
  515. }
  516. async createGravatar(email) {
  517. try { await this._validateHook(); } catch { return; }
  518. const hash = crypto.createHash('md5').update(email).digest('hex');
  519. return `https://www.gravatar.com/avatar/${hash}`;
  520. }
  521. }