-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
205 lines (187 loc) · 5.81 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
const axios = require("axios");
const { addonBuilder, serveHTTP } = require("stremio-addon-sdk");
const builder = new addonBuilder({
id: "org.janime",
version: "1.0.0",
name: "jAnime",
resources: ["stream"],
types: ["movie", "series"],
catalogs: [],
});
async function fetchData(query, episodeNumber, date) {
const data = await fetchFromAniwatch(`/anime/search/suggest?q=${query}`);
console.log("Data:", data);
const formattedDate = await formatDate(date);
const dateMatch = data.suggestions.find(
(s) => s.moreInfo[0] === formattedDate
);
const exactMatch = data.suggestions.find(
(s) => s.name.toLowerCase() === query.toLowerCase()
);
let suggestion;
if (exactMatch) {
console.log("Exact match found:", exactMatch);
suggestion = exactMatch;
} else if (dateMatch) {
console.log("Date match found:", dateMatch);
suggestion = dateMatch;
}
if (suggestion) {
console.log("Suggestion found:", suggestion);
const episodes = await fetchFromAniwatch(
`/anime/episodes/${suggestion.id}`
);
const episode = episodes.episodes.find((e) => e.number === episodeNumber);
if (episode) {
return getSourceUrl(episode.episodeId);
} else {
console.log(`Tidak ada episode dengan nomor ${episodeNumber}`);
}
} else {
console.log("Tidak ada suggestions yang memenuhi kriteria");
}
}
async function fetchFromAniwatch(endpoint) {
try {
const resp = await axios.get(
`https://api-aniwatch.onrender.com${endpoint}`
);
return resp.data;
} catch (error) {
console.error(`Error fetching data from Aniwatch at ${endpoint}:`, error);
return null;
}
}
async function getSourceUrl(episodeId) {
console.log("Episode ID:", episodeId);
try {
const resp = await axios.get(
`https://anime-api-k3tm.onrender.com/api/stream?id=${episodeId}`
);
const streamingInfo = resp.data.results.streamingInfo;
// Mencari objek yang sesuai dengan kriteria server "HD-1" dan type "sub" atau "Vidcloud" dan type "sub"
console.log("Streaming Info:", streamingInfo);
const targetStream = streamingInfo.find((stream) => {
return (
(stream.subtitleResult &&
stream.subtitleResult.subtitles &&
stream.subtitleResult.subtitles.some(
(subtitle) =>
subtitle.kind === "captions" && subtitle.label === "English"
)) ||
(stream.value &&
stream.value.decryptionResult &&
stream.value.decryptionResult.server === "Vidcloud" &&
stream.value.decryptionResult.type === "sub")
);
});
if (targetStream) {
return (
targetStream.value?.decryptionResult?.link ||
targetStream.subtitleResult?.subtitles.find(
(subtitle) => subtitle.label === "English"
)?.file
);
} else {
console.log(
`Tidak ada sumber dengan server HD-1 atau Vidcloud dan type sub`
);
return null;
}
} catch (error) {
console.error("Error in getSourceUrl:", error);
return null;
}
}
async function formatDate(dateString) {
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
const [year, month, day] = dateString.split("-");
const monthName = months[parseInt(month, 10) - 1];
return `${monthName} ${parseInt(day, 10)}, ${year}`;
}
async function getTmdbData(imdbId, isMovie) {
try {
const response = await axios.get(
`https://api.themoviedb.org/3/find/${imdbId}?api_key=7ac6de5ca5060c7504e05da7b218a30c&external_source=imdb_id`
);
if (isMovie) {
return response.data.movie_results;
} else {
return response.data.tv_results;
}
} catch (error) {
console.error(`Error fetching TMDb data for IMDb ID ${imdbId}:`, error);
return null;
}
}
async function getTmdbNameFromImdbId(imdbId, isMovie) {
const data = await getTmdbData(imdbId, isMovie);
if (data && data.length > 0) {
return isMovie ? data[0].title : data[0].name;
} else {
throw new Error(
`No ${isMovie ? "Movie" : "TV"} results found for the given IMDb ID`
);
}
}
async function getImdbDate(imdbId, isMovie) {
const data = await getTmdbData(imdbId, isMovie);
if (data && data.length > 0) {
return isMovie ? data[0].release_date : data[0].first_air_date;
} else {
throw new Error(
`No ${isMovie ? "Movie" : "TV"} results found for the given IMDb ID`
);
}
}
builder.defineStreamHandler(async ({ type, id }) => {
try {
let url;
if (type === "movie") {
const title = await getTmdbNameFromImdbId(id, true);
const date = await getImdbDate(id, true);
url = await fetchData(title, 1, date);
} else if (type === "series") {
console.log("ID:", id);
const [imdbId, season, episode] = id.split(":");
const title = await getTmdbNameFromImdbId(imdbId, false);
const date = await getImdbDate(imdbId, false);
console.log(
"Title:",
title + " season " + season + " episode " + episode
);
let titles;
if (season === "1") {
titles = title;
} else {
titles = `${title} Season ${season}`;
}
url = await fetchData(titles, Number(episode), date);
}
if (url) {
return Promise.resolve({
streams: [{ url: url, title: `🎞️ Aniwatch - Auto` }],
});
} else {
return Promise.reject(new Error("No streaming URL found"));
}
} catch (error) {
return Promise.reject(error);
}
});
const addonInterface = builder.getInterface();
serveHTTP(addonInterface, { port: 7000 });
console.log("Addon hosting on http://localhost:7000");