Skip to content

Commit

Permalink
fix(163/music/djradio): add option for info (DIYgod#17944)
Browse files Browse the repository at this point in the history
* fix(163/music/djradio): remove duplicate part with embedded tag

* fix(163/music/djradio): add option for info

* fix: use cache

---------
  • Loading branch information
cscnk52 authored Dec 20, 2024
1 parent dd08502 commit e20c7a0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
47 changes: 27 additions & 20 deletions lib/routes/163/music/djradio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ 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',
path: '/music/djradio/:id/:info?',
categories: ['multimedia'],
example: '/163/music/djradio/347317067',
parameters: { id: '节目 id, 可在电台节目页 URL 中找到' },
parameters: { id: '节目 id, 可在电台节目页 URL 中找到', info: '默认在正文尾部显示节目相关信息,任意值为不显示' },
features: {
requireConfig: false,
requirePuppeteer: false,
Expand All @@ -25,35 +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 All @@ -64,6 +70,7 @@ async function handler(ctx) {
pg,
description,
itunes_duration: mm_ss_duration,
info,
});

return {
Expand Down
2 changes: 2 additions & 0 deletions lib/routes/163/templates/music/djradio-content.art
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
<p>{{$value}}</p>
{{/each}}
</div>
{{ if info }}
<div>
<audio src="https://music.163.com/song/media/outer/url?id={{pg.mainTrackId}}.mp3" controls="controls"></audio>
<p>时长: {{itunes_duration}}</p>
<p><a href="https://music.163.com/program/{{pg.id}}">查看节目</a></p>
</div>
{{ /if }}

0 comments on commit e20c7a0

Please sign in to comment.