forked from ljsydpwym/StremioCinema
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
278 lines (250 loc) · 9.35 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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const Logger = require('./logger.js')
const Stremio = require('./stremio.js')
const Webshare = require('./webshare.js')
const Tmdb = require('./tmdb.js')
const SC = require('./sc.js')
const qs = require('querystring')
const Config = require('./config.js')
const {format, formatHeight, bytesToSize} = require('./helpers')
const express = require('express')
const cors = require('cors')
const app = express()
const path = require('path');
const config = new Config();
app.use(cors())
const baseUrl = '/1/:token'
app.get(baseUrl + '/manifest.json', function (req, res) {
res.setHeader('Cache-Control', 'max-age=86400') // one day
res.setHeader('Content-Type', 'application/json')
res.send({
id: config.getId(),
version: '1.0.0',
name: config.getName(),
description: "Add-on to hook into SCC and Webshare VIP search",
catalogs: [{
type: 'movie',
id: 'scc_movies_news',
name: 'SCC - movies',
extra: [
{name: "search", isRequired: false},
]
}, {
type: 'series',
id: 'scc_series_news',
name: 'SCC - series',
extra: [
{name: "search", isRequired: false},
]
}, {
type: "anime",
id: "scc_anime_news",
name: "SCC - anime",
extra: [
{name: "search", isRequired: false},
]
}
],
resources: ['stream', 'catalog', {
name: "meta",
types: ["movie", "series", "anime"]
}],
types: ['movie', 'series', "anime"],
})
})
app.get(baseUrl + '/log', function (req, res) {
res.sendFile('log.txt', {root: __dirname})
})
const logger = new Logger("Main", config.isDev)
const sc = new SC()
const stremio = new Stremio(sc)
const tmdb = new Tmdb()
app.get(baseUrl + '/stream/:type/:id.json', async function (req, res) {
const webshare = new Webshare()
const args = req.params
logger.log("defineStreamHandler", args)
let scId
if (args.type === 'series' || args.type === "anime") {
const idParts = args.id.split(":")
const season = idParts[1]
const episode = idParts[2]
logger.log("idParts", idParts)
const mediaId = idParts[0];
const isImdbId = mediaId.startsWith("tt");
if (!isImdbId) {
scId = await sc.episode(mediaId, season, episode)
logger.log("episode found")
} else {
const searchType = args.type === "series" ? 'tvshow' : 'anime'
try {
const scShow = await sc.search(mediaId, searchType)
logger.log("scShow", scShow.hits.hits[0]);
scId = await sc.episode(scShow.hits.hits[0]._id, season, episode)
logger.log("episode found")
} catch (e) {
logger.log("error", e)
const tmdbInfo = await tmdb.find(mediaId)
logger.log("tmdbInfo", tmdbInfo)
const tmdbShow = tmdbInfo.tv_results[0]
logger.log("tmdbShow", tmdbShow)
const search = `${tmdbShow.name}`
const fallbackSearch = (await sc.search(search, "*")).hits.hits
const scShowId = fallbackSearch[0]._id
scId = await sc.episode(scShowId, season, episode)
logger.log("episode NOT found - fallback search", search)
}
}
} else {
try {
const mediaId = args.id;
const isImdbId = mediaId.startsWith("tt")
if (!isImdbId) {
scId = args.id
} else {
const scFiles = (await sc.search(mediaId, 'movie')).hits.hits
logger.log("scFiles", scFiles)
let scImdbMovie
if (scFiles.length >= 1) {
scImdbMovie = scFiles[0]
logger.log("movie found")
} else {
const tmdbInfo = await tmdb.find(mediaId)
logger.log("tmdbInfo", tmdbInfo)
const tmdbMovie = tmdbInfo.movie_results[0]
logger.log("tmdbMovie", tmdbMovie)
const search = tmdbMovie.title
const scMovies = (await sc.search(search, 'movie')).hits.hits
scImdbMovie = scMovies[0]
logger.log("movie not found - fallback search", search)
}
if (scImdbMovie === undefined) {
throw new Error("Movie not found")
}
scId = scImdbMovie._id
}
} catch (e) {
logger.log("error", e)
return res.send({streams: []})
}
}
logger.log("scId", scId)
const scStreams = await sc.streams(scId)
logger.log("scStreams", scStreams)
const files = Array.from(scStreams)
.sort((a, b) => b.size - a.size)
.map(it => {
const videos = Array.from(it.video)
.filter(it => it.height !== undefined)
.map(it => formatHeight(it.height))
const audios = [...new Set(Array.from(it.audio)
.filter(it => it.language !== undefined && it.language.length > 0)
.map(it => format(it.language))
.sort((a, b) => a.localeCompare(b)))];
const subtitles = [...new Set(Array.from(it.subtitles)
.filter(it => it.language !== undefined && it.language.length > 0)
.map(it => format(it.language))
.sort((a, b) => a.localeCompare(b)))];
const name = `Size: ${bytesToSize(it.size)}`
const video = videos ? "Video: " + videos.join(",") : undefined
const audio = audios ? "Audio: " + audios.join(",") : undefined
const subtitle = subtitles ? "Subtitles: " + subtitles.join(",") : undefined
return {
ident: it.ident,
original: it,
name: [name, video, audio, subtitle].join("\n")
}
}
)
logger.log("files", files)
await webshare.loginIfNeeded(args.token)
const streams = await Promise.all(files.map(async (it) => {
const link = await webshare.file_link(it.ident, it.original, "video_stream")
return {
url: link,
title: `${it.name}`
}
}))
const output = {streams: streams}
logger.log("output", output)
res.send(output)
})
async function fetchAndFormatData(type, search, skip) {
const scData = search ? await sc.search(search, type) : await sc.searchFrom(type, skip);
const scMovies = scData.hits.hits;
return Object.entries(scMovies).map(([_, data]) => stremio.formatMetaData(data, type === "movie" ? undefined : "series"));
}
app.get(baseUrl + '/catalog/:type/:id/:extra?.json', async function (req, res) {
const {id} = req.params;
logger.log("catalog", req.params)
const splitted = id.split("_");
const prefix = splitted[0];
const realId = splitted[1];
const sorting = splitted[2];
if(prefix !== "scc"){
return res.send({metas: []});
}
const extra = req.params.extra ? qs.parse(req.params.extra) : {search: null, skip: null};
if (extra.skip === undefined)
extra.skip = null;
if (extra.search === undefined)
extra.search = null;
let type;
switch (realId) {
case "movies":
type = "movie";
break;
case "series":
type = "tvshow";
break;
case "anime":
type = "anime";
break;
default:
type = undefined;
}
if (type === undefined) {
logger.log("for id " + realId + " type is undefined");
return res.json({metas: []});
}
const metas = await fetchAndFormatData(type, extra.search, extra.skip);
logger.log("metas", metas)
return res.json({metas});
})
app.get(baseUrl + '/meta/:type/:id.json', async function (req, res) {
const {type, id} = req.params;
logger.log("meta", req.params)
if(!id.startsWith(sc.PREFIX) && type !== "anime"){
return res.send({meta: []});
}
let sccId = sc.getWithoutPrefix(id);
if (type === "series") {
const data = await sc.media(sccId);
const episodes = await sc.episodes(sccId);
const meta = stremio.createMeta(data, type, id);
meta.videos = episodes.map(it => stremio.formatEpisodeMetaData(it));
return res.send({meta});
}
if (type === "movie") {
const data = await sc.media(sccId);
const meta = stremio.createMeta(data, type, id);
return res.send({meta});
}
if (type === "anime"){
if(id.startsWith("tt")){
sccId = (await sc.search(id, type)).hits.hits[0]._id;
}
logger.log("sccId", sccId);
const data = await sc.media(sccId);
const episodes = await sc.episodes(sccId);
const meta = stremio.createMeta(data, type, id);
meta.videos = episodes.map(it => stremio.formatEpisodeMetaData(it));
return res.send({meta});
}
});
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '/index.html'))
})
const port = process.env.PORT || 4000
app.listen(port, function () {
console.log(`http://127.0.0.1:${port}${baseUrl}/manifest.json`)
})
module.exports = app