Skip to content

Commit

Permalink
hum
Browse files Browse the repository at this point in the history
  • Loading branch information
simon300000 committed Sep 7, 2023
1 parent 5d165e9 commit 692e9c2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
10 changes: 10 additions & 0 deletions api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down Expand Up @@ -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)
47 changes: 45 additions & 2 deletions api/dispatcher.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>(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)
}
}
5 changes: 2 additions & 3 deletions api/spider.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -48,7 +47,7 @@ const down = async <T>(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
}
Expand Down

0 comments on commit 692e9c2

Please sign in to comment.