1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- 'use strict';
- import request = require('request');
- var isAuthenticated = false;
- /**
- * Performs a GET request for the resource.
- */
- export function get(config: IConfig, options: request.Options, done: (err: Error, result?: string) => void) {
- authenticate(config, err => {
- if (err) return done(err);
- request.get(modify(options), (err: Error, response: any, body: any) => {
- if (err) return done(err);
- done(null, typeof body === 'string' ? body : String(body));
- });
- });
- }
- /**
- * Performs a POST request for the resource.
- */
- export function post(config: IConfig, options: request.Options, done: (err: Error, result?: string) => void) {
- authenticate(config, err => {
- if (err) return done(err);
- request.post(modify(options), (err: Error, response: any, body: any) => {
- if (err) return done(err);
- done(null, typeof body === 'string' ? body : String(body));
- });
- });
- }
- /**
- * Authenticates using the configured pass and user.
- */
- function authenticate(config: IConfig, done: (err: Error) => void) {
- if (isAuthenticated || !config.pass || !config.user) return done(null);
- var options = {
- form: {
- formname: 'RpcApiUser_Login',
- fail_url: 'https://www.crunchyroll.com/login',
- name: config.user,
- password: config.pass
- },
- jar: true,
- url: 'https://www.crunchyroll.com/?a=formhandler'
- };
- request.post(options, (err: Error) => {
- if (err) return done(err);
- isAuthenticated = true;
- done(null);
- });
- }
- /**
- * Modifies the options to use the authenticated cookie jar.
- */
- function modify(options: string|request.Options): request.Options {
- if (typeof options !== 'string') {
- options.jar = true;
- return options;
- }
- return {jar: true, url: options.toString()};
- }
|