my_request.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. 'use strict';
  2. import cheerio = require('cheerio');
  3. import request = require('request');
  4. import rp = require('request-promise');
  5. import Promise = require('bluebird');
  6. import uuid = require('uuid');
  7. import path = require('path');
  8. import fs = require('fs-extra');
  9. import languages = require('./languages');
  10. import log = require('./log');
  11. import { RequestPromise } from 'request-promise';
  12. import { Response } from 'request';
  13. // tslint:disable-next-line:no-var-requires
  14. const cookieStore = require('tough-cookie-file-store');
  15. // tslint:disable-next-line:no-var-requires
  16. const cloudscraper = require('cloudscraper');
  17. const CR_COOKIE_DOMAIN = 'http://crunchyroll.com';
  18. let isAuthenticated = false;
  19. let isPremium = false;
  20. let j: request.CookieJar;
  21. const defaultHeaders: request.Headers =
  22. {
  23. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
  24. 'Connection': 'keep-alive',
  25. 'Referer': 'https://www.crunchyroll.com/login',
  26. };
  27. function AuthError(msg: string): IAuthError
  28. {
  29. return { name: 'AuthError', message: msg, authError: true };
  30. }
  31. function startSession(config: IConfig): Promise<any>
  32. {
  33. return rp(
  34. {
  35. method: 'GET',
  36. url: config.crSessionUrl,
  37. qs:
  38. {
  39. device_id: config.crDeviceId,
  40. device_type: config.crDeviceType,
  41. access_token: config.crSessionKey,
  42. version: config.crAPIVersion,
  43. locale: config.crLocale,
  44. },
  45. json: true,
  46. })
  47. .then((response: any) =>
  48. {
  49. if ((response.data === undefined) || (response.data.session_id === undefined))
  50. {
  51. throw new Error('Getting session failed: ' + JSON.stringify(response));
  52. }
  53. return response.data.session_id;
  54. });
  55. }
  56. function login(config: IConfig, sessionId: string, user: string, pass: string): Promise<any>
  57. {
  58. return rp(
  59. {
  60. method: 'POST',
  61. url: config.crLoginUrl,
  62. form:
  63. {
  64. account: user,
  65. password: pass,
  66. session_id: sessionId,
  67. version: config.crAPIVersion,
  68. },
  69. json: true,
  70. jar: j,
  71. })
  72. .then((response) =>
  73. {
  74. if (response.error) throw new Error('Login failed: ' + response.message);
  75. return response.data;
  76. });
  77. }
  78. function checkIfUserIsAuth(config: IConfig, done: (err: Error) => void): void
  79. {
  80. if (j === undefined)
  81. {
  82. loadCookies(config);
  83. }
  84. /**
  85. * The main page give us some information about the user
  86. */
  87. const options =
  88. {
  89. headers: defaultHeaders,
  90. jar: j,
  91. url: 'http://www.crunchyroll.com/',
  92. method: 'GET',
  93. };
  94. cloudscraper.request(options, (err: Error, rep: string, body: string) =>
  95. {
  96. if (err)
  97. {
  98. return done(err);
  99. }
  100. const $ = cheerio.load(body);
  101. /* As we are here, try to detect which locale CR tell us */
  102. const localeRE = /LOCALE = "([a-zA-Z]+)",/g;
  103. const locale = localeRE.exec($('script').text())[1];
  104. const countryCode = languages.localeToCC(locale);
  105. if (config.crlang === undefined)
  106. {
  107. log.info('No locale set. Setting to the one reported by CR: "' + countryCode + '"');
  108. config.crlang = countryCode;
  109. }
  110. else if (config.crlang !== countryCode)
  111. {
  112. log.warn('Crunchy is configured for locale "' + config.crlang + '" but CR report "' + countryCode + '" (LOCALE = ' + locale + ')');
  113. log.warn('Check if it is correct or rerun (once) with "-l ' + countryCode + '" to correct.');
  114. }
  115. /* Check if auth worked */
  116. const regexps = /ga\('set', 'dimension[5-8]', '([^']*)'\);/g;
  117. const dims = regexps.exec($('script').text());
  118. for (let i = 1; i < 5; i++)
  119. {
  120. if ((dims[i] !== undefined) && (dims[i] !== '') && (dims[i] !== 'not-registered'))
  121. {
  122. isAuthenticated = true;
  123. }
  124. if ((dims[i] === 'premium') || (dims[i] === 'premiumplus'))
  125. {
  126. isPremium = true;
  127. }
  128. }
  129. if (isAuthenticated === false)
  130. {
  131. const error = $('ul.message, li.error').text();
  132. return done(AuthError('Authentication failed: ' + error));
  133. }
  134. else
  135. {
  136. if (isPremium === false)
  137. {
  138. log.warn('Do not use this app without a premium account.');
  139. }
  140. else
  141. {
  142. log.info('You have a premium account! Good!');
  143. }
  144. }
  145. done(null);
  146. });
  147. }
  148. function loadCookies(config: IConfig)
  149. {
  150. const cookiePath = path.join(config.output || process.cwd(), '.cookies.json');
  151. if (!fs.existsSync(cookiePath))
  152. {
  153. fs.closeSync(fs.openSync(cookiePath, 'w'));
  154. }
  155. j = request.jar(new cookieStore(cookiePath));
  156. }
  157. export function eatCookies(config: IConfig)
  158. {
  159. const cookiePath = path.join(config.output || process.cwd(), '.cookies.json');
  160. if (fs.existsSync(cookiePath))
  161. {
  162. fs.removeSync(cookiePath);
  163. }
  164. j = undefined;
  165. }
  166. export function getUserAgent(): string
  167. {
  168. return defaultHeaders['User-Agent'];
  169. }
  170. /**
  171. * Performs a GET request for the resource.
  172. */
  173. export function get(config: IConfig, options: string|request.Options, done: (err: any, result?: string) => void)
  174. {
  175. if (j === undefined)
  176. {
  177. loadCookies(config);
  178. }
  179. if (config.userAgent)
  180. {
  181. defaultHeaders['User-Agent'] = config.userAgent;
  182. }
  183. authenticate(config, (err) =>
  184. {
  185. if (err)
  186. {
  187. return done(err);
  188. }
  189. cloudscraper.request(modify(options, 'GET'), (error: any, response: any, body: any) =>
  190. {
  191. if (error) return done(error);
  192. done(null, typeof body === 'string' ? body : String(body));
  193. });
  194. });
  195. }
  196. /**
  197. * Performs a POST request for the resource.
  198. */
  199. export function post(config: IConfig, options: request.Options, done: (err: Error, result?: string) => void)
  200. {
  201. if (j === undefined)
  202. {
  203. loadCookies(config);
  204. }
  205. if (config.userAgent)
  206. {
  207. defaultHeaders['User-Agent'] = config.userAgent;
  208. }
  209. authenticate(config, (err) =>
  210. {
  211. if (err)
  212. {
  213. return done(err);
  214. }
  215. cloudscraper.request(modify(options, 'POST'), (error: Error, response: any, body: any) =>
  216. {
  217. if (error)
  218. {
  219. return done(error);
  220. }
  221. done(null, typeof body === 'string' ? body : String(body));
  222. });
  223. });
  224. }
  225. /**
  226. * Authenticates using the configured pass and user.
  227. */
  228. function authenticate(config: IConfig, done: (err: Error) => void)
  229. {
  230. if (isAuthenticated)
  231. {
  232. return done(null);
  233. }
  234. /* First of all, check if the user is not already logged via the cookies */
  235. checkIfUserIsAuth(config, (errCheckAuth) =>
  236. {
  237. if (isAuthenticated)
  238. {
  239. return done(null);
  240. }
  241. /* So if we are here now, that mean we are not authenticated so do as usual */
  242. if (!config.pass || !config.user)
  243. {
  244. log.error('You need to give login/password to use Crunchy');
  245. process.exit(-1);
  246. }
  247. log.info('Seems we are not currently logged. Let\'s login!');
  248. if (config.logUsingApi)
  249. {
  250. if (config.crDeviceId === undefined)
  251. {
  252. config.crDeviceId = uuid.v4();
  253. }
  254. if (!config.crSessionUrl || !config.crDeviceType || !config.crAPIVersion ||
  255. !config.crLocale || !config.crLoginUrl)
  256. {
  257. return done(AuthError('Invalid API configuration, please check your config file.'));
  258. }
  259. startSession(config)
  260. .then((sessionId: string) =>
  261. {
  262. defaultHeaders.Cookie = `sess_id=${sessionId}; c_locale=enUS`;
  263. return login(config, sessionId, config.user, config.pass);
  264. })
  265. .then((userData) =>
  266. {
  267. checkIfUserIsAuth(config, (errCheckAuth2) =>
  268. {
  269. if (isAuthenticated)
  270. {
  271. return done(null);
  272. }
  273. else
  274. {
  275. return done(errCheckAuth2);
  276. }
  277. });
  278. })
  279. .catch((errInChk) =>
  280. {
  281. return done(AuthError(errInChk.message));
  282. });
  283. }
  284. else if (config.logUsingCookie)
  285. {
  286. j.setCookie(request.cookie('c_userid=' + config.crUserId + '; Domain=crunchyroll.com; HttpOnly; hostOnly=false;'),
  287. CR_COOKIE_DOMAIN);
  288. j.setCookie(request.cookie('c_userkey=' + config.crUserKey + '; Domain=crunchyroll.com; HttpOnly; hostOnly=false;'),
  289. CR_COOKIE_DOMAIN);
  290. checkIfUserIsAuth(config, (errCheckAuth2) =>
  291. {
  292. if (isAuthenticated)
  293. {
  294. return done(null);
  295. }
  296. else
  297. {
  298. return done(errCheckAuth2);
  299. }
  300. });
  301. }
  302. else
  303. {
  304. /* First get https://www.crunchyroll.com/login to get the login token */
  305. const options =
  306. {
  307. headers: defaultHeaders,
  308. jar: j,
  309. gzip: false,
  310. method: 'GET',
  311. url: 'https://www.crunchyroll.com/login'
  312. };
  313. cloudscraper.request(options, (err: Error, rep: string, body: string) =>
  314. {
  315. if (err) return done(err);
  316. const $ = cheerio.load(body);
  317. /* Get the token from the login page */
  318. const token = $('input[name="login_form[_token]"]').attr('value');
  319. if (token === '')
  320. {
  321. return done(AuthError('Can\'t find token!'));
  322. }
  323. /* Now call the page again with the token and credentials */
  324. const options =
  325. {
  326. headers: defaultHeaders,
  327. form:
  328. {
  329. 'login_form[name]': config.user,
  330. 'login_form[password]': config.pass,
  331. 'login_form[redirect_url]': '/',
  332. 'login_form[_token]': token
  333. },
  334. jar: j,
  335. gzip: false,
  336. method: 'POST',
  337. url: 'https://www.crunchyroll.com/login'
  338. };
  339. cloudscraper.request(options, (err: Error, rep: string, body: string) =>
  340. {
  341. if (err)
  342. {
  343. return done(err);
  344. }
  345. /* Now let's check if we are authentificated */
  346. checkIfUserIsAuth(config, (errCheckAuth2) =>
  347. {
  348. if (isAuthenticated)
  349. {
  350. return done(null);
  351. }
  352. else
  353. {
  354. return done(errCheckAuth2);
  355. }
  356. });
  357. });
  358. });
  359. }
  360. });
  361. }
  362. /**
  363. * Modifies the options to use the authenticated cookie jar.
  364. */
  365. function modify(options: string|request.Options, reqMethod: string): request.Options
  366. {
  367. if (typeof options !== 'string')
  368. {
  369. options.jar = j;
  370. options.headers = defaultHeaders;
  371. options.method = reqMethod;
  372. options.followAllRedirects = true;
  373. return options;
  374. }
  375. return {
  376. jar: j,
  377. headers: defaultHeaders,
  378. url: options.toString(),
  379. method: reqMethod
  380. };
  381. }