utils.js 14 KB

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