5 Commits

Author SHA1 Message Date
Godzil
2853334d7f 1.1.11
- Update login mechanism to march CR september 2016 changes
2016-09-16 22:20:43 +01:00
Godzil
69dd28d31b Update login to match latest CR changes 2016-09-16 22:20:39 +01:00
Godzil
56afce02ea 1.1.10
- Change name format to follow Plex forvourite one.
- Remplace ":" in file name to prevent issue on Windows
2016-09-10 20:22:10 +01:00
Godzil
bc4697061e Remplace ':' in filename to make Windows happy 2016-09-10 20:17:19 +01:00
Godzil
55ffe85f77 Make the name to be more Plex friendly 2016-09-10 11:53:45 +01:00
3 changed files with 77 additions and 18 deletions

View File

@@ -12,7 +12,7 @@
"type": "git",
"url": "git://github.com/Godzil/crunchyroll.js.git"
},
"version": "1.1.9",
"version": "1.1.11",
"bin": {
"crunchy": "./bin/crunchy"
},

View File

@@ -51,8 +51,8 @@ function fileExist(path: string) {
*/
function download(config: IConfig, page: IEpisodePage, player: IEpisodePlayer, done: (err: Error, ign: boolean) => void) {
var series = config.series || page.series;
series = series.replace("/","_").replace("'","_");
var fileName = name(config, page, series, "").replace("/","_").replace("'","_");
series = series.replace("/","_").replace("'","_").replace(":","_");
var fileName = name(config, page, series, "").replace("/","_").replace("'","_").replace(":","_");
var filePath = path.join(config.output || process.cwd(), series, fileName);
if (fileExist(filePath + ".mkv"))
{
@@ -61,7 +61,7 @@ function download(config: IConfig, page: IEpisodePage, player: IEpisodePlayer, d
do
{
count = count + 1;
fileName = name(config, page, series, "-" + count).replace("/","_").replace("'","_");
fileName = name(config, page, series, "-" + count).replace("/","_").replace("'","_").replace(":","_");
filePath = path.join(config.output || process.cwd(), series, fileName);
} while(fileExist(filePath + ".mkv"))
console.info("Renaming to '"+fileName+"'...");
@@ -137,7 +137,7 @@ function name(config: IConfig, page: IEpisodePage, series: string, extra: string
var episode = (episodeNum < 10 ? '0' : '') + page.episode;
var volume = (volumeNum < 10 ? '0' : '') + page.volume;
var tag = config.tag || 'CrunchyRoll';
return series + ' ' + volume + 'x' + episode + extra + ' [' + tag + ']';
return series + ' - s' + volume + 'e' + episode +' - [' + tag + ']' + extra;
}
/**

View File

@@ -1,6 +1,14 @@
'use strict';
import request = require('request');
import cheerio = require('cheerio');
var isAuthenticated = false;
var isPremium = false;
var defaultHeaders:request.Headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0',
'Connection': 'keep-alive'
};
/**
* Performs a GET request for the resource.
@@ -33,21 +41,71 @@ export function post(config: IConfig, options: request.Options, done: (err: Erro
*/
function authenticate(config: IConfig, done: (err: Error) => void) {
if (isAuthenticated || !config.pass || !config.user) return done(null);
/* First just request the login page */
var options = {
form: {
formname: 'RpcApiUser_Login',
fail_url: 'https://www.crunchyroll.com/login',
name: config.user,
password: config.pass
},
headers: defaultHeaders,
jar: true,
url: 'https://www.crunchyroll.com/?a=formhandler'
};
request.post(options, (err: Error) => {
url: 'https://www.crunchyroll.com/login'
}
request(options, (err: Error, rep: any, body: any) =>
{
if (err) return done(err);
isAuthenticated = true;
done(null);
});
var $ = cheerio.load(body);
/* Get the token from the login page */
var token = $('input[name="login_form[_token]"]').attr("value");
if (token == "") return done(new Error("Can't find token!"));
var options =
{
headers: defaultHeaders,
form:
{
'login_form[redirect_url]': '/',
'login_form[name]': config.user,
'login_form[password]': config.pass,
'login_form[_token]': token,
},
jar: true,
gzip: false,
url: 'https://www.crunchyroll.com/login'
};
request.post(options, (err: Error, rep: string, body: string) =>
{
if (err) return done(err);
/* The page return with a meta based redirection, as we wan't to check that everything is fine, reload
* the main page. A bit convoluted, but more sure.
*/
var options =
{
headers: defaultHeaders,
jar: true,
url: 'http://www.crunchyroll.com/'
}
request(options, (err: Error, rep: string, body: string) =>
{
if (err) return done(err);
var $ = cheerio.load(body);
/* Check if auth worked */
var regexps = /ga\(\'set\', \'dimension[5-8]\', \'([^']*)\'\);/g
var dims = regexps.exec($('script').text())
for(var i = 1; i < 5; i++)
{
if ((dims[i] != undefined) && (dims[i] != "") && (dims[i] != "not-registered")) { isAuthenticated = true; }
if ((dims[i] == "premium") || (dims[i] == "premiumplus")) { isPremium = true; }
}
if (isAuthenticated == false)
{
var error = $('ul.message, li.error').text();
return done(new Error("Authentication failed: " + error));
}
if (isPremium == false) { console.info("Do not use this app without a premium account."); }
else { console.info("You have a premium account! Good!"); }
done(null);
})
})
})
}
/**
@@ -56,7 +114,8 @@ function authenticate(config: IConfig, done: (err: Error) => void) {
function modify(options: string|request.Options): request.Options {
if (typeof options !== 'string') {
options.jar = true;
options.headers = defaultHeaders;
return options;
}
return {jar: true, url: options.toString()};
return {jar: true, headers: defaultHeaders, url: options.toString()};
}