diff options
| author | Factiven <[email protected]> | 2023-08-09 15:11:53 +0700 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-08-09 15:11:53 +0700 |
| commit | bae1f53877c3447d3ba940f823e5a8a097f5b22c (patch) | |
| tree | 98abb195bcad6f3e6c61c76c62fc238f227b0ead /pages/api | |
| parent | Update package-lock.json (diff) | |
| download | moopa-3.9.0.tar.xz moopa-3.9.0.zip | |
Update v3.9.0 - Merged Beta to Main (#41)v3.9.0
* initial commit
* Update_v.3.6.7-beta-v1.2
* Update_v.3.6.7-beta-v1.3
* Update_v.3.6.7-beta-v1.3
> update API
* Fixed mediaList won't update
* added .env disqus shortname
* Update_v3.6.7-beta-v1.4
>Implementing database
* Create main.yml
* Update v3.6.7-beta-v1.5
small patch
* title home page
* Update content.js
* Delete db-test.js
* Update content.js
* Update home page card
* Update v3.7.0
* Update v3.7.1-beta
> migrating backend to main code
> fixed schedule component
* Update v3.8.0
> Added dub options
> Moved schedule backend
* Update v.3.8.1
> Fixed episodes on watch page isn't dubbed
* Update v3.8.1-patch-1
* Update v3.8.1-patch-2
> Another patch for dub
* Update v3.8.2
> Removed prisma configuration for database since it's not stable yet
* Update v3.8.3
> Fixed different provider have same id
* Update v.3.8.3
> Fixed player bug where the controls won't hide after updating anilist progress
* Update v3.8.4-patch-2
* Update v3.8.5
> Update readme.md
> Update .env.example
* Update next.config.js
* small adjusment info page
* Update v3.8.6
> Minor update for Android 13 user
* Update v3.8.7
> Added prev and next button to mediaSession
* Update v3.8.7-beta-v2
* Beta v2 (#37)
* Update schema.prisma
* Update schema.prisma
* Update schema.prisma
* Update 3.9.0-beta-v2.1
> Implemented database for storing user Watch List and settings
> Added buttons to auto-play next episodes
* Update v3.9.0-beta-v2.2
* Update README.md
---------
Co-authored-by: Chitraksh Maheshwari <[email protected]>
Diffstat (limited to 'pages/api')
| -rw-r--r-- | pages/api/anify/info/[id].js | 37 | ||||
| -rw-r--r-- | pages/api/anify/page/[...params].js | 41 | ||||
| -rw-r--r-- | pages/api/consumet/episode/[id].js | 12 | ||||
| -rw-r--r-- | pages/api/user/profile.js | 52 | ||||
| -rw-r--r-- | pages/api/user/update/episode.js | 68 |
5 files changed, 207 insertions, 3 deletions
diff --git a/pages/api/anify/info/[id].js b/pages/api/anify/info/[id].js new file mode 100644 index 0000000..c33d158 --- /dev/null +++ b/pages/api/anify/info/[id].js @@ -0,0 +1,37 @@ +import axios from "axios"; +import cacheData from "memory-cache"; + +const API_KEY = process.env.API_KEY; + +// Function to fetch new data +export async function fetchInfo(id) { + try { + const { data } = await axios.get( + `https://api.anify.tv/info/${id}?apikey=${API_KEY}` + ); + return data; + } catch (error) { + console.error("Error fetching data:", error); + return null; + } +} + +export default async function handler(req, res) { + try { + const id = req.query.id; + const cached = cacheData.get(id); + if (cached) { + return res.status(200).json(cached); + } else { + const data = await fetchInfo(id); + if (data) { + res.status(200).json(data); + cacheData.put(id, data, 1000 * 60 * 10); + } else { + res.status(404).json({ message: "Schedule not found" }); + } + } + } catch (error) { + res.status(500).json({ error }); + } +} diff --git a/pages/api/anify/page/[...params].js b/pages/api/anify/page/[...params].js new file mode 100644 index 0000000..80dda6c --- /dev/null +++ b/pages/api/anify/page/[...params].js @@ -0,0 +1,41 @@ +import axios from "axios"; +import cacheData from "memory-cache"; + +const API_KEY = process.env.API_KEY; + +// Function to fetch new data +async function fetchData(id, providerId, chapterId) { + try { + const res = await fetch( + `https://api.anify.tv/pages?id=${id}&providerId=${providerId}&readId=${chapterId}&apikey=${API_KEY}` + ); + const data = await res.json(); + return data; + // return { id, providerId, chapterId }; + } catch (error) { + console.error("Error fetching data:", error); + return null; + } +} + +export default async function handler(req, res) { + try { + const id = req.query.params; + const chapter = req.query.chapter; + // res.status(200).json({ id, chapter }); + const cached = cacheData.get(chapter); + if (cached) { + return res.status(200).json(cached); + } else { + const data = await fetchData(id[0], id[1], chapter); + if (data) { + res.status(200).json(data); + cacheData.put(id[2], data, 1000 * 60 * 10); + } else { + res.status(404).json({ message: "Manga/Novel not found :(" }); + } + } + } catch (error) { + res.status(500).json({ error }); + } +} diff --git a/pages/api/consumet/episode/[id].js b/pages/api/consumet/episode/[id].js index 737292f..e6f40ce 100644 --- a/pages/api/consumet/episode/[id].js +++ b/pages/api/consumet/episode/[id].js @@ -7,12 +7,18 @@ export default async function handler(req, res) { try { const id = req.query.id; const dub = req.query.dub || false; + const refresh = req.query.refresh || false; const providers = ["enime", "gogoanime"]; const datas = []; const cached = cacheData.get(id + dub); - if (cached) { + + if (refresh) { + cacheData.del(id + dub); + } + + if (!refresh && cached) { return res.status(200).json(cached); } else { async function fetchData(provider) { @@ -31,7 +37,7 @@ export default async function handler(req, res) { } return res.json(); }); - if (data.episodes?.length > 0) { + if (data.episodes.length > 0) { datas.push({ providerId: provider, episodes: dub ? data.episodes : data.episodes.reverse(), @@ -53,7 +59,7 @@ export default async function handler(req, res) { if (datas.length === 0) { return res.status(404).json({ message: "Anime not found" }); } else { - cacheData.put(id + dub, { data: datas }, 1000 * 60 * 60 * 15); // 15 minutes + cacheData.put(id + dub, { data: datas }, 1000 * 60 * 60 * 10); res.status(200).json({ data: datas }); } } diff --git a/pages/api/user/profile.js b/pages/api/user/profile.js new file mode 100644 index 0000000..2b44ae2 --- /dev/null +++ b/pages/api/user/profile.js @@ -0,0 +1,52 @@ +import { + createUser, + deleteUser, + getUser, + updateUser, +} from "../../../prisma/user"; + +export default async function handler(req, res) { + try { + switch (req.method) { + case "POST": { + const { name, setting } = req.body; + const new_user = await createUser(name, setting); + if (!new_user) { + return res.status(200).json({ message: "User is already created" }); + } else { + return res.status(201).json(new_user); + } + } + case "PUT": { + const { name, anime } = req.body; + const user = await updateUser(name, anime); + if (!user) { + return res.status(200).json({ message: "Title is already there" }); + } else { + return res.status(200).json(user); + } + } + case "GET": { + const { name } = req.query; + const user = await getUser(name); + if (!user) { + return res.status(404).json({ message: "User not found" }); + } else { + return res.status(200).json(user); + } + } + case "DELETE": { + const { name } = req.body; + const user = await deleteUser(name); + if (!user) { + return res.status(404).json({ message: "User not found" }); + } else { + return res.status(200).json(user); + } + } + } + } catch (error) { + console.log(error); + return res.status(500).json({ message: "Internal server error" }); + } +} diff --git a/pages/api/user/update/episode.js b/pages/api/user/update/episode.js new file mode 100644 index 0000000..f69bb78 --- /dev/null +++ b/pages/api/user/update/episode.js @@ -0,0 +1,68 @@ +import { + createList, + getEpisode, + updateUserEpisode, +} from "../../../../prisma/user"; + +export default async function handler(req, res) { + try { + switch (req.method) { + case "POST": { + const { name, id } = JSON.parse(req.body); + + const episode = await createList(name, id); + if (!episode) { + return res + .status(200) + .json({ message: "Episode is already created" }); + } else { + return res.status(201).json(episode); + } + } + case "PUT": { + const { + name, + id, + watchId, + title, + image, + number, + duration, + timeWatched, + aniTitle, + provider, + } = JSON.parse(req.body); + const episode = await updateUserEpisode({ + name, + id, + watchId, + title, + image, + number, + duration, + timeWatched, + aniTitle, + provider, + }); + if (!episode) { + return res.status(200).json({ message: "Episode is already there" }); + } else { + return res.status(200).json(episode); + } + } + case "GET": { + const { name, id } = req.query; + // console.log(req.query); + const episode = await getEpisode(name, id); + if (!episode) { + return res.status(404).json({ message: "Episode not found" }); + } else { + return res.status(200).json(episode); + } + } + } + } catch (error) { + console.log(error); + return res.status(500).json({ message: "Internal server error" }); + } +} |