From 692e9c20d5a32ca99e26c3f7c969fc1f485456a1 Mon Sep 17 00:00:00 2001 From: simon3000 Date: Thu, 7 Sep 2023 11:34:21 +0200 Subject: [PATCH] hum --- api/api.ts | 10 ++++++++++ api/dispatcher.ts | 47 +++++++++++++++++++++++++++++++++++++++++++++-- api/spider.ts | 5 ++--- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/api/api.ts b/api/api.ts index ba7b8387..14c79fc0 100644 --- a/api/api.ts +++ b/api/api.ts @@ -15,6 +15,7 @@ import { albums, AvailableLocales, availableLocales } from './albumParser.js' import { search as searchF } from './common.js' import { joinJob } from './spider.js' +import { dispatch, receipt } from './dispatcher.js' const __dirname = dirname(fileURLToPath(import.meta.url)) @@ -197,6 +198,15 @@ router.get('/playerDiffRank', async ctx => { ctx.body = await getPlayerDiffRank() }) +router.post('/dispatch', ctx => { + ctx.body = { url: dispatch() } +}) + +router.post('/receipt', koaBody(), ctx => { + const { url, data } = ctx.request.body + ctx.body = { result: receipt(url, JSON.parse(data)) } +}) + app.use(router.routes()) app.listen(8301) diff --git a/api/dispatcher.ts b/api/dispatcher.ts index 7ecb8efa..3807669f 100644 --- a/api/dispatcher.ts +++ b/api/dispatcher.ts @@ -1,3 +1,46 @@ -import { Agent } from 'undici' +import { Agent, fetch as f, setGlobalDispatcher } from 'undici' -export const getAgent = () => new Agent({ connect: { timeout: 120_000 } }) +import { wait } from './common' + +setGlobalDispatcher(new Agent({ connect: { timeout: 120_000 } })) + +type Job = { + url: string + p: (data: unknown) => void +} + +let jobs: Job[] = [] + +export const fetch = async (url: string) => { + if (url.includes('music_tag')) { + return f(url).then(res => res.json()) + } + const promise = new Promise(resolve => jobs.push({ url, p: resolve })) + return promise +} + +export const dispatch = () => jobs[Math.floor(Math.random() * jobs.length)].url + +export const receipt = (url: string, data: any) => { + if (data.code === 0) { + jobs = jobs.filter(job => { + if (job.url === url) { + job.p(data) + return false + } + return true + }) + return true + } + return false +} + +while (true) { + await wait(1000 * 10) + const url = dispatch() + if (url) { + const res = await f(url) + const data = await res.json() as any + receipt(url, data) + } +} diff --git a/api/spider.ts b/api/spider.ts index 653e0d92..ac69b2f6 100644 --- a/api/spider.ts +++ b/api/spider.ts @@ -1,7 +1,6 @@ -import { fetch } from 'undici' import LRU from 'lru-cache' -import { getAgent } from './dispatcher.js' +import { fetch } from './dispatcher.js' import { MusicData, MusicCore, PlayerValue, RawAPI, RankKey, MusicTagList, genKey } from './type.js' import { rank, player, search, rankUpdateTime, playerUpdateTime, putTag, checkNewSong, isNewSong, saveRaw, playerDataOld, updatePlayerData, setPlayerNumer } from './database.js' @@ -48,7 +47,7 @@ const down = async (url: string) => { if (hit) { return hit as T } - const result = await fetch(url, { dispatcher: getAgent() }).then(w => w.json()) + const result = await fetch(url) downCache.set(url, result) return result as T }