forked from MrMonkey42/stremio-addon-debrid-search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddon.js
172 lines (158 loc) · 6.12 KB
/
addon.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
import { addonBuilder } from "stremio-addon-sdk"
import DebridLink from './lib/debrid-link.js'
import RealDebrid from './lib/real-debrid.js'
import packageInfo from "./package.json" assert { type: "json" }
import StreamProvider from './lib/stream-provider.js'
import CatalogProvider from './lib/catalog-provider.js'
// Docs: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/responses/manifest.md
const manifest = {
id: "community.stremio.debrid-search",
version: packageInfo.version,
name: "Debrid Search",
description: packageInfo.description,
background: `https://i.ibb.co/VtSfFP9/t8wVwcg.jpg`,
logo: `https://img.icons8.com/fluency/256/search-in-cloud.png`,
catalogs: [
{
"id": "debridsearch",
"type": "other",
"extra": [
{
"name": "search",
"isRequired": false
},
{
"name": "skip",
"isRequired": false
}
]
}
],
resources: [
"catalog",
"stream"
],
types: [
"movie",
"series",
'anime',
"other"
],
idPrefixes: ['tt'],
behaviorHints: {
configurable: true,
configurationRequired: true
},
// Ref - https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/responses/manifest.md#user-data
config: [
{
"key": "DebridProvider",
"title": "Debrid Provider",
"type": "select",
"required": true,
"options": ["RealDebrid", "DebridLink"]
},
{
"key": "DebridApiKey",
"title": "Debrid API Key",
"type": "text",
"required": true
},
]
}
const CACHE_MAX_AGE = parseInt(process.env.CACHE_MAX_AGE) || 1 * 60 // 1 min
const STALE_REVALIDATE_AGE = 1 * 60 // 1 min
const STALE_ERROR_AGE = 1 * 24 * 60 * 60 // 1 days
const builder = new addonBuilder(manifest)
builder.defineCatalogHandler((args) => {
return new Promise((resolve, reject) => {
console.log("Request for catalog with args: " + JSON.stringify(args))
// Request to Debrid Search
if (args.id == 'debridsearch') {
if (!((args.config?.DebridProvider && args.config?.DebridApiKey) || args.config?.DebridLinkApiKey)) {
reject(new Error('Invalid Debrid configuration: Missing configs'))
}
// Search catalog request
if (args.extra.search) {
CatalogProvider.searchTorrents(args.config, args.extra.search)
.then(metas => {
console.log("Response metas: " + JSON.stringify(metas))
resolve({
metas: metas,
cacheMaxAge: CACHE_MAX_AGE,
staleRevalidate: STALE_REVALIDATE_AGE,
staleError: STALE_ERROR_AGE
})
})
.catch(err => reject(err))
} else {
// Standard catalog request
let resultsPromise
if (args.config.DebridLinkApiKey) {
resultsPromise = DebridLink.listTorrents(args.config.DebridLinkApiKey, args.extra.skip)
} else if (args.config.DebridProvider == "DebridLink") {
resultsPromise = DebridLink.listTorrents(args.config.DebridApiKey, args.extra.skip)
} else if (args.config.DebridProvider == "RealDebrid") {
resultsPromise = RealDebrid.listTorrents(args.config.DebridApiKey, args.extra.skip)
} else {
reject(new Error('Invalid Debrid configuration: Unknown DebridProvider'))
}
resultsPromise
.then(metas => {
console.log("Response metas: " + JSON.stringify(metas))
resolve({
metas: metas,
cacheMaxAge: CACHE_MAX_AGE,
staleRevalidate: STALE_REVALIDATE_AGE,
staleError: STALE_ERROR_AGE
})
})
.catch(err => reject(err))
}
} else {
reject(new Error('Invalid catalog request'))
}
})
})
// Docs: https://github.com/Stremio/stremio-addon-sdk/blob/master/docs/api/requests/defineStreamHandler.md
builder.defineStreamHandler(args => {
return new Promise((resolve, reject) => {
if (!args.id.match(/tt\d+/i)) {
resolve({ streams: [] })
return
}
console.log("Request for streams with args: " + JSON.stringify(args))
switch (args.type) {
case 'movie':
StreamProvider.getMovieStreams(args.config, args.type, args.id)
.then(streams => {
console.log("Response streams: " + JSON.stringify(streams))
resolve({
streams: streams,
cacheMaxAge: CACHE_MAX_AGE,
staleRevalidate: STALE_REVALIDATE_AGE,
staleError: STALE_ERROR_AGE
})
})
.catch(err => reject(err))
break
case 'series':
StreamProvider.getSeriesStreams(args.config, args.type, args.id)
.then(streams => {
console.log("Response streams: " + JSON.stringify(streams))
resolve({
streams: streams,
cacheMaxAge: CACHE_MAX_AGE,
staleRevalidate: STALE_REVALIDATE_AGE,
staleError: STALE_ERROR_AGE
})
})
.catch(err => reject(err))
break
default:
results = resolve({ streams: [] })
break
}
})
})
export default builder.getInterface()