Update server.js
Browse files
server.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
-
const express = require(
|
| 2 |
-
const axios = require(
|
| 3 |
-
const cheerio = require(
|
| 4 |
-
const stringSimilarity = require(
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
-
const PORT =
|
| 8 |
const baseURL = "https://www.tokyoinsider.com/anime/";
|
| 9 |
|
| 10 |
-
app.get(
|
| 11 |
-
const animeName = decodeURIComponent(req.params.name).trim();
|
| 12 |
const episodeNumber = req.params.episode;
|
| 13 |
const firstLetter = animeName[0].toUpperCase();
|
| 14 |
const categoryURL = `${baseURL}${firstLetter}`;
|
|
@@ -22,9 +22,9 @@ app.get('/anime/:name/:episode', async (req, res) => {
|
|
| 22 |
|
| 23 |
let animeLinks = {};
|
| 24 |
|
| 25 |
-
$(
|
| 26 |
-
let title = $(link).text().trim();
|
| 27 |
-
let href = $(link).attr(
|
| 28 |
|
| 29 |
if (title && href) {
|
| 30 |
animeLinks[title] = `https://www.tokyoinsider.com${href}`;
|
|
@@ -38,21 +38,30 @@ app.get('/anime/:name/:episode', async (req, res) => {
|
|
| 38 |
let bestMatch = findClosestMatch(animeName, Object.keys(animeLinks));
|
| 39 |
|
| 40 |
if (bestMatch) {
|
| 41 |
-
let
|
|
|
|
| 42 |
console.log(`Anime Found: ${bestMatch}`);
|
| 43 |
console.log(`Episode URL: ${episodeURL}`);
|
| 44 |
|
| 45 |
let downloads = await getEpisodeDownloads(episodeURL);
|
| 46 |
|
| 47 |
if (downloads.length === 0) {
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
let categorizedDownloads = categorizeDownloads(downloads);
|
| 52 |
|
| 53 |
-
let responseObj = {
|
| 54 |
-
anime: bestMatch,
|
| 55 |
-
episode: episodeNumber,
|
| 56 |
episodeURL,
|
| 57 |
...categorizedDownloads
|
| 58 |
};
|
|
@@ -77,17 +86,17 @@ async function getEpisodeDownloads(episodeURL) {
|
|
| 77 |
|
| 78 |
let downloads = [];
|
| 79 |
|
| 80 |
-
$(
|
| 81 |
-
let linkElement = $(div).find(
|
| 82 |
-
let infoElement = $(div).find(
|
| 83 |
|
| 84 |
if (linkElement.length > 0 && infoElement.length > 0) {
|
| 85 |
let title = linkElement.text().trim();
|
| 86 |
-
let url = linkElement.attr(
|
| 87 |
-
let sizeText = infoElement.find(
|
| 88 |
-
let downloadsCount = infoElement.find(
|
| 89 |
-
let uploader = infoElement.find(
|
| 90 |
-
let addedOn = infoElement.find(
|
| 91 |
|
| 92 |
let size = parseSize(sizeText);
|
| 93 |
|
|
@@ -122,7 +131,7 @@ function categorizeDownloads(downloads) {
|
|
| 122 |
|
| 123 |
function parseSize(sizeText) {
|
| 124 |
let size = parseFloat(sizeText);
|
| 125 |
-
|
| 126 |
if (sizeText.includes("GB")) {
|
| 127 |
size *= 1024;
|
| 128 |
}
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const axios = require("axios");
|
| 3 |
+
const cheerio = require("cheerio");
|
| 4 |
+
const stringSimilarity = require("string-similarity");
|
| 5 |
|
| 6 |
const app = express();
|
| 7 |
+
const PORT = 3000;
|
| 8 |
const baseURL = "https://www.tokyoinsider.com/anime/";
|
| 9 |
|
| 10 |
+
app.get("/anime/:name/:episode", async (req, res) => {
|
| 11 |
+
const animeName = decodeURIComponent(req.params.name).replace(/\s+/g, " ").trim();
|
| 12 |
const episodeNumber = req.params.episode;
|
| 13 |
const firstLetter = animeName[0].toUpperCase();
|
| 14 |
const categoryURL = `${baseURL}${firstLetter}`;
|
|
|
|
| 22 |
|
| 23 |
let animeLinks = {};
|
| 24 |
|
| 25 |
+
$("a[href*='/anime/']").each((i, link) => {
|
| 26 |
+
let title = $(link).text().replace(/\s+/g, " ").trim();
|
| 27 |
+
let href = $(link).attr("href");
|
| 28 |
|
| 29 |
if (title && href) {
|
| 30 |
animeLinks[title] = `https://www.tokyoinsider.com${href}`;
|
|
|
|
| 38 |
let bestMatch = findClosestMatch(animeName, Object.keys(animeLinks));
|
| 39 |
|
| 40 |
if (bestMatch) {
|
| 41 |
+
let baseAnimeURL = animeLinks[bestMatch].replace(/\/episode\/\d+$/, "");
|
| 42 |
+
let episodeURL = `${baseAnimeURL}/episode/${episodeNumber}`;
|
| 43 |
console.log(`Anime Found: ${bestMatch}`);
|
| 44 |
console.log(`Episode URL: ${episodeURL}`);
|
| 45 |
|
| 46 |
let downloads = await getEpisodeDownloads(episodeURL);
|
| 47 |
|
| 48 |
if (downloads.length === 0) {
|
| 49 |
+
console.log("No downloads found, retrying alternative URL...");
|
| 50 |
+
let alternativeURL = `${baseAnimeURL}/ep/${episodeNumber}`;
|
| 51 |
+
downloads = await getEpisodeDownloads(alternativeURL);
|
| 52 |
+
|
| 53 |
+
if (downloads.length === 0) {
|
| 54 |
+
return res.status(404).json({ error: "No download links found." });
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
episodeURL = alternativeURL;
|
| 58 |
}
|
| 59 |
|
| 60 |
let categorizedDownloads = categorizeDownloads(downloads);
|
| 61 |
|
| 62 |
+
let responseObj = {
|
| 63 |
+
anime: bestMatch,
|
| 64 |
+
episode: episodeNumber,
|
| 65 |
episodeURL,
|
| 66 |
...categorizedDownloads
|
| 67 |
};
|
|
|
|
| 86 |
|
| 87 |
let downloads = [];
|
| 88 |
|
| 89 |
+
$(".c_h2, .c_h2b").each((i, div) => {
|
| 90 |
+
let linkElement = $(div).find("a[href*='media.tokyoinsider.com']");
|
| 91 |
+
let infoElement = $(div).find(".finfo");
|
| 92 |
|
| 93 |
if (linkElement.length > 0 && infoElement.length > 0) {
|
| 94 |
let title = linkElement.text().trim();
|
| 95 |
+
let url = linkElement.attr("href");
|
| 96 |
+
let sizeText = infoElement.find("b").eq(0).text();
|
| 97 |
+
let downloadsCount = infoElement.find("b").eq(1).text();
|
| 98 |
+
let uploader = infoElement.find("b").eq(2).text();
|
| 99 |
+
let addedOn = infoElement.find("b").eq(3).text();
|
| 100 |
|
| 101 |
let size = parseSize(sizeText);
|
| 102 |
|
|
|
|
| 131 |
|
| 132 |
function parseSize(sizeText) {
|
| 133 |
let size = parseFloat(sizeText);
|
| 134 |
+
|
| 135 |
if (sizeText.includes("GB")) {
|
| 136 |
size *= 1024;
|
| 137 |
}
|