my_request.ts 9.4 KB

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