-
Notifications
You must be signed in to change notification settings - Fork 45
/
kissanime.js
152 lines (127 loc) · 4.61 KB
/
kissanime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// python -m http.server
// http://localhost:8000/kissanime.js
// CONFIG
var siteName = "Kissanime"
var rootUrl = 'http://kissanime.ru'
var URL = window.location.origin
// END CONFIG
var episodeLinks = $('table.listing a').map(function(i,el) { return $(el).attr('href'); });
var episodeNames = $('table.listing a').map(function(i,el) { return $.trim( $(el).html() ); });
$.ajaxSetup({async:false});
$.getScript(rootUrl + "/Scripts/asp.js");
console.log('Starting ' + siteName + ' Batch Downloader script...');
var startEpisode;
do {
startEpisode = Number(prompt("Enter episode (listing) number you want to start from", defaultText="1"));
if(startEpisode <= 0 || startEpisode > episodeLinks.length) {
alert("Episode number entered must be greater than 0 and lesser than total number of eps");
} else {
break;
}
} while(true);
var endEpisode;
do {
endEpisode = Number(prompt("Enter episode (listing) number you want to end at", defaultText="2"));
if(endEpisode <= 0 || endEpisode > episodeLinks.length || endEpisode < startEpisode) {
alert("Episode number entered must be greater than 0 and lesser than total number of eps");
} else {
break;
}
} while(true);
var videoQuality = prompt(
"Enter video quality preferences for the download. Example - '720,480'\nThis first looks for 720p, if 720 is not available, it picks 480.",
defaultText="720,480"
);
if (videoQuality == null){
videoQuality = "720,480,360";
}
var opOptions = prompt(
"How do you want output to be?\n0 = simple list of links\n1 = List with filenames (for wget, aria2 helper scripts)\n2 = HTML page with links",
defaultText="0"
);
if (opOptions == null){
opOptions = "0";
}
var i;
var linkStr = "";
console.log('Starting to fetch links..');
for (i = (episodeLinks.length - startEpisode); i >= (episodeLinks.length - endEpisode); i--) {
console.log('Fetching listing ' + (episodeLinks.length - i) + ' [' + episodeNames[i] + ']');
jQuery.ajax({
url: URL + episodeLinks[i],
tryCount : 0,
retryLimit : 3,
success: function(result) {
var $result = eval($(result));
// console.log(result.search("Save link as"));
// console.log(result.search("divDownload"));
var data = $(result).find("#divDownload"); // download data
var links = $(data[0]).find("a");
if (data == null || data == "" || data.length == 0){ // captcha maybe
console.log("Captcha detected at " + URL + episodeLinks[i]);
prompt("Captcha detected. Solve it by opening the link below in a new tab. After solving, press OK.",
defaultText=URL + episodeLinks[i]);
this.tryCount++;
$.ajax(this); // retry
return;
}
// console.log(links);
var quals = videoQuality.split(',');
var found = false;
// pick download
for (var j=0; j<quals.length; j++){
// check if the format exists or not
if (found)
return;
$.each(links, function(index, el) {
// console.log(el);
if ( $(el).html().search(quals[j]) > -1 ){
long_url = $(el).attr('href');
name = getDownloadName(episodeNames[i], $(el).html());
if (opOptions == "1"){
linkStr += encodeURI(long_url) + " " + name + "\n";
} else if (opOptions == "2"){
linkStr += '<a href="' + long_url + '" download="' + name + '">' + name + '</a><br>';
} else {
linkStr += long_url + "\n";
}
found = true;
// console.log('Episode ' + (episodeLinks.length - i));
console.log(long_url);
}
});
}
// successful response processed
},
error: function(xhr, textStatus, errorThrown ) {
console.log(textStatus)
// http://stackoverflow.com/questions/10024469/whats-the-best-way-to-retry-an-ajax-request-on-failure-using-jquery
this.tryCount++;
if (this.tryCount <= this.retryLimit) {
//try again
console.log('Retrying..');
$.ajax(this);
}
return;
},
async: false,
script: true
});
}
console.log('Opening list of links')
download("links." + ((opOptions == '2') ? 'html' : 'txt'), (opOptions == '2') ? 'text/html' : 'text/plain', linkStr)
// http://ourcodeworld.com/articles/read/189/how-to-create-a-file-and-generate-a-download-with-javascript-in-the-browser-without-a-server
function download(filename, datatype, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:' + datatype + ';charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
// element.setAttribute('target', '_blank');
// ^^ problems with safari
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
function getDownloadName(epName, dl){
return (epName + "__" + dl).replace(/\s/g, '_');
}