utils.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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) {
  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. let args = [];
  261. for (let i = 1; i < Object.keys(arguments).length; i++) {
  262. args.push(arguments[i]);
  263. }
  264. socket.emit.apply(socket, args);
  265. }
  266. }
  267. }
  268. async getRoomSockets(room) {
  269. try { await this._validateHook(); } catch { return; }
  270. let sockets = this.io.io.sockets.sockets;
  271. let roomSockets = [];
  272. for (let id in sockets) {
  273. let socket = sockets[id];
  274. if (socket.rooms[room]) roomSockets.push(socket);
  275. }
  276. return roomSockets;
  277. }
  278. async getSongFromYouTube(songId, cb) {
  279. try { await this._validateHook(); } catch { return; }
  280. youtubeRequestCallbacks.push({cb: (test) => {
  281. youtubeRequestsActive = true;
  282. const youtubeParams = [
  283. 'part=snippet,contentDetails,statistics,status',
  284. `id=${encodeURIComponent(songId)}`,
  285. `key=${config.get('apis.youtube.key')}`
  286. ].join('&');
  287. request(`https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`, (err, res, body) => {
  288. youtubeRequestCallbacks.splice(0, 1);
  289. if (youtubeRequestCallbacks.length > 0) {
  290. youtubeRequestCallbacks[0].cb(youtubeRequestCallbacks[0].songId);
  291. } else youtubeRequestsActive = false;
  292. if (err) {
  293. console.error(err);
  294. return null;
  295. }
  296. body = JSON.parse(body);
  297. //TODO Clean up duration converter
  298. let dur = body.items[0].contentDetails.duration;
  299. dur = dur.replace('PT', '');
  300. let duration = 0;
  301. dur = dur.replace(/([\d]*)H/, (v, v2) => {
  302. v2 = Number(v2);
  303. duration = (v2 * 60 * 60);
  304. return '';
  305. });
  306. dur = dur.replace(/([\d]*)M/, (v, v2) => {
  307. v2 = Number(v2);
  308. duration += (v2 * 60);
  309. return '';
  310. });
  311. dur = dur.replace(/([\d]*)S/, (v, v2) => {
  312. v2 = Number(v2);
  313. duration += v2;
  314. return '';
  315. });
  316. let song = {
  317. songId: body.items[0].id,
  318. title: body.items[0].snippet.title,
  319. duration
  320. };
  321. cb(song);
  322. });
  323. }, songId});
  324. if (!youtubeRequestsActive) {
  325. youtubeRequestCallbacks[0].cb(youtubeRequestCallbacks[0].songId);
  326. }
  327. }
  328. async getPlaylistFromYouTube(url, cb) {
  329. try { await this._validateHook(); } catch { return; }
  330. let name = 'list'.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  331. var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
  332. let playlistId = regex.exec(url)[1];
  333. function getPage(pageToken, songs) {
  334. let nextPageToken = (pageToken) ? `pageToken=${pageToken}` : '';
  335. const youtubeParams = [
  336. 'part=contentDetails',
  337. `playlistId=${encodeURIComponent(playlistId)}`,
  338. `maxResults=5`,
  339. `key=${config.get('apis.youtube.key')}`,
  340. nextPageToken
  341. ].join('&');
  342. request(`https://www.googleapis.com/youtube/v3/playlistItems?${youtubeParams}`, (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. songs = songs.concat(body.items);
  349. if (body.nextPageToken) getPage(body.nextPageToken, songs);
  350. else {
  351. console.log(songs);
  352. cb(songs);
  353. }
  354. });
  355. }
  356. getPage(null, []);
  357. }
  358. async getSongFromSpotify(song, cb) {
  359. try { await this._validateHook(); } catch { return; }
  360. if (!config.get("apis.spotify.enabled")) return cb("Spotify is not enabled", null);
  361. const spotifyParams = [
  362. `q=${encodeURIComponent(song.title)}`,
  363. `type=track`
  364. ].join('&');
  365. const token = await this.spotify.getToken();
  366. const options = {
  367. url: `https://api.spotify.com/v1/search?${spotifyParams}`,
  368. headers: {
  369. Authorization: `Bearer ${token}`
  370. }
  371. };
  372. request(options, (err, res, body) => {
  373. if (err) console.error(err);
  374. body = JSON.parse(body);
  375. if (body.error) console.error(body.error);
  376. durationArtistLoop:
  377. for (let i in body) {
  378. let items = body[i].items;
  379. for (let j in items) {
  380. let item = items[j];
  381. let hasArtist = false;
  382. for (let k = 0; k < item.artists.length; k++) {
  383. let artist = item.artists[k];
  384. if (song.title.indexOf(artist.name) !== -1) hasArtist = true;
  385. }
  386. if (hasArtist && song.title.indexOf(item.name) !== -1) {
  387. song.duration = item.duration_ms / 1000;
  388. song.artists = item.artists.map(artist => {
  389. return artist.name;
  390. });
  391. song.title = item.name;
  392. song.explicit = item.explicit;
  393. song.thumbnail = item.album.images[1].url;
  394. break durationArtistLoop;
  395. }
  396. }
  397. }
  398. cb(null, song);
  399. });
  400. }
  401. async getSongsFromSpotify(title, artist, cb) {
  402. try { await this._validateHook(); } catch { return; }
  403. if (!config.get("apis.spotify.enabled")) return cb([]);
  404. const spotifyParams = [
  405. `q=${encodeURIComponent(title)}`,
  406. `type=track`
  407. ].join('&');
  408. const token = await this.spotify.getToken();
  409. const options = {
  410. url: `https://api.spotify.com/v1/search?${spotifyParams}`,
  411. headers: {
  412. Authorization: `Bearer ${token}`
  413. }
  414. };
  415. request(options, (err, res, body) => {
  416. if (err) return console.error(err);
  417. body = JSON.parse(body);
  418. if (body.error) return console.error(body.error);
  419. let songs = [];
  420. for (let i in body) {
  421. let items = body[i].items;
  422. for (let j in items) {
  423. let item = items[j];
  424. let hasArtist = false;
  425. for (let k = 0; k < item.artists.length; k++) {
  426. let localArtist = item.artists[k];
  427. if (artist.toLowerCase() === localArtist.name.toLowerCase()) hasArtist = true;
  428. }
  429. if (hasArtist && (title.indexOf(item.name) !== -1 || item.name.indexOf(title) !== -1)) {
  430. let song = {};
  431. song.duration = item.duration_ms / 1000;
  432. song.artists = item.artists.map(artist => {
  433. return artist.name;
  434. });
  435. song.title = item.name;
  436. song.explicit = item.explicit;
  437. song.thumbnail = item.album.images[1].url;
  438. songs.push(song);
  439. }
  440. }
  441. }
  442. cb(songs);
  443. });
  444. }
  445. async shuffle(array) {
  446. try { await this._validateHook(); } catch { return; }
  447. let currentIndex = array.length, temporaryValue, randomIndex;
  448. // While there remain elements to shuffle...
  449. while (0 !== currentIndex) {
  450. // Pick a remaining element...
  451. randomIndex = Math.floor(Math.random() * currentIndex);
  452. currentIndex -= 1;
  453. // And swap it with the current element.
  454. temporaryValue = array[currentIndex];
  455. array[currentIndex] = array[randomIndex];
  456. array[randomIndex] = temporaryValue;
  457. }
  458. return array;
  459. }
  460. async getError(err) {
  461. try { await this._validateHook(); } catch { return; }
  462. let error = 'An error occurred.';
  463. if (typeof err === "string") error = err;
  464. else if (err.message) {
  465. if (err.message !== 'Validation failed') error = err.message;
  466. else error = err.errors[Object.keys(err.errors)].message;
  467. }
  468. return error;
  469. }
  470. async canUserBeInStation(station, userId, cb) {
  471. try { await this._validateHook(); } catch { return; }
  472. async.waterfall([
  473. (next) => {
  474. if (station.privacy !== 'private') return next(true);
  475. if (!userId) return next(false);
  476. next();
  477. },
  478. (next) => {
  479. this.db.models.user.findOne({_id: userId}, next);
  480. },
  481. (user, next) => {
  482. if (!user) return next(false);
  483. if (user.role === 'admin') return next(true);
  484. if (station.type === 'official') return next(false);
  485. if (station.owner === userId) return next(true);
  486. next(false);
  487. }
  488. ], (err) => {
  489. if (err === true) return cb(true);
  490. return cb(false);
  491. });
  492. }
  493. }