youtube.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import async from "async";
  2. import config from "config";
  3. import request from "request";
  4. import CoreClass from "../core";
  5. let YouTubeModule;
  6. class _YouTubeModule extends CoreClass {
  7. // eslint-disable-next-line require-jsdoc
  8. constructor() {
  9. super("youtube", {
  10. concurrency: 1,
  11. priorities: {
  12. GET_PLAYLIST: 11
  13. }
  14. });
  15. YouTubeModule = this;
  16. }
  17. /**
  18. * Initialises the activities module
  19. *
  20. * @returns {Promise} - returns promise (reject, resolve)
  21. */
  22. initialize() {
  23. return new Promise(resolve => {
  24. resolve();
  25. });
  26. }
  27. /**
  28. * Gets the details of a song using the YouTube API
  29. *
  30. * @param {object} payload - object that contains the payload
  31. * @param {string} payload.songId - the YouTube API id of the song
  32. * @returns {Promise} - returns promise (reject, resolve)
  33. */
  34. GET_SONG(payload) {
  35. // songId, cb
  36. return new Promise((resolve, reject) => {
  37. const youtubeParams = [
  38. "part=snippet,contentDetails,statistics,status",
  39. `id=${encodeURIComponent(payload.songId)}`,
  40. `key=${config.get("apis.youtube.key")}`
  41. ].join("&");
  42. request(`https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`, (err, res, body) => {
  43. if (err) {
  44. YouTubeModule.log("ERROR", "GET_SONG", `${err.message}`);
  45. return reject(new Error("An error has occured. Please try again later."));
  46. }
  47. body = JSON.parse(body);
  48. if (body.error) {
  49. YouTubeModule.log("ERROR", "GET_SONG", `${body.error.message}`);
  50. return reject(new Error("An error has occured. Please try again later."));
  51. }
  52. if (body.items[0] === undefined)
  53. return reject(new Error("The specified video does not exist or cannot be publicly accessed."));
  54. // TODO Clean up duration converter
  55. let dur = body.items[0].contentDetails.duration;
  56. dur = dur.replace("PT", "");
  57. let duration = 0;
  58. dur = dur.replace(/([\d]*)H/, (v, v2) => {
  59. v2 = Number(v2);
  60. duration = v2 * 60 * 60;
  61. return "";
  62. });
  63. dur = dur.replace(/([\d]*)M/, (v, v2) => {
  64. v2 = Number(v2);
  65. duration += v2 * 60;
  66. return "";
  67. });
  68. // eslint-disable-next-line no-unused-vars
  69. dur = dur.replace(/([\d]*)S/, (v, v2) => {
  70. v2 = Number(v2);
  71. duration += v2;
  72. return "";
  73. });
  74. const song = {
  75. songId: body.items[0].id,
  76. title: body.items[0].snippet.title,
  77. duration
  78. };
  79. return resolve({ song });
  80. });
  81. // songId: payload.songId
  82. });
  83. }
  84. /**
  85. * Returns an array of songs taken from a YouTube playlist
  86. *
  87. * @param {object} payload - object that contains the payload
  88. * @param {boolean} payload.musicOnly - whether to return music videos or all videos in the playlist
  89. * @param {string} payload.url - the url of the YouTube playlist
  90. * @returns {Promise} - returns promise (reject, resolve)
  91. */
  92. GET_PLAYLIST(payload) {
  93. // payload includes: url, musicOnly
  94. return new Promise((resolve, reject) => {
  95. const name = "list".replace(/[\\[]/, "\\[").replace(/[\]]/, "\\]");
  96. const regex = new RegExp(`[\\?&]${name}=([^&#]*)`);
  97. const splitQuery = regex.exec(payload.url);
  98. if (!splitQuery) {
  99. YouTubeModule.log("ERROR", "GET_PLAYLIST", "Invalid YouTube playlist URL query.");
  100. return reject(new Error("Invalid playlist URL."));
  101. }
  102. const playlistId = splitQuery[1];
  103. return async.waterfall(
  104. [
  105. next => {
  106. let songs = [];
  107. let nextPageToken = "";
  108. async.whilst(
  109. next => {
  110. YouTubeModule.log(
  111. "INFO",
  112. `Getting playlist progress for job (${this.toString()}): ${
  113. songs.length
  114. } songs gotten so far. Is there a next page: ${nextPageToken !== undefined}.`
  115. );
  116. next(null, nextPageToken !== undefined);
  117. },
  118. next => {
  119. // Add 250ms delay between each job request
  120. setTimeout(() => {
  121. YouTubeModule.runJob("GET_PLAYLIST_PAGE", { playlistId, nextPageToken }, this)
  122. // eslint-disable-next-line no-loop-func
  123. .then(response => {
  124. songs = songs.concat(response.songs);
  125. nextPageToken = response.nextPageToken;
  126. next();
  127. })
  128. // eslint-disable-next-line no-loop-func
  129. .catch(err => {
  130. next(err);
  131. });
  132. }, 250);
  133. },
  134. err => {
  135. next(err, songs);
  136. }
  137. );
  138. },
  139. (songs, next) => {
  140. next(
  141. null,
  142. songs.map(song => song.contentDetails.videoId)
  143. );
  144. },
  145. (songs, next) => {
  146. if (!payload.musicOnly) return next(true, { songs });
  147. return YouTubeModule.runJob(
  148. "FILTER_MUSIC_VIDEOS",
  149. {
  150. videoIds: songs.slice()
  151. },
  152. this
  153. )
  154. .then(filteredSongs => {
  155. next(null, { filteredSongs, songs });
  156. })
  157. .catch(next);
  158. }
  159. ],
  160. (err, response) => {
  161. if (err && err !== true) {
  162. YouTubeModule.log("ERROR", "GET_PLAYLIST", "Some error has occurred.", err.message);
  163. reject(new Error("Some error has occurred."));
  164. } else {
  165. resolve({ songs: response.filteredSongs ? response.filteredSongs.songIds : response.songs });
  166. }
  167. }
  168. );
  169. });
  170. }
  171. /**
  172. * Returns a a page from a YouTube playlist. Is used internally by GET_PLAYLIST.
  173. *
  174. * @param {object} payload - object that contains the payload
  175. * @param {boolean} payload.playlistId - the playlist id to get videos from
  176. * @param {boolean} payload.nextPageToken - the nextPageToken to use
  177. * @param {string} payload.url - the url of the YouTube playlist
  178. * @returns {Promise} - returns promise (reject, resolve)
  179. */
  180. GET_PLAYLIST_PAGE(payload) {
  181. // payload includes: playlistId, nextPageToken
  182. return new Promise((resolve, reject) => {
  183. const nextPageToken = payload.nextPageToken ? `pageToken=${payload.nextPageToken}` : "";
  184. const videosPerPage = 50;
  185. const youtubeParams = [
  186. "part=contentDetails",
  187. `playlistId=${encodeURIComponent(payload.playlistId)}`,
  188. `maxResults=${videosPerPage}`,
  189. `key=${config.get("apis.youtube.key")}`,
  190. nextPageToken
  191. ].join("&");
  192. request(`https://www.googleapis.com/youtube/v3/playlistItems?${youtubeParams}`, async (err, res, body) => {
  193. if (err) {
  194. YouTubeModule.log("ERROR", "GET_PLAYLIST_PAGE", `${err.message}`);
  195. return reject(new Error("An error has occured. Please try again later."));
  196. }
  197. body = JSON.parse(body);
  198. if (body.error) {
  199. YouTubeModule.log("ERROR", "GET_PLAYLIST_PAGE", `${body.error.message}`);
  200. return reject(new Error("An error has occured. Please try again later."));
  201. }
  202. const songs = body.items;
  203. if (body.nextPageToken) return resolve({ nextPageToken: body.nextPageToken, songs });
  204. return resolve({ songs });
  205. });
  206. });
  207. }
  208. /**
  209. * Filters a list of YouTube videos so that they only contains videos with music. Is used internally by GET_PLAYLIST
  210. *
  211. * @param {object} payload - object that contains the payload
  212. * @param {Array} payload.videoIds - an array of YouTube videoIds to filter through
  213. * @param {Array} payload.page - the current page/set of video's to get, starting at 0. If left null, 0 is assumed. Will recurse.
  214. * @returns {Promise} - returns promise (reject, resolve)
  215. */
  216. FILTER_MUSIC_VIDEOS(payload) {
  217. return new Promise((resolve, reject) => {
  218. const page = payload.page ? payload.page : 0;
  219. const videosPerPage = 50; // 50 is the max I believe
  220. const localVideoIds = payload.videoIds.splice(page * 50, videosPerPage);
  221. if (localVideoIds.length === 0) {
  222. return resolve({ videoIds: [] });
  223. }
  224. const youtubeParams = [
  225. "part=topicDetails",
  226. `id=${encodeURIComponent(localVideoIds.join(","))}`,
  227. `maxResults=${videosPerPage}`,
  228. `key=${config.get("apis.youtube.key")}`
  229. ].join("&");
  230. return request(`https://www.googleapis.com/youtube/v3/videos?${youtubeParams}`, (err, res, body) => {
  231. if (err) {
  232. YouTubeModule.log("ERROR", "FILTER_MUSIC_VIDEOS", `${err.message}`);
  233. return reject(new Error("Failed to find playlist from YouTube"));
  234. }
  235. body = JSON.parse(body);
  236. if (body.error) {
  237. YouTubeModule.log("ERROR", "FILTER_MUSIC_VIDEOS", `${body.error.message}`);
  238. return reject(new Error("An error has occured. Please try again later."));
  239. }
  240. const songIds = [];
  241. body.items.forEach(item => {
  242. const songId = item.id;
  243. if (!item.topicDetails) return;
  244. if (item.topicDetails.relevantTopicIds.indexOf("/m/04rlf") !== -1) {
  245. songIds.push(songId);
  246. }
  247. });
  248. return YouTubeModule.runJob("FILTER_MUSIC_VIDEOS", { videoIds: payload.videoIds, page: page + 1 }, this)
  249. .then(result => {
  250. resolve({ songIds: songIds.concat(result.songIds) });
  251. })
  252. .catch(err => {
  253. reject(err);
  254. });
  255. });
  256. });
  257. }
  258. }
  259. export default new _YouTubeModule();