animes & books page data is fetched auto now
This commit is contained in:
@@ -3,7 +3,6 @@ import { queryAll, queryOne } from '../../shared/database';
|
||||
import {Anime, Episode, Extension, StreamData} from '../types';
|
||||
|
||||
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
|
||||
const TTL = 60 * 60 * 6;
|
||||
|
||||
const ANILIST_URL = "https://graphql.anilist.co";
|
||||
|
||||
@@ -79,6 +78,54 @@ const MEDIA_FIELDS = `
|
||||
}
|
||||
`;
|
||||
|
||||
export async function refreshTrendingAnime(): Promise<void> {
|
||||
const query = `
|
||||
query {
|
||||
Page(page: 1, perPage: 10) {
|
||||
media(type: ANIME, sort: TRENDING_DESC) { ${MEDIA_FIELDS} }
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const data = await fetchAniList(query, {});
|
||||
const list = data?.Page?.media || [];
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
await queryOne("DELETE FROM trending");
|
||||
|
||||
let rank = 1;
|
||||
for (const anime of list) {
|
||||
await queryOne(
|
||||
"INSERT INTO trending (rank, id, full_data, updated_at) VALUES (?, ?, ?, ?)",
|
||||
[rank++, anime.id, JSON.stringify(anime), now]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export async function refreshTopAiringAnime(): Promise<void> {
|
||||
const query = `
|
||||
query {
|
||||
Page(page: 1, perPage: 10) {
|
||||
media(type: ANIME, status: RELEASING, sort: SCORE_DESC) { ${MEDIA_FIELDS} }
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const data = await fetchAniList(query, {});
|
||||
const list = data?.Page?.media || [];
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
await queryOne("DELETE FROM top_airing");
|
||||
|
||||
let rank = 1;
|
||||
for (const anime of list) {
|
||||
await queryOne(
|
||||
"INSERT INTO top_airing (rank, id, full_data, updated_at) VALUES (?, ?, ?, ?)",
|
||||
[rank++, anime.id, JSON.stringify(anime), now]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchAniList(query: string, variables: any) {
|
||||
const res = await fetch(ANILIST_URL, {
|
||||
method: "POST",
|
||||
@@ -119,76 +166,16 @@ export async function getAnimeById(id: string | number): Promise<Anime | { error
|
||||
|
||||
export async function getTrendingAnime(): Promise<Anime[]> {
|
||||
const rows = await queryAll(
|
||||
"SELECT full_data, updated_at FROM trending ORDER BY rank ASC LIMIT 10"
|
||||
"SELECT full_data FROM trending ORDER BY rank ASC LIMIT 10"
|
||||
);
|
||||
|
||||
if (rows.length) {
|
||||
const expired = (Date.now() / 1000 - rows[0].updated_at) > TTL;
|
||||
if (!expired) {
|
||||
return rows.map((r: { full_data: string }) => JSON.parse(r.full_data));
|
||||
}
|
||||
}
|
||||
|
||||
const query = `
|
||||
query {
|
||||
Page(page: 1, perPage: 10) {
|
||||
media(type: ANIME, sort: TRENDING_DESC) { ${MEDIA_FIELDS} }
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const data = await fetchAniList(query, {});
|
||||
const list = data?.Page?.media || [];
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
await queryOne("DELETE FROM trending");
|
||||
let rank = 1;
|
||||
|
||||
for (const anime of list) {
|
||||
await queryOne(
|
||||
"INSERT INTO trending (rank, id, full_data, updated_at) VALUES (?, ?, ?, ?)",
|
||||
[rank++, anime.id, JSON.stringify(anime), now]
|
||||
);
|
||||
}
|
||||
|
||||
return list;
|
||||
return rows.map((r: { full_data: string; }) => JSON.parse(r.full_data));
|
||||
}
|
||||
|
||||
export async function getTopAiringAnime(): Promise<Anime[]> {
|
||||
const rows = await queryAll(
|
||||
"SELECT full_data, updated_at FROM top_airing ORDER BY rank ASC LIMIT 10"
|
||||
"SELECT full_data FROM top_airing ORDER BY rank ASC LIMIT 10"
|
||||
);
|
||||
|
||||
if (rows.length) {
|
||||
const expired = (Date.now() / 1000 - rows[0].updated_at) > TTL;
|
||||
if (!expired) {
|
||||
return rows.map((r: { full_data: string }) => JSON.parse(r.full_data));
|
||||
}
|
||||
}
|
||||
|
||||
const query = `
|
||||
query {
|
||||
Page(page: 1, perPage: 10) {
|
||||
media(type: ANIME, status: RELEASING, sort: SCORE_DESC) { ${MEDIA_FIELDS} }
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
||||
const data = await fetchAniList(query, {});
|
||||
const list = data?.Page?.media || [];
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
|
||||
await queryOne("DELETE FROM top_airing");
|
||||
let rank = 1;
|
||||
|
||||
for (const anime of list) {
|
||||
await queryOne(
|
||||
"INSERT INTO top_airing (rank, id, full_data, updated_at) VALUES (?, ?, ?, ?)",
|
||||
[rank++, anime.id, JSON.stringify(anime), now]
|
||||
);
|
||||
}
|
||||
|
||||
return list;
|
||||
return rows.map((r: { full_data: string; }) => JSON.parse(r.full_data));
|
||||
}
|
||||
|
||||
export async function searchAnimeLocal(query: string): Promise<Anime[]> {
|
||||
|
||||
Reference in New Issue
Block a user