Spaces:
Paused
Paused
Delete index.js
#2
by mikmc5 - opened
index.js
DELETED
|
@@ -1,302 +0,0 @@
|
|
| 1 |
-
require("dotenv").config();
|
| 2 |
-
const express = require("express");
|
| 3 |
-
const app = express();
|
| 4 |
-
const { removeDuplicate } = require("./helper");
|
| 5 |
-
const UTILS = require("./utils");
|
| 6 |
-
const redisClient = require("./redis");
|
| 7 |
-
const config = require("./config");
|
| 8 |
-
|
| 9 |
-
const REGEX = {
|
| 10 |
-
season_range:
|
| 11 |
-
/S(?:(eason )|(easons )|(eason )|(easons )|(aisons )|(aison ))?(?<start>\d{1,2})\s*?(?:-|&|à|et)\s*?(?<end>\d{1,2})/, //start and end Sxx-xx|Season(s) xx-xx|Sxx à xx
|
| 12 |
-
ep_range: /((?:e)|(?:ep))?(?: )?(?<start>\d{1,4})\s?(-|~)\s?(?<end>\d{1,4})/, //xxx-xxx
|
| 13 |
-
ep_rangewithS:
|
| 14 |
-
/((?:e)|(?:pisode))\s*(?<start>\d{1,3}(?!\d)|\d\d\d??)(?:-?e?(?<end>\d{1,3}))?(?!\d)/, //Exxx-xxx
|
| 15 |
-
};
|
| 16 |
-
|
| 17 |
-
const redis = redisClient();
|
| 18 |
-
|
| 19 |
-
// ----------------------------------------------
|
| 20 |
-
app.get("/", (req, res) => {
|
| 21 |
-
return res.status(200).send("okok");
|
| 22 |
-
});
|
| 23 |
-
|
| 24 |
-
app
|
| 25 |
-
.get("/manifest.json", (req, res) => {
|
| 26 |
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
| 27 |
-
res.setHeader("Access-Control-Allow-Headers", "*");
|
| 28 |
-
res.setHeader("Content-Type", "application/json");
|
| 29 |
-
|
| 30 |
-
//
|
| 31 |
-
var json = { ...config };
|
| 32 |
-
|
| 33 |
-
return res.send(json);
|
| 34 |
-
})
|
| 35 |
-
.get("/stream/:type/:id", async (req, res) => {
|
| 36 |
-
res.setHeader("Access-Control-Allow-Origin", "*");
|
| 37 |
-
res.setHeader("Access-Control-Allow-Headers", "*");
|
| 38 |
-
res.setHeader("Content-Type", "application/json");
|
| 39 |
-
|
| 40 |
-
try {
|
| 41 |
-
await redis.connect();
|
| 42 |
-
let ping = await redis.ping();
|
| 43 |
-
console.log({ ping });
|
| 44 |
-
} catch (error) {}
|
| 45 |
-
|
| 46 |
-
//
|
| 47 |
-
media = req.params.type;
|
| 48 |
-
let id = req.params.id;
|
| 49 |
-
id = id.replace(".json", "");
|
| 50 |
-
|
| 51 |
-
try {
|
| 52 |
-
let stream_cached = await redis.json.get(config.id + "|" + id);
|
| 53 |
-
if (!!stream_cached) {
|
| 54 |
-
console.log(
|
| 55 |
-
`Returning results from cache: ${stream_cached?.length} found`
|
| 56 |
-
);
|
| 57 |
-
// await redis.disconnect();
|
| 58 |
-
return res.send({ streams: stream_cached });
|
| 59 |
-
}
|
| 60 |
-
} catch (error) {
|
| 61 |
-
console.log(`Failed to get ${id} from cache`);
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
let tmp = [];
|
| 65 |
-
|
| 66 |
-
if (id.includes("kitsu")) {
|
| 67 |
-
tmp = await UTILS.getImdbFromKitsu(id);
|
| 68 |
-
if (!tmp) {
|
| 69 |
-
return res.send({ stream: {} });
|
| 70 |
-
}
|
| 71 |
-
} else {
|
| 72 |
-
tmp = id.split(":");
|
| 73 |
-
}
|
| 74 |
-
|
| 75 |
-
let [tt, s, e, abs_season, abs_episode, abs] = tmp;
|
| 76 |
-
|
| 77 |
-
console.log(tmp);
|
| 78 |
-
|
| 79 |
-
let meta = await UTILS.getMeta(tt, media);
|
| 80 |
-
|
| 81 |
-
console.log({ meta: id });
|
| 82 |
-
console.log({ name: meta?.name, year: meta?.year });
|
| 83 |
-
|
| 84 |
-
let query = "";
|
| 85 |
-
query = meta?.name ?? "";
|
| 86 |
-
|
| 87 |
-
let result = [];
|
| 88 |
-
|
| 89 |
-
query = query.replace(/['<>:]/g, "");
|
| 90 |
-
|
| 91 |
-
if (media == "movie") {
|
| 92 |
-
query += " " + meta?.year;
|
| 93 |
-
result = await UTILS.fetchTorrent2(encodeURIComponent(query), "movie");
|
| 94 |
-
} else if (media == "series") {
|
| 95 |
-
let promises = [
|
| 96 |
-
UTILS.fetchTorrent2(
|
| 97 |
-
encodeURIComponent(
|
| 98 |
-
`${UTILS.simplifiedName(query)} S${(s ?? "1").padStart(2, "0")}`
|
| 99 |
-
)
|
| 100 |
-
),
|
| 101 |
-
UTILS.fetchTorrent2(
|
| 102 |
-
encodeURIComponent(
|
| 103 |
-
`${UTILS.simplifiedName(query)} Season ${s ?? "1"}`
|
| 104 |
-
)
|
| 105 |
-
),
|
| 106 |
-
UTILS.fetchTorrent2(
|
| 107 |
-
encodeURIComponent(`${UTILS.simplifiedName(query)} Complet`)
|
| 108 |
-
),
|
| 109 |
-
UTILS.fetchTorrent2(
|
| 110 |
-
encodeURIComponent(
|
| 111 |
-
`${UTILS.simplifiedName(query)} S${s?.padStart(
|
| 112 |
-
2,
|
| 113 |
-
"0"
|
| 114 |
-
)}E${e?.padStart(2, "0")}`
|
| 115 |
-
)
|
| 116 |
-
),
|
| 117 |
-
];
|
| 118 |
-
|
| 119 |
-
if (+s == 1) {
|
| 120 |
-
promises.push(
|
| 121 |
-
UTILS.fetchTorrent2(
|
| 122 |
-
encodeURIComponent(
|
| 123 |
-
`${UTILS.simplifiedName(query)} ${e?.padStart(2, "0")}`
|
| 124 |
-
)
|
| 125 |
-
)
|
| 126 |
-
);
|
| 127 |
-
}
|
| 128 |
-
|
| 129 |
-
if (abs) {
|
| 130 |
-
promises.push(
|
| 131 |
-
UTILS.fetchTorrent2(
|
| 132 |
-
encodeURIComponent(
|
| 133 |
-
`${UTILS.simplifiedName(query)} ${abs_episode?.padStart(2, "0")}`
|
| 134 |
-
)
|
| 135 |
-
)
|
| 136 |
-
);
|
| 137 |
-
promises.push(
|
| 138 |
-
UTILS.fetchTorrent2(
|
| 139 |
-
encodeURIComponent(
|
| 140 |
-
`${UTILS.simplifiedName(query)} ${abs_episode?.padStart(3, "0")}`
|
| 141 |
-
)
|
| 142 |
-
)
|
| 143 |
-
);
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
result = await Promise.all(promises);
|
| 147 |
-
|
| 148 |
-
result = result.reduce((resArr, curr) => {
|
| 149 |
-
if (curr) {
|
| 150 |
-
resArr = [...resArr, ...curr];
|
| 151 |
-
}
|
| 152 |
-
return resArr;
|
| 153 |
-
}, []);
|
| 154 |
-
}
|
| 155 |
-
|
| 156 |
-
result = removeDuplicate(result, "Title");
|
| 157 |
-
|
| 158 |
-
// ------------------------------- FOR RANGE THINGS ---------------------------------------------
|
| 159 |
-
|
| 160 |
-
let matches = [];
|
| 161 |
-
|
| 162 |
-
for (const key in result) {
|
| 163 |
-
const element = result[key];
|
| 164 |
-
|
| 165 |
-
let r = new RegExp(REGEX.season_range, "gmi");
|
| 166 |
-
let match = r.exec(element["Title"]);
|
| 167 |
-
if (match && match["groups"] != null) {
|
| 168 |
-
if (
|
| 169 |
-
![match["groups"]["start"], match["groups"]["end"]].includes(
|
| 170 |
-
meta?.year
|
| 171 |
-
)
|
| 172 |
-
) {
|
| 173 |
-
if (s >= +match["groups"]["start"] && s <= +match["groups"]["end"]) {
|
| 174 |
-
matches.push(result[key]);
|
| 175 |
-
result.splice(key, 1);
|
| 176 |
-
continue;
|
| 177 |
-
}
|
| 178 |
-
}
|
| 179 |
-
}
|
| 180 |
-
|
| 181 |
-
r = new RegExp(REGEX.ep_range, "gmi");
|
| 182 |
-
match = r.exec(element["Title"]);
|
| 183 |
-
|
| 184 |
-
if (match && match["groups"] != null) {
|
| 185 |
-
if (
|
| 186 |
-
![match["groups"]["start"], match["groups"]["end"]].includes(
|
| 187 |
-
meta?.year
|
| 188 |
-
)
|
| 189 |
-
) {
|
| 190 |
-
if (
|
| 191 |
-
abs_episode >= +match["groups"]["start"] &&
|
| 192 |
-
abs_episode <= +match["groups"]["end"]
|
| 193 |
-
) {
|
| 194 |
-
matches.push(result[key]);
|
| 195 |
-
result.splice(key, 1);
|
| 196 |
-
}
|
| 197 |
-
}
|
| 198 |
-
}
|
| 199 |
-
}
|
| 200 |
-
|
| 201 |
-
console.log({ matches: matches.map((el) => el["Title"]) });
|
| 202 |
-
|
| 203 |
-
result = [...matches, ...result];
|
| 204 |
-
result.sort((a, b) => {
|
| 205 |
-
return -(+a["Peers"] - +b["Peers"]) ?? 0;
|
| 206 |
-
});
|
| 207 |
-
|
| 208 |
-
console.log({ "Retenus for filtering": result.length });
|
| 209 |
-
|
| 210 |
-
const MAX_RES = process.env.MAX_RES ?? 20;
|
| 211 |
-
result = result?.length >= MAX_RES ? result.splice(0, MAX_RES) : result;
|
| 212 |
-
|
| 213 |
-
// ----------------------------------------------------------------------------
|
| 214 |
-
|
| 215 |
-
result = (result ?? []).filter(
|
| 216 |
-
(torrent) =>
|
| 217 |
-
(torrent["MagnetUri"] != "" || torrent["Link"] != "") &&
|
| 218 |
-
torrent["Peers"] >= 0
|
| 219 |
-
);
|
| 220 |
-
|
| 221 |
-
console.log({ "Result after removing low peers items": result.length });
|
| 222 |
-
|
| 223 |
-
let torrentParsed = await Promise.all(
|
| 224 |
-
result.map((torrent) =>
|
| 225 |
-
UTILS.getParsedFromMagnetorTorrentFile(
|
| 226 |
-
torrent,
|
| 227 |
-
torrent["MagnetUri"] || torrent["Link"]
|
| 228 |
-
)
|
| 229 |
-
)
|
| 230 |
-
);
|
| 231 |
-
console.log({
|
| 232 |
-
"After parsing (non parsed included)": torrentParsed.length,
|
| 233 |
-
});
|
| 234 |
-
|
| 235 |
-
// engine ? engine.destroy() : null;
|
| 236 |
-
|
| 237 |
-
torrentParsed = torrentParsed.filter(
|
| 238 |
-
(torrent) =>
|
| 239 |
-
torrent &&
|
| 240 |
-
torrent?.parsedTor != null &&
|
| 241 |
-
torrent?.parsedTor?.files?.length > 0
|
| 242 |
-
);
|
| 243 |
-
|
| 244 |
-
console.log({ "Parsed torrents": torrentParsed.length });
|
| 245 |
-
|
| 246 |
-
let stream_results = await Promise.all(
|
| 247 |
-
torrentParsed.map((torrent) =>
|
| 248 |
-
UTILS.toStream(
|
| 249 |
-
torrent,
|
| 250 |
-
media,
|
| 251 |
-
s,
|
| 252 |
-
e,
|
| 253 |
-
abs_season,
|
| 254 |
-
abs_episode,
|
| 255 |
-
abs,
|
| 256 |
-
torrentParsed.length
|
| 257 |
-
)
|
| 258 |
-
)
|
| 259 |
-
);
|
| 260 |
-
|
| 261 |
-
stream_results = Array.from(new Set(stream_results)).filter((e) => !!e);
|
| 262 |
-
|
| 263 |
-
stream_results = [
|
| 264 |
-
...UTILS.filterBasedOnQuality(stream_results, UTILS.qualities["4k"]),
|
| 265 |
-
...UTILS.filterBasedOnQuality(stream_results, UTILS.qualities.fhd),
|
| 266 |
-
...UTILS.filterBasedOnQuality(stream_results, UTILS.qualities.hd),
|
| 267 |
-
...UTILS.filterBasedOnQuality(stream_results, UTILS.qualities.sd),
|
| 268 |
-
...UTILS.filterBasedOnQuality(stream_results, UTILS.qualities.unknown),
|
| 269 |
-
];
|
| 270 |
-
nbreAdded = 0;
|
| 271 |
-
|
| 272 |
-
if (stream_results.length != 0) {
|
| 273 |
-
try {
|
| 274 |
-
let cache_ok = await redis.json.set(
|
| 275 |
-
`${config.id}|${id}`,
|
| 276 |
-
"$",
|
| 277 |
-
stream_results
|
| 278 |
-
);
|
| 279 |
-
if (cache_ok) {
|
| 280 |
-
await redis.expireAt(
|
| 281 |
-
`${config.id}|${id}`,
|
| 282 |
-
new Date(Date.now() + 1000 * 60 * 60 * 24 * 7)
|
| 283 |
-
);
|
| 284 |
-
}
|
| 285 |
-
console.log({ cache_ok });
|
| 286 |
-
} catch (error) {
|
| 287 |
-
console.log("Failed to cache " + id.toString() + ": ", error);
|
| 288 |
-
}
|
| 289 |
-
}
|
| 290 |
-
|
| 291 |
-
console.log({ Final: stream_results.length });
|
| 292 |
-
// try {
|
| 293 |
-
// await redis.disconnect();
|
| 294 |
-
// } catch (error) {
|
| 295 |
-
// console.log("Failed to disconnect from redis");
|
| 296 |
-
// }
|
| 297 |
-
|
| 298 |
-
return res.send({ streams: stream_results });
|
| 299 |
-
})
|
| 300 |
-
.listen(process.env.PORT || 3000, () => {
|
| 301 |
-
console.log("The server is working on " + process.env.PORT || 3000);
|
| 302 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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