utils.js 12 KB

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