forked from MrMonkey42/stremio-addon-debrid-search
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaddon.js
More file actions
104 lines (93 loc) · 3.81 KB
/
addon.js
File metadata and controls
104 lines (93 loc) · 3.81 KB
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
import { addonBuilder } from "stremio-addon-sdk"
import StreamProvider from './lib/stream-provider.js'
import CatalogProvider from './lib/catalog-provider.js'
import { getManifest } from './lib/util/manifest.js'
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(getManifest())
builder.defineCatalogHandler((args) => {
return new Promise((resolve, reject) => {
const debugArgs = structuredClone(args)
if (args.config?.DebridApiKey)
debugArgs.config.DebridApiKey = '*'.repeat(args.config.DebridApiKey.length)
console.log("Request for catalog with args: " + JSON.stringify(debugArgs))
// 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,
...enrichCacheParams()
})
})
.catch(err => reject(err))
} else {
// Standard catalog request
CatalogProvider.listTorrents(args.config, args.extra.skip)
.then(metas => {
console.log("Response metas: " + JSON.stringify(metas))
resolve({
metas
})
})
.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
}
const debugArgs = structuredClone(args)
if (args.config?.DebridApiKey)
debugArgs.config.DebridApiKey = '*'.repeat(args.config.DebridApiKey.length)
console.log("Request for streams with args: " + JSON.stringify(debugArgs))
switch (args.type) {
case 'movie':
StreamProvider.getMovieStreams(args.config, args.type, args.id)
.then(streams => {
console.log("Response streams: " + JSON.stringify(streams))
resolve({
streams,
...enrichCacheParams()
})
})
.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,
...enrichCacheParams()
})
})
.catch(err => reject(err))
break
default:
results = resolve({ streams: [] })
break
}
})
})
function enrichCacheParams() {
return {
cacheMaxAge: CACHE_MAX_AGE,
staleError: STALE_ERROR_AGE
}
}
export default builder.getInterface()