my_request.ts 9.7 KB

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