Create server.js
Browse files
server.js
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const express = require('express');
|
2 |
+
const axios = require('axios');
|
3 |
+
const cheerio = require('cheerio');
|
4 |
+
|
5 |
+
const app = express();
|
6 |
+
const PORT = 7860;
|
7 |
+
|
8 |
+
// Enable CORS
|
9 |
+
const cors = require('cors');
|
10 |
+
app.use(cors());
|
11 |
+
|
12 |
+
app.get('/anime/:title/:episode', async (req, res) => {
|
13 |
+
const animeTitle = req.params.title;
|
14 |
+
const episodeNumber = req.params.episode;
|
15 |
+
|
16 |
+
console.log(`Requested: ${animeTitle} Episode ${episodeNumber}`);
|
17 |
+
|
18 |
+
try {
|
19 |
+
const episodeData = await searchGogoanime(animeTitle, episodeNumber);
|
20 |
+
res.json(episodeData);
|
21 |
+
} catch (error) {
|
22 |
+
res.status(500).json({ error: "Failed to fetch episode data", details: error.message });
|
23 |
+
}
|
24 |
+
});
|
25 |
+
|
26 |
+
async function searchGogoanime(animeTitle, episodeNumber) {
|
27 |
+
try {
|
28 |
+
console.log(`Searching for: ${animeTitle} Episode ${episodeNumber}`);
|
29 |
+
|
30 |
+
// Search Gogoanime
|
31 |
+
const searchUrl = `https://ww25.gogoanimes.fi/search.html?keyword=${encodeURIComponent(animeTitle)}`;
|
32 |
+
const searchResponse = await axios.get(searchUrl);
|
33 |
+
const $ = cheerio.load(searchResponse.data);
|
34 |
+
|
35 |
+
// Find first result
|
36 |
+
const firstResult = $('.items li a').first();
|
37 |
+
const animeSlug = firstResult.attr('href')?.split('/')[2];
|
38 |
+
if (!animeSlug) throw new Error("Anime not found");
|
39 |
+
|
40 |
+
console.log(`Anime Found: ${animeTitle} (${animeSlug})`);
|
41 |
+
|
42 |
+
// Episode page URL
|
43 |
+
const episodeUrl = `https://ww25.gogoanimes.fi/${animeSlug}-episode-${episodeNumber}`;
|
44 |
+
console.log(`Episode URL: ${episodeUrl}`);
|
45 |
+
|
46 |
+
return await getEpisodeInfo(episodeUrl, animeTitle, episodeNumber);
|
47 |
+
} catch (error) {
|
48 |
+
throw new Error("Error searching Gogoanime: " + error.message);
|
49 |
+
}
|
50 |
+
}
|
51 |
+
|
52 |
+
async function getEpisodeInfo(episodeUrl, animeTitle, episodeNumber) {
|
53 |
+
try {
|
54 |
+
console.log(`Fetching: ${episodeUrl}`);
|
55 |
+
const response = await axios.get(episodeUrl);
|
56 |
+
const $ = cheerio.load(response.data);
|
57 |
+
|
58 |
+
// Find download page URL
|
59 |
+
const downloadPageUrl = $('.dowloads a').attr('href');
|
60 |
+
if (!downloadPageUrl) throw new Error("Download page not found");
|
61 |
+
|
62 |
+
console.log(`Download Page Found: ${downloadPageUrl}`);
|
63 |
+
|
64 |
+
return {
|
65 |
+
anime: animeTitle,
|
66 |
+
episode: episodeNumber,
|
67 |
+
owner: "Reiker",
|
68 |
+
downloadPage: downloadPageUrl
|
69 |
+
};
|
70 |
+
} catch (error) {
|
71 |
+
throw new Error("Error fetching episode info: " + error.message);
|
72 |
+
}
|
73 |
+
}
|
74 |
+
|
75 |
+
// Start API Server
|
76 |
+
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));
|