my_request.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. /**
  152. * Performs a GET request for the resource.
  153. */
  154. export function get(config: IConfig, options: string|request.Options, done: (err: any, result?: string) => void)
  155. {
  156. if (j === undefined)
  157. {
  158. loadCookies(config);
  159. }
  160. authenticate(config, (err) =>
  161. {
  162. if (err)
  163. {
  164. return done(err);
  165. }
  166. cloudscraper.request(modify(options, 'GET'), (error: any, response: any, body: any) =>
  167. {
  168. if (error) return done(error);
  169. done(null, typeof body === 'string' ? body : String(body));
  170. });
  171. });
  172. }
  173. /**
  174. * Performs a POST request for the resource.
  175. */
  176. export function post(config: IConfig, options: request.Options, done: (err: Error, result?: string) => void)
  177. {
  178. if (j === undefined)
  179. {
  180. loadCookies(config);
  181. }
  182. authenticate(config, (err) =>
  183. {
  184. if (err)
  185. {
  186. return done(err);
  187. }
  188. cloudscraper.request(modify(options, 'POST'), (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. /**
  199. * Authenticates using the configured pass and user.
  200. */
  201. function authenticate(config: IConfig, done: (err: Error) => void)
  202. {
  203. if (isAuthenticated)
  204. {
  205. return done(null);
  206. }
  207. /* First of all, check if the user is not already logged via the cookies */
  208. checkIfUserIsAuth(config, (errCheckAuth) =>
  209. {
  210. if (isAuthenticated)
  211. {
  212. return done(null);
  213. }
  214. /* So if we are here now, that mean we are not authenticated so do as usual */
  215. if (!config.pass || !config.user)
  216. {
  217. log.error('You need to give login/password to use Crunchy');
  218. process.exit(-1);
  219. }
  220. if (config.logUsingApi)
  221. {
  222. if (config.crDeviceId === undefined)
  223. {
  224. config.crDeviceId = uuid.v4();
  225. }
  226. if (!config.crSessionUrl || !config.crDeviceType || !config.crAPIVersion ||
  227. !config.crLocale || !config.crLoginUrl)
  228. {
  229. return done(AuthError('Invalid API configuration, please check your config file.'));
  230. }
  231. startSession(config)
  232. .then((sessionId: string) =>
  233. {
  234. defaultHeaders.Cookie = `sess_id=${sessionId}; c_locale=enUS`;
  235. return login(config, sessionId, config.user, config.pass);
  236. })
  237. .then((userData) =>
  238. {
  239. checkIfUserIsAuth(config, (errCheckAuth2) =>
  240. {
  241. if (isAuthenticated)
  242. {
  243. return done(null);
  244. }
  245. else
  246. {
  247. return done(errCheckAuth2);
  248. }
  249. });
  250. })
  251. .catch((errInChk) =>
  252. {
  253. return done(AuthError(errInChk.message));
  254. });
  255. }
  256. else if (config.logUsingCookie)
  257. {
  258. j.setCookie(request.cookie('c_userid=' + config.crUserId + '; Domain=crunchyroll.com; HttpOnly; hostOnly=false;'),
  259. CR_COOKIE_DOMAIN);
  260. j.setCookie(request.cookie('c_userkey=' + config.crUserKey + '; Domain=crunchyroll.com; HttpOnly; hostOnly=false;'),
  261. CR_COOKIE_DOMAIN);
  262. checkIfUserIsAuth(config, (errCheckAuth2) =>
  263. {
  264. if (isAuthenticated)
  265. {
  266. return done(null);
  267. }
  268. else
  269. {
  270. return done(errCheckAuth2);
  271. }
  272. });
  273. }
  274. else
  275. {
  276. log.error('This method of login is currently unsupported...\n');
  277. return done(AuthError('Unsupported login method'));
  278. }
  279. });
  280. }
  281. /**
  282. * Modifies the options to use the authenticated cookie jar.
  283. */
  284. function modify(options: string|request.Options, reqMethod: string): request.Options
  285. {
  286. if (typeof options !== 'string')
  287. {
  288. options.jar = j;
  289. options.headers = defaultHeaders;
  290. options.method = reqMethod;
  291. return options;
  292. }
  293. return {
  294. jar: j,
  295. headers: defaultHeaders,
  296. url: options.toString(),
  297. method: reqMethod
  298. };
  299. }