-
Notifications
You must be signed in to change notification settings - Fork 4
/
sources.js
208 lines (180 loc) · 6.15 KB
/
sources.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
const { parse } = require("fast-html-parser");
const youtubedl = require("youtube-dl-exec");
const config = require('./config');
require('dotenv').config();
const axios = require('axios').default;
const NodeCache = require("node-cache");
const cache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
const StreamCache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
const MetaCache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
const CatalogCache = new NodeCache({ stdTTL: (0.5 * 60 * 60), checkperiod: (1 * 60 * 60) });
client = axios.create({
baseURL: config.BaseURL,
timeout: 10000
});
async function stream(einthusan_id) {
try {
const id = einthusan_id.split(":")[1];
const Cached = StreamCache.get(id);
if (Cached) return Cached;
let url = `${config.BaseURL}/movie/watch/${id}/`;
let info = await youtubedl(url, {
dumpSingleJson: true,
simulate: true,
skipDownload: true,
noDownload: true,
});
//console.log(info)
if (!info) throw "error on youtubedl";
let streams = {
url: info.url,
};
//console.log(streams)
if (streams) StreamCache.set(id, streams);
return streams;
} catch (e) {
console.error(e);
return Promise.reject(e);
}
}
async function meta(einthusan_id) {
try {
const id = einthusan_id.split(":")[1];
const Cached = MetaCache.get(id);
if (Cached) return Cached;
const url = `/movie/watch/${id}/`;
console.log("url", url);
const res = await client.get(url);
if (!res || !res.data) throw "error requesting metadata";
const html = parse(res.data);
const movie_description = html.querySelector("#UIMovieSummary").querySelector("li");
const img = movie_description.querySelector("div.block1 a img").rawAttributes['src'];
const year = movie_description.querySelector("div.info p").childNodes[0].rawText;
const title = movie_description.querySelector("a.title h3").rawText;
const description = movie_description.querySelector("p.synopsis").rawText;
//var genresarray = details[3].childNodes[2].querySelectorAll("a");
var genresarray = [];
const actorsarray = html.querySelectorAll("div.prof p");
let trailer = html.querySelectorAll("div.extras a")[1];
if (trailer.rawAttributes['href']) {
trailer = trailer.rawAttributes['href'].split("v=")[1];
} else {
trailer = false;
}
let actors = [];
if (actorsarray) {
for (let i = 0; i < actorsarray.length; i++) {
actors[i] = actorsarray[i].rawText;
}
}
let genres = [];
if (genresarray) {
for (let i = 0; i < genresarray.length; i++) {
genres[i] = genresarray[i].rawText;
}
}
let metaObj = {
id: einthusan_id,
name: title,
posterShape: 'poster',
type: 'movie',
};
if (year) {
metaObj.releaseInfo = year
};
if (img) {
metaObj.poster = "https:" + img
};
if (img) {
metaObj.background = "https:" + img
};
if (year) {
metaObj.releaseInfo = year
};
if (genres) {
metaObj.genres = genres
};
if (description) {
metaObj.description = description
};
if (actors) {
metaObj.cast = actors
};
//if (runtime){metaObj.runtime = runtime};
if (trailer) {
metaObj.trailers = [{
source: trailer,
type: "Trailer"
}
]
}
if (metaObj) MetaCache.set(id, metaObj);
//console.log("metaObj", metaObj);
return metaObj;
} catch (e) {
console.error(e);
return Promise.reject(e);
}
}
async function search(lang, slug) {
try {
const CacheID = slug + "_" + lang
slug = encodeURI(slug);
lang = lang.substring(0, lang.length - 6);
const url = `/movie/results/?lang=${lang}&query=${slug}`;
console.log('search url:', url);
let res = cache.get(CacheID);
if (!res) {
while (!res || res.length == 0) {
res = await getcatalogresults(url);
}
}
cache.set(CacheID, res);
return res;
} catch (e) {
console.error(e);
return Promise.reject(e);
}
}
async function getcatalogresults(url) {
try {
const Cached = CatalogCache.get(url);
if (Cached) return Cached;
const res = await client.get(url);
if (!res || !res.data) throw "error getcatalogresults";
const html = parse(res.data);
let search_results = html.querySelector("#UIMovieSummary");
//console.log("search_results",search_results)
if (search_results) {
search_results = search_results.querySelectorAll("li");
} else {
return [];
}
let resultsarray = [];
for (let i = 0; i < search_results.length; i++) {
const img = search_results[i].querySelector("div.block1 a img").rawAttributes['src'];
const year = search_results[i].querySelector("div.info p").childNodes[0].rawText;
const title = search_results[i].querySelector("a.title h3").rawText;
const id = search_results[i].querySelector("a.title").rawAttributes['href'];
resultsarray.push({
id: "einthusan_id:" + id.split('/')[3],
type: "movie",
name: title,
poster: "https:" + img,
releaseInfo: year,
posterShape: 'poster'
})
}
if (resultsarray) CatalogCache.set(url, resultsarray);
console.log('resultsarray:', resultsarray);
return resultsarray;
} catch (e) {
console.error(e);
return Promise.reject(e);
}
}
module.exports = {
search,
meta,
stream
};