forked from anime-vsub/anime-skip-9animetv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
162 lines (139 loc) · 3.87 KB
/
main.ts
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
import { getListEpisodes } from "./logic/get-list-episodes.ts"
import { getServersEpisode } from "./logic/get-servers-episode.ts"
import { getConfServer } from "./logic/get-conf-server.ts"
import { getSource } from "./logic/get-source.ts"
import { Hono } from "hono"
import { cors } from "hono/cors"
import { Cache } from "ttl_cache"
import { searchAnime } from "./logic/search-anime.ts"
import { rangeEmpty } from "./logic/range-empty.ts"
const app = new Hono()
const kv = await Deno.openKv?.()
const cache = new Cache<string, unknown>(30 * 60 * 1000) // 30 minutes
const optsOrigin = [
"https://animevsub.eu.org",
"https://animevsub.netlify.app",
"http://localhost:9000",
"http://localhost:9200"
]
app.use(
"*",
cors({
origin: (origin, c) => {
return optsOrigin.includes(origin)
? origin
: c.req.header("x-requested-with") === "git.shin.animevsub"
? c.req.header("origin") || ""
: optsOrigin[0]
}
})
)
app.get("/list-episodes", async (c) => {
const name = c.req.queries("name")
if (!name) {
return c.json(
{
message: 'Missing query "name"',
code: "missing_query_name"
},
400
)
}
const hash = name.join("|")
const $animesOnCache = cache.get(hash) as
| Awaited<ReturnType<typeof searchAnime>>
| undefined
const animes = $animesOnCache ?? (await searchAnime(name))
if (!$animesOnCache) cache.set(hash, animes)
if ((animes.length === 0) === undefined) {
return c.json(
{
message: "Nothing found",
code: "nothing_found"
},
404
)
}
// TODO: default get first item
const inKv = (await kv?.get(["anime", animes[0].id]))?.value
if (inKv) {
// update store
// if (animes[0].progress.current === animes[0].progress.total) {
// void kv?.set(["anime", animes[0].id], inKv, {
// expireIn: 2592e6 /* 30 days */
// })
// }
return c.json(inKv)
}
const data = {
list: (
await Promise.all(animes.map((anime) => getListEpisodes(anime.id)))
).flat(1),
...animes[0]
}
void kv?.set(["anime", animes[0].id], data, {
expireIn:
animes[0].progress.current === animes[0].progress.total
? 2592e6 /* 30 days */
: 432e5 /* 12 hours */
})
return c.json(data)
})
app.get("/episode-skip/:ep_id", async (c) => {
const ep_id = c.req.param("ep_id")
const inKv = (
await kv?.get<
Awaited<ReturnType<typeof getSource>> & { thumbs: string | null }
>(["episode skip", ep_id])
)?.value
if (
inKv &&
(!rangeEmpty(inKv.intro) || !rangeEmpty(inKv.outro)) &&
inKv.thumbs
) {
// void kv?.set(["episode skip", ep_id], inKv, {
// expireIn: 2592e6 /* 30 days */
// })
return c.json(inKv)
}
const servers = await getServersEpisode(ep_id)
for (const server of servers) {
try {
const confServer = await getConfServer(server.id)
const idRaw = confServer.link.slice(
(confServer.link.lastIndexOf("/") >>> 0) + 1
)
const serverId = idRaw.slice(0, idRaw.indexOf("?") >>> 0)
const source = await getSource(serverId)
const thumbs =
(await source.tracks.find((track) => track.kind === "thumbnails")
?.file) ?? null
if (
(!("intro" in source) ||
!("outro" in source) ||
(rangeEmpty(source.intro) && rangeEmpty(source.outro))) &&
!thumbs
)
throw new Error("Nothing found 'intro' or 'outro'")
const data = { ...source, thumbs }
void kv?.set(["episode skip", ep_id], data, {
expireIn: 2592e6 /* 30 days */
})
return c.json(data)
} catch (err) {
console.warn(
"[episode-skip]: load op/ep server %o failure. Change server (code: %o)",
server,
err
)
}
}
return c.json(
{
message: "Nothing found",
code: "nothing_found"
},
404
)
})
Deno.serve(app.fetch)