-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
251 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import fs from 'fs-extra'; | ||
import path from 'node:path'; | ||
|
||
import type { FetchedResource } from 'animegarden'; | ||
import type { Database, NewUser, NewTeam, MeiliSearch } from '@animegarden/database'; | ||
|
||
import { fetchMoePage } from '@animegarden/scraper'; | ||
|
||
import { ufetch } from '../utils'; | ||
|
||
export async function fetchMoe(from: number, to: number | undefined, outDir: string) { | ||
await fs.mkdir(outDir, { recursive: true }); | ||
if ((await fs.readdir(outDir)).length > 0) { | ||
throw new Error(`Out dir ${outDir} is not empty`); | ||
} | ||
|
||
let empty = 0; | ||
for (let page = from; to === undefined || page <= to; page++) { | ||
console.log(`Fetch moe page ${page}`); | ||
|
||
const r = await fetchMoePage(ufetch, { | ||
page, | ||
retry: Number.MAX_SAFE_INTEGER | ||
}); | ||
|
||
await fs.promises.writeFile( | ||
path.join(outDir, `${page}.json`), | ||
JSON.stringify(r, null, 2), | ||
'utf-8' | ||
); | ||
|
||
if (r.length === 0) { | ||
empty++; | ||
if (empty >= 10) { | ||
break; | ||
} | ||
} else { | ||
empty = 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { ResourceType } from 'animegarden'; | ||
|
||
export const Anime = '549ef207fe682f7549f1ea90'; | ||
|
||
export const Collection = '54967e14ff43b99e284d0bf7'; | ||
|
||
export const Music = '549eef6ffe682f7549f1ea8b'; | ||
|
||
export const Manga = '549eefebfe682f7549f1ea8c'; | ||
|
||
export const TV = '549ff1db30bcfc225bf9e607'; | ||
|
||
export const Other = '549ef250fe682f7549f1ea91'; | ||
|
||
export const Game = '549ef015fe682f7549f1ea8d'; | ||
|
||
export function getType(tags: string[]): ResourceType { | ||
for (const tag of tags) { | ||
switch (tag) { | ||
case Anime: | ||
return '動畫'; | ||
case Collection: | ||
return '季度全集'; | ||
case Manga: | ||
return '漫畫'; | ||
case Music: | ||
return '音樂'; | ||
case TV: | ||
return '日劇'; | ||
case Game: | ||
return '遊戲'; | ||
case Other: | ||
return '其他'; | ||
default: | ||
} | ||
} | ||
return '其他'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { retryFn } from 'animegarden'; | ||
|
||
const Users = new Map<string, { provider: 'moe'; providerId: string; name: string }>(); | ||
|
||
const Teams = new Map< | ||
string, | ||
{ provider: 'moe'; providerId: string; name: string; avatar: string } | ||
>(); | ||
|
||
export async function fetchUser( | ||
ofetch: (request: string, init?: RequestInit) => Promise<Response>, | ||
id: string | ||
) { | ||
if (Users.get(id)) { | ||
return Users.get(id)!; | ||
} | ||
|
||
const resp = await retryFn(async () => { | ||
const resp = await ofetch(`https://bangumi.moe/api/user/fetch`, { | ||
method: 'POST', | ||
body: JSON.stringify({ _ids: [id] }), | ||
headers: new Headers([ | ||
[ | ||
'User-Agent', | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0' | ||
] | ||
]) | ||
}); | ||
if (!resp.ok) { | ||
throw new Error(resp.statusText, { cause: resp }); | ||
} | ||
return resp; | ||
}, 10); | ||
if (!resp.ok) { | ||
throw new Error('Failed connecting https://bangumi.moe/'); | ||
} | ||
|
||
const data = await resp.json(); | ||
|
||
const user = { | ||
provider: 'moe', | ||
providerId: id, | ||
name: data[0].username | ||
} as const; | ||
|
||
Users.set(id, user); | ||
|
||
return user; | ||
} | ||
|
||
export async function fetchTeam( | ||
ofetch: (request: string, init?: RequestInit) => Promise<Response>, | ||
id: string | ||
) { | ||
if (Teams.get(id)) { | ||
return Teams.get(id)!; | ||
} | ||
|
||
const resp = await retryFn(async () => { | ||
const resp = await ofetch(`https://bangumi.moe/api/team/fetch`, { | ||
method: 'POST', | ||
body: JSON.stringify({ _ids: [id] }), | ||
headers: new Headers([ | ||
[ | ||
'User-Agent', | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0' | ||
] | ||
]) | ||
}); | ||
if (!resp.ok) { | ||
throw new Error(resp.statusText, { cause: resp }); | ||
} | ||
return resp; | ||
}, 10); | ||
if (!resp.ok) { | ||
throw new Error('Failed connecting https://bangumi.moe/'); | ||
} | ||
|
||
const data = await resp.json(); | ||
|
||
const team = { | ||
provider: 'moe', | ||
providerId: id, | ||
name: data[0].name, | ||
avatar: data[0].icon | ||
} as const; | ||
|
||
Teams.set(id, team); | ||
|
||
return team; | ||
} |