request.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. import request = require('request');
  3. import cheerio = require('cheerio');
  4. var isAuthenticated = false;
  5. var isPremium = false;
  6. var defaultHeaders: request.Headers = {
  7. 'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
  8. 'Connection': 'keep-alive'
  9. };
  10. /**
  11. * Performs a GET request for the resource.
  12. */
  13. export function get(config: IConfig, options: request.Options, done: (err: Error, result?: string) => void) {
  14. authenticate(config, err => {
  15. if (err) return done(err);
  16. request.get(modify(options), (err: Error, response: any, body: any) => {
  17. if (err) return done(err);
  18. done(null, typeof body === 'string' ? body : String(body));
  19. });
  20. });
  21. }
  22. /**
  23. * Performs a POST request for the resource.
  24. */
  25. export function post(config: IConfig, options: request.Options, done: (err: Error, result?: string) => void) {
  26. authenticate(config, err => {
  27. if (err) return done(err);
  28. request.post(modify(options), (err: Error, response: any, body: any) => {
  29. if (err) return done(err);
  30. done(null, typeof body === 'string' ? body : String(body));
  31. });
  32. });
  33. }
  34. /**
  35. * Authenticates using the configured pass and user.
  36. */
  37. function authenticate(config: IConfig, done: (err: Error) => void) {
  38. if (isAuthenticated || !config.pass || !config.user) return done(null);
  39. /* Bypass the login page and send a login request directly */
  40. var options =
  41. {
  42. headers: defaultHeaders,
  43. jar: true,
  44. gzip: false,
  45. url: 'https://www.crunchyroll.com/?a=formhandler&formname=RpcApiUser_Login&name=' + config.user + '&password=' + config.pass
  46. };
  47. request(options, (err: Error, rep: string, body: string) =>
  48. {
  49. if (err) return done(err);
  50. /* The page return with a meta based redirection, as we wan't to check that everything is fine, reload
  51. * the main page. A bit convoluted, but more sure.
  52. */
  53. var options =
  54. {
  55. headers: defaultHeaders,
  56. jar: true,
  57. url: 'http://www.crunchyroll.com/'
  58. };
  59. request(options, (err: Error, rep: string, body: string) =>
  60. {
  61. if (err) return done(err);
  62. var $ = cheerio.load(body);
  63. /* Check if auth worked */
  64. var regexps = /ga\(\'set\', \'dimension[5-8]\', \'([^']*)\'\);/g;
  65. var dims = regexps.exec($('script').text());
  66. for (var i = 1; i < 5; i++)
  67. {
  68. if ((dims[i] !== undefined) && (dims[i] !== '') && (dims[i] !== 'not-registered')) { isAuthenticated = true; }
  69. if ((dims[i] === 'premium') || (dims[i] === 'premiumplus')) { isPremium = true; }
  70. }
  71. if (isAuthenticated === false)
  72. {
  73. var error = $('ul.message, li.error').text();
  74. return done(new Error('Authentication failed: ' + error));
  75. }
  76. if (isPremium === false) { console.log('Do not use this app without a premium account.'); }
  77. else { console.log('You have a premium account! Good!'); }
  78. done(null);
  79. });
  80. });
  81. }
  82. /**
  83. * Modifies the options to use the authenticated cookie jar.
  84. */
  85. function modify(options: string|request.Options): request.Options {
  86. if (typeof options !== 'string') {
  87. options.jar = true;
  88. options.headers = defaultHeaders;
  89. return options;
  90. }
  91. return {jar: true, headers: defaultHeaders, url: options.toString()};
  92. }