From f801f8f422954b884a6541321dba0669ee9d6173 Mon Sep 17 00:00:00 2001 From: Factiven Date: Sun, 22 Oct 2023 19:43:17 +0700 Subject: Update v4.2.0 (#93) --- lib/Artplayer.js | 290 -------------------------------------- lib/anify/getMangaId.js | 40 ++++++ lib/anify/page.js | 13 +- lib/anilist/aniAdvanceSearch.js | 127 +++++++++++++---- lib/anilist/getMedia.js | 2 + lib/anilist/useAnilist.js | 7 +- lib/consumet/manga/getChapters.js | 80 +++++++++++ lib/consumet/manga/getPage.js | 49 +++++++ lib/graphql/query.js | 14 +- 9 files changed, 291 insertions(+), 331 deletions(-) delete mode 100644 lib/Artplayer.js create mode 100644 lib/anify/getMangaId.js create mode 100644 lib/consumet/manga/getChapters.js create mode 100644 lib/consumet/manga/getPage.js (limited to 'lib') diff --git a/lib/Artplayer.js b/lib/Artplayer.js deleted file mode 100644 index 48da24d..0000000 --- a/lib/Artplayer.js +++ /dev/null @@ -1,290 +0,0 @@ -import { useEffect, useRef } from "react"; -import Artplayer from "artplayer"; -import Hls from "hls.js"; -import { useRouter } from "next/router"; - -export default function Player({ - option, - res, - quality, - subSize, - subtitles, - provider, - getInstance, - id, - track, - // socket - // isPlay, - // watchdata, - // room, - autoplay, - setautoplay, - ...rest -}) { - const artRef = useRef(); - - const router = useRouter(); - - function playM3u8(video, url, art) { - if (Hls.isSupported()) { - if (art.hls) art.hls.destroy(); - const hls = new Hls(); - hls.loadSource(url); - hls.attachMedia(video); - art.hls = hls; - art.on("destroy", () => hls.destroy()); - } else if (video.canPlayType("application/vnd.apple.mpegurl")) { - video.src = url; - } else { - art.notice.show = "Unsupported playback format: m3u8"; - } - } - - useEffect(() => { - const art = new Artplayer({ - ...option, - container: artRef.current, - type: "m3u8", - customType: { - m3u8: playM3u8, - }, - fullscreen: true, - hotkey: true, - lock: true, - setting: true, - playbackRate: true, - autoOrientation: true, - pip: true, - theme: "#f97316", - controls: [ - { - index: 10, - name: "fast-rewind", - position: "left", - html: '', - tooltip: "Backward 5s", - click: function () { - art.backward = 5; - }, - }, - { - index: 11, - name: "fast-forward", - position: "left", - html: '', - tooltip: "Forward 5s", - click: function () { - art.forward = 5; - }, - }, - ], - settings: [ - { - html: "Autoplay Next", - // icon: '', - tooltip: "ON/OFF", - switch: localStorage.getItem("autoplay") === "true" ? true : false, - onSwitch: function (item) { - setautoplay(!item.switch); - localStorage.setItem("autoplay", !item.switch); - return !item.switch; - }, - }, - provider === "zoro" && { - html: "Subtitles", - icon: '', - width: 300, - tooltip: "Settings", - selector: [ - { - html: "Display", - icon: '', - tooltip: "Show", - switch: true, - onSwitch: function (item) { - item.tooltip = item.switch ? "Hide" : "Show"; - art.subtitle.show = !item.switch; - return !item.switch; - }, - }, - { - html: "Font Size", - icon: '', - selector: subSize, - onSelect: function (item) { - if (item.html === "Small") { - art.subtitle.style({ fontSize: "16px" }); - localStorage.setItem( - "subSize", - JSON.stringify({ - size: "16px", - html: "Small", - }) - ); - } else if (item.html === "Medium") { - art.subtitle.style({ fontSize: "36px" }); - localStorage.setItem( - "subSize", - JSON.stringify({ - size: "36px", - html: "Medium", - }) - ); - } else if (item.html === "Large") { - art.subtitle.style({ fontSize: "56px" }); - localStorage.setItem( - "subSize", - JSON.stringify({ - size: "56px", - html: "Large", - }) - ); - } - }, - }, - { - html: "Language", - icon: '', - tooltip: "English", - selector: [...subtitles], - onSelect: function (item) { - art.subtitle.switch(item.url, { - name: item.html, - }); - return item.html; - }, - }, - { - html: "Font Family", - tooltip: localStorage.getItem("font") - ? localStorage.getItem("font") - : "Arial", - selector: [ - { html: "Arial" }, - { html: "Comic Sans MS" }, - { html: "Verdana" }, - { html: "Tahoma" }, - { html: "Trebuchet MS" }, - { html: "Times New Roman" }, - { html: "Georgia" }, - { html: "Impact " }, - { html: "Andalé Mono" }, - { html: "Palatino" }, - { html: "Baskerville" }, - { html: "Garamond" }, - { html: "Courier New" }, - { html: "Brush Script MT" }, - ], - onSelect: function (item) { - art.subtitle.style({ fontFamily: item.html }); - localStorage.setItem("font", item.html); - return item.html; - }, - }, - { - html: "Font Shadow", - tooltip: localStorage.getItem("subShadow") - ? JSON.parse(localStorage.getItem("subShadow")).shadow - : "Default", - selector: [ - { html: "None", value: "none" }, - { - html: "Uniform", - value: - "2px 2px 0px #000, -2px -2px 0px #000, 2px -2px 0px #000, -2px 2px 0px #000", - }, - { html: "Raised", value: "-1px 2px 3px rgba(0, 0, 0, 1)" }, - { html: "Depressed", value: "-2px -3px 3px rgba(0, 0, 0, 1)" }, - { html: "Glow", value: "0 0 10px rgba(0, 0, 0, 0.8)" }, - { - html: "Block", - value: - "-3px 3px 4px rgba(0, 0, 0, 1),2px 2px 4px rgba(0, 0, 0, 1),1px -1px 3px rgba(0, 0, 0, 1),-3px -2px 4px rgba(0, 0, 0, 1)", - }, - ], - onSelect: function (item) { - art.subtitle.style({ textShadow: item.value }); - localStorage.setItem( - "subShadow", - JSON.stringify({ shadow: item.html, value: item.value }) - ); - return item.html; - }, - }, - ], - }, - provider === "gogoanime" && { - html: "Quality", - width: 150, - tooltip: `${res}`, - selector: quality, - onSelect: function (item) { - art.switchQuality(item.url, item.html); - localStorage.setItem("quality", item.html); - return item.html; - }, - }, - ].filter(Boolean), - }); - - if ("mediaSession" in navigator) { - art.on("video:timeupdate", () => { - const session = navigator.mediaSession; - if (!session) return; - session.setPositionState({ - duration: art.duration, - playbackRate: art.playbackRate, - position: art.currentTime, - }); - }); - - navigator.mediaSession.setActionHandler("play", () => { - art.play(); - }); - - navigator.mediaSession.setActionHandler("pause", () => { - art.pause(); - }); - - navigator.mediaSession.setActionHandler("previoustrack", () => { - if (track?.prev) { - router.push( - `/en/anime/watch/${id}/${provider}?id=${encodeURIComponent( - track?.prev?.id - )}&num=${track?.prev?.number}` - ); - } - }); - - navigator.mediaSession.setActionHandler("nexttrack", () => { - if (track?.next) { - router.push( - `/en/anime/watch/${id}/${provider}?id=${encodeURIComponent( - track?.next?.id - )}&num=${track?.next?.number}` - ); - } - }); - } - - art.events.proxy(document, "keydown", (event) => { - if (event.key === "f" || event.key === "F") { - art.fullscreen = !art.fullscreen; - } - }); - - // artInstanceRef.current = art; - - if (getInstance && typeof getInstance === "function") { - getInstance(art); - } - - return () => { - if (art && art.destroy) { - art.destroy(false); - } - }; - }, []); - - return
; -} diff --git a/lib/anify/getMangaId.js b/lib/anify/getMangaId.js new file mode 100644 index 0000000..e18da65 --- /dev/null +++ b/lib/anify/getMangaId.js @@ -0,0 +1,40 @@ +import axios from "axios"; + +export async function fetchInfo(romaji, english, native) { + try { + const { data: getManga } = await axios.get( + `https://api.anify.tv/search-advanced?query=${ + english || romaji + }&type=manga` + ); + + const findManga = getManga.find( + (manga) => + manga.title.romaji === romaji || + manga.title.english === english || + manga.title.native === native + ); + + if (!findManga) { + return null; + } + + return { id: findManga.id }; + } catch (error) { + console.error("Error fetching data:", error); + return null; + } +} + +export default async function getMangaId(romaji, english, native) { + try { + const data = await fetchInfo(romaji, english, native); + if (data) { + return data; + } else { + return { message: "Schedule not found" }; + } + } catch (error) { + return { error }; + } +} diff --git a/lib/anify/page.js b/lib/anify/page.js index 65ed309..0f0bb93 100644 --- a/lib/anify/page.js +++ b/lib/anify/page.js @@ -1,10 +1,10 @@ import { redis } from "../redis"; // Function to fetch new data -async function fetchData(id, providerId, chapterId, key) { +async function fetchData(id, chapterNumber, providerId, chapterId, key) { try { const res = await fetch( - `https://api.anify.tv/pages?id=${id}&providerId=${providerId}&readId=${chapterId}&apikey=${key}` + `https://api.anify.tv/pages/${id}/${chapterNumber}/${providerId}/${chapterId}&apikey=${key}` ); const data = await res.json(); return data; @@ -16,6 +16,7 @@ async function fetchData(id, providerId, chapterId, key) { export default async function getAnifyPage( mediaId, + chapterNumber, providerId, chapterId, key @@ -28,7 +29,13 @@ export default async function getAnifyPage( if (cached) { return JSON.parse(cached); } else { - const data = await fetchData(mediaId, providerId, chapterId, key); + const data = await fetchData( + mediaId, + chapterNumber, + providerId, + chapterId, + key + ); if (!data.error) { if (redis) { await redis.set(chapterId, JSON.stringify(data), "EX", 60 * 10); diff --git a/lib/anilist/aniAdvanceSearch.js b/lib/anilist/aniAdvanceSearch.js index 02a5c53..cf344b0 100644 --- a/lib/anilist/aniAdvanceSearch.js +++ b/lib/anilist/aniAdvanceSearch.js @@ -23,37 +23,104 @@ export async function aniAdvanceSearch({ return result; }, {}); - const response = await fetch("https://graphql.anilist.co/", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - query: advanceSearchQuery, - variables: { - ...(search && { - search: search, - ...(!sort && { sort: "SEARCH_MATCH" }), - }), - ...(type && { type: type }), - ...(seasonYear && { seasonYear: seasonYear }), - ...(season && { - season: season, - ...(!seasonYear && { seasonYear: new Date().getFullYear() }), - }), - ...(categorizedGenres && { ...categorizedGenres }), - ...(format && { format: format }), - // ...(genres && { genres: genres }), - // ...(tags && { tags: tags }), - ...(perPage && { perPage: perPage }), - ...(sort && { sort: sort }), + if (type === "MANGA") { + const response = await fetch("https://api.anify.tv/search-advanced", { + method: "POST", + body: JSON.stringify({ + type: "manga", + genres: categorizedGenres, + ...(search && { query: search }), ...(page && { page: page }), + ...(perPage && { perPage: perPage }), + ...(format && { format: format }), + ...(seasonYear && { year: seasonYear }), + ...(type && { type: type }), + }), + }); + + const data = await response.json(); + return { + pageInfo: { + hasNextPage: data.length >= (perPage ?? 20), + currentPage: page, + lastPage: Math.ceil(data.length / (perPage ?? 20)), + perPage: perPage ?? 20, + total: data.length, + }, + media: data.map((item) => ({ + averageScore: item.averageRating, + bannerImage: item.bannerImage, + chapters: item.totalChapters, + coverImage: { + color: item.color, + extraLarge: item.coverImage, + large: item.coverImage, + }, + description: item.description, + duration: item.duration ?? null, + endDate: { + day: null, + month: null, + year: null, + }, + mappings: item.mappings, + format: item.format, + genres: item.genres, + id: item.id, + isAdult: false, + mediaListEntry: null, + nextAiringEpisode: null, + popularity: item.averagePopularity, + season: null, + seasonYear: item.year, + startDate: { + day: null, + month: null, + year: item.year, + }, + status: item.status, + studios: { edges: [] }, + title: { + userPreferred: + item.title.english ?? item.title.romaji ?? item.title.native, + }, + type: item.type, + volumes: item.totalVolumes ?? null, + })), + }; + } else { + const response = await fetch("https://graphql.anilist.co/", { + method: "POST", + headers: { + "Content-Type": "application/json", }, - }), - }); + body: JSON.stringify({ + query: advanceSearchQuery, + variables: { + ...(search && { + search: search, + ...(!sort && { sort: "SEARCH_MATCH" }), + }), + ...(type && { type: type }), + ...(seasonYear && { seasonYear: seasonYear }), + ...(season && { + season: season, + ...(!seasonYear && { seasonYear: new Date().getFullYear() }), + }), + ...(categorizedGenres && { ...categorizedGenres }), + ...(format && { format: format }), + // ...(genres && { genres: genres }), + // ...(tags && { tags: tags }), + ...(perPage && { perPage: perPage }), + ...(sort && { sort: sort }), + ...(page && { page: page }), + }, + }), + }); - const datas = await response.json(); - // console.log(datas); - const data = datas.data.Page; - return data; + const datas = await response.json(); + // console.log(datas); + const data = datas.data.Page; + return data; + } } diff --git a/lib/anilist/getMedia.js b/lib/anilist/getMedia.js index 66bb1b0..2e1b0d0 100644 --- a/lib/anilist/getMedia.js +++ b/lib/anilist/getMedia.js @@ -115,6 +115,8 @@ export default function GetMedia(session, stats) { data.data.Page.recommendations.map((i) => i.mediaRecommendation) ); }); + + // eslint-disable-next-line react-hooks/exhaustive-deps }, [username, accessToken, status?.stats]); return { anime, manga, recommendations }; diff --git a/lib/anilist/useAnilist.js b/lib/anilist/useAnilist.js index 17ab11b..20c1964 100644 --- a/lib/anilist/useAnilist.js +++ b/lib/anilist/useAnilist.js @@ -1,4 +1,4 @@ -import { toast } from "react-toastify"; +import { toast } from "sonner"; export const useAniList = (session) => { const accessToken = session?.user?.token; @@ -238,11 +238,6 @@ export const useAniList = (session) => { console.log(`Progress Updated: ${progress}`, status); toast.success(`Progress Updated: ${progress}`, { position: "bottom-right", - autoClose: 5000, - hideProgressBar: false, - closeOnClick: true, - draggable: true, - theme: "dark", }); } }; diff --git a/lib/consumet/manga/getChapters.js b/lib/consumet/manga/getChapters.js new file mode 100644 index 0000000..7a19bbc --- /dev/null +++ b/lib/consumet/manga/getChapters.js @@ -0,0 +1,80 @@ +let API_URL; +API_URL = process.env.API_URI; +// remove / from the end of the url if it exists +if (API_URL.endsWith("/")) { + API_URL = API_URL.slice(0, -1); +} + +async function fetchInfo(id) { + try { + const providers = [ + "mangadex", + "mangahere", + "mangakakalot", + // "mangapark", + // "mangapill", + "mangasee123", + ]; + let datas = []; + + async function promiseMe(provider) { + try { + const data = await fetch( + `${API_URL}/meta/anilist-manga/info/${id}?provider=${provider}` + ).then((res) => { + if (!res.ok) { + switch (res.status) { + case 404: { + return null; + } + } + } + return res.json(); + }); + if (data.chapters.length > 0) { + datas.push({ + providerId: provider, + chapters: data.chapters, + }); + } + } catch (error) { + console.error(`Error fetching data for provider '${provider}':`, error); + } + } + + await Promise.all(providers.map((provider) => promiseMe(provider))); + + return datas; + } catch (error) { + console.error("Error fetching data:", error); + return null; + } +} + +export default async function getConsumetChapters(id, redis) { + try { + let cached; + let chapters; + + if (redis) { + cached = await redis.get(`chapter:${id}`); + } + + if (cached) { + chapters = JSON.parse(cached); + } else { + chapters = await fetchInfo(id); + } + + if (chapters?.length === 0) { + return null; + } + if (redis) { + await redis.set(`chapter:${id}`, JSON.stringify(chapters), "EX", 60 * 60); // 1 hour + } + + return chapters; + } catch (error) { + return { error }; + } +} diff --git a/lib/consumet/manga/getPage.js b/lib/consumet/manga/getPage.js new file mode 100644 index 0000000..832c1d7 --- /dev/null +++ b/lib/consumet/manga/getPage.js @@ -0,0 +1,49 @@ +let API_URL; +API_URL = process.env.API_URI; +// remove / from the end of the url if it exists +if (API_URL.endsWith("/")) { + API_URL = API_URL.slice(0, -1); +} + +// Function to fetch new data +async function fetchData(id, providerId, chapterId, key) { + try { + const res = await fetch( + `${API_URL}/meta/anilist-manga/read?chapterId=${chapterId}&provider=${providerId}` + ); + const data = await res.json(); + return data; + } catch (error) { + console.error("Error fetching data:", error); + return null; + } +} + +export default async function getConsumetPages( + mediaId, + providerId, + chapterId, + key +) { + try { + // let cached; + // if (redis) { + // cached = await redis.get(chapterId); + // } + // if (cached) { + // return JSON.parse(cached); + // } else { + const data = await fetchData(mediaId, providerId, chapterId, key); + if (!data.error) { + // if (redis) { + // await redis.set(chapterId, JSON.stringify(data), "EX", 60 * 10); + // } + return data; + } else { + return { message: "Manga/Novel not found :(" }; + } + // } + } catch (error) { + return { error }; + } +} diff --git a/lib/graphql/query.js b/lib/graphql/query.js index a09c6ac..45d3d68 100644 --- a/lib/graphql/query.js +++ b/lib/graphql/query.js @@ -176,8 +176,14 @@ query { }`; const mediaInfoQuery = ` - query ($id: Int) { - Media(id: $id) { + query ($id: Int, $type:MediaType) { + Media(id: $id, type:$type) { + mediaListEntry { + status + progress + progressVolumes + status + } id type format @@ -191,6 +197,10 @@ const mediaInfoQuery = ` large color } + startDate { + year + month + } bannerImage description episodes -- cgit v1.2.3