-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (172 loc) · 5.07 KB
/
index.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const fetch = require('node-fetch')
const cheerio = require('cheerio')
const endpoints = require('./endpoints')
class Anime {
/**
* constructor, instantiates the object
* @param {string} prefix - it uses this in front of the request, you could use this to prevent cors errors in browsers
* @param {string} url - the url to make request to, default: http://animepill.com
*/
constructor(prefix = '', url) {
this.base = url || 'http://animepill.com'
this.prefix = prefix
}
// options can look like this
// {limit: 50}
_genOptions(options) {
let i = 0
let res = ''
for (let opt in options) {
if (i === 0) {
res += `?${opt}=${options[opt]}`
} else {
res += `&${opt}=${options[opt]}`
}
i++
}
return res
}
_parsePathParam(path, param) {
const reg = /{(.*?)}/g // get the stuff between the {}
const arr = path.match(reg) || [] // array with the results of the regexp
for (var i = 0; i < arr.length; i++) {
let par = param[arr[i].substr(1, arr[i].length - 2)]
if (par) {
path = path.replace(arr[i], par)
} else {
path = path.replace(arr[i], '')
}
}
path = path.replace(/\/\/+/g, '/') // remove double slashes
return path
}
_get(path, options, param) {
if (
path.search(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/
) != -1
) {
// url is passed
return fetch(path).then(res => res.text())
}
return fetch(
this.prefix +
this.base +
this._parsePathParam(path, param) +
this._genOptions(options)
).then(res => res.text())
}
_getJson(path, options, param) {
return fetch(
this.prefix +
this.base +
this._parsePathParam(path, param) +
this._genOptions(options)
).then(res => res.json())
}
/**
* searches for anime
* @param {string} query - what you are searching for
* @returns {Promise<Object[]>} an array with results
*/
search(query) {
return this._getJson(endpoints.search, { q: query }).then(res => {
return new Promise((resolve, reject) => {
const output = []
for (let item of res.data) {
output.push(
Object.assign({}, item, {
getEpisodes: () => this.getEpisodes(item.slug)
})
)
}
resolve(output)
})
})
}
/**
* gets episodes of an anime
* @param {string} slug - the slug of an anime you can get a slug in a search
* @returns {Promise<Object>} an object with all the episodes
*/
getEpisodes(slug) {
return this._get(endpoints.eps, {}, { slug: slug }).then(html => {
return new Promise((resolve, reject) => {
try {
const $ = cheerio.load(html)
const output = {}
$('.anime__episodes')
.children()
.each((i, elem) => {
elem = $(elem)
const titles = elem
.text()
.trim()
.split('"')
.map(x => x.trim())
.filter(x => x != '')
const href = elem.find('a').attr('href')
let episode = titles[0]
.split(/([0-9]+)/)
.filter(x => x != '')
.filter(x => !isNaN(x))
.map(x => parseInt(x))
episode = episode[episode.length - 1]
output[episode] = {
titles: titles,
href: href,
episode: episode,
slug: slug,
getEpisode: () => this.getEpisode(href)
}
})
resolve(output)
} catch (e) {
reject(e)
}
})
})
}
/**
* searches for anime
* @param {string} slug - the slug of an anime you can get a slug in a search
* @returns {Promise<Object[]>} an array with the episode mp4 uri
*/
getEpisode(slug, ep) {
let endpoint = endpoints.ep
if (
slug.search(
/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/
) != -1
) {
// url is passed
endpoint = slug
}
return this._get(endpoint, {}, { slug: slug, episode: ep }).then(html => {
const $ = cheerio.load(html)
return fetch($('#video-iframe').attr('src'))
.then(data => data.text())
.then(html => {
return new Promise((resolve, reject) => {
try {
const searchPart = "<script type='text/javascript'>eval("
html = html.substring(
html.indexOf(searchPart) + searchPart.length - 1,
html.length
)
html = html.substring(0, html.indexOf('</script>'))
html = eval(html)
html = html.substring(
html.indexOf('myVideo.src(') + 'myVideo.src('.length,
html.length - 2
)
resolve(eval(html))
} catch (e) {
reject(e)
}
})
})
})
}
}
module.exports = Anime