Skip to content

Commit

Permalink
fix: use cache
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyRL committed Dec 20, 2024
1 parent c31f1db commit 0d040fb
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions lib/routes/163/music/djradio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
import cache from '@/utils/cache';
import { config } from '@/config';

export const route: Route = {
path: '/music/djradio/:id/:info?',
Expand All @@ -25,36 +27,39 @@ export const route: Route = {
handler,
};

const ProcessFeed = (id, limit, offset) =>
cache.tryGet(
`163:music:djradio:${id}:${limit}:${offset}`,
async () =>
await got.post('https://music.163.com/api/dj/program/byradio', {
headers: {
Referer: 'https://music.163.com/',
},
form: {
radioId: id,
limit,
offset,
},
}),
config.cache.routeExpire,
false
);

async function handler(ctx) {
const id = ctx.req.param('id');
const info = !ctx.req.param('info');

const ProcessFeed = (limit, offset) =>
got.post('https://music.163.com/api/dj/program/byradio', {
headers: {
Referer: 'https://music.163.com/',
},
form: {
radioId: id,
limit,
offset,
},
});

const response = await ProcessFeed(1, 0);
const response = await ProcessFeed(id, 1, 0);

const programs = response.data.programs || [];
const { radio, dj } = programs[0] || { radio: {}, dj: {} };
const count = response.data.count || 0;

const countPage = [];
for (let i = 0; i < Math.ceil(count / 500); i++) {
countPage.push(i);
}
const countPage = Array.from({ length: Math.ceil(count / 500) }, (_, i) => i);

const items = await Promise.all(
countPage.map(async (item) => {
const response = await ProcessFeed(500, item * 500);
const response = await ProcessFeed(id, 500, item * 500);
const programs = response.data.programs || [];
const list = programs.map((pg) => {
const description = (pg.description || '').split('\n').map((p) => p);
Expand Down

0 comments on commit 0d040fb

Please sign in to comment.