Skip to content

Commit 39058be

Browse files
authored
feat(router/acfun): add embed option (#21589)
1 parent 7774bf7 commit 39058be

File tree

3 files changed

+57
-36
lines changed

3 files changed

+57
-36
lines changed

lib/routes/acfun/bangumi.ts

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import type { Route } from '@/types';
22
import { ViewType } from '@/types';
3-
import got from '@/utils/got';
3+
import ofetch from '@/utils/ofetch';
44
import { parseDate } from '@/utils/parse-date';
55

6+
import { renderDescription } from './templates/description';
7+
68
export const route: Route = {
7-
path: '/bangumi/:id',
9+
path: '/bangumi/:id/:embed?',
810
categories: ['anime'],
911
view: ViewType.Videos,
10-
example: '/acfun/bangumi/5022158',
11-
parameters: { id: '番剧 id' },
12+
example: '/acfun/bangumi/6000617',
13+
parameters: { id: '番剧 id', embed: '默认为开启内嵌视频, 任意值为关闭' },
1214
features: {
1315
requireConfig: false,
1416
requirePuppeteer: false,
@@ -22,31 +24,28 @@ export const route: Route = {
2224
handler,
2325
description: `::: tip
2426
番剧 id 不包含开头的 aa。
25-
例如:\`https://www.acfun.cn/bangumi/aa5022158\` 的番剧 id 是 5022158,不包括开头的 aa。
27+
例如:\`https://www.acfun.cn/bangumi/aa6000617\` 的番剧 id 是 6000617,不包括开头的 aa。
2628
:::`,
2729
};
2830

2931
async function handler(ctx) {
3032
const id = ctx.req.param('id');
31-
const url = `https://www.acfun.cn/bangumi/aa${id}`;
33+
const embed = !ctx.req.param('embed');
34+
const link = `https://www.acfun.cn/bangumi/aa${id}`;
3235

33-
const bangumiPage = await got(url, {
34-
headers: {
35-
Referer: 'https://www.acfun.cn',
36-
},
37-
});
38-
const bangumiData = JSON.parse(bangumiPage.data.match(/window.bangumiData = (.*?);\n/)[1]);
39-
const bangumiList = JSON.parse(bangumiPage.data.match(/window.bangumiList = (.*?);\n/)[1]);
36+
const bangumiPage = await ofetch(link);
37+
const bangumiData = JSON.parse(bangumiPage.match(/window.bangumiData = (.*?);\n/)[1]);
38+
const bangumiList = JSON.parse(bangumiPage.match(/window.bangumiList = (.*?);\n/)[1]);
4039

4140
return {
4241
title: bangumiData.bangumiTitle,
43-
link: url,
42+
link,
4443
description: bangumiData.bangumiIntro,
4544
image: bangumiData.belongResource.coverImageV,
4645
item: bangumiList.items.map((item) => ({
47-
title: `${item.episodeName} - ${item.title}`,
48-
description: `<img src="${item.imgInfo.thumbnailImage.cdnUrls[0].url.split('?')[0]}">`,
49-
link: `http://www.acfun.cn/bangumi/aa${id}_36188_${item.itemId}`,
46+
title: `${item.episodeName}${item.title ? ` - ${item.title}` : ''}`,
47+
description: renderDescription({ embed, aid: `ac${item.itemId}`, img: item.imgInfo.thumbnailImage.cdnUrls[0].url.split('?')[0] }),
48+
link: `https://www.acfun.cn/bangumi/aa${id}_36188_${item.itemId}`,
5049
pubDate: parseDate(item.updateTime, 'x'),
5150
})),
5251
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { renderToString } from 'hono/jsx/dom/server';
2+
3+
type DescriptionProps = {
4+
embed: boolean;
5+
aid: string;
6+
img?: string;
7+
};
8+
9+
const Description = ({ embed, aid, img }: DescriptionProps) => (
10+
<>
11+
{embed ? (
12+
<>
13+
<iframe width="640" height="360" src={`https://www.acfun.cn/player/${aid}`} frameborder="0" allowfullscreen></iframe>
14+
<br />
15+
</>
16+
) : null}
17+
{img ? <img src={img} /> : null}
18+
</>
19+
);
20+
21+
export const renderDescription = (props: DescriptionProps): string => renderToString(<Description {...props} />);

lib/routes/acfun/video.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import { load } from 'cheerio';
22

33
import type { Route } from '@/types';
44
import { ViewType } from '@/types';
5-
import got from '@/utils/got';
5+
import ofetch from '@/utils/ofetch';
66
import { parseDate } from '@/utils/parse-date';
77

8+
import { renderDescription } from './templates/description';
9+
810
export const route: Route = {
9-
path: '/user/video/:uid',
11+
path: '/user/video/:uid/:embed?',
1012
radar: [
1113
{
1214
source: ['www.acfun.cn/u/:id'],
@@ -16,6 +18,7 @@ export const route: Route = {
1618
name: '用户投稿',
1719
parameters: {
1820
uid: '用户 UID',
21+
embed: '默认为开启内嵌视频, 任意值为关闭',
1922
},
2023
categories: ['anime'],
2124
example: '/acfun/user/video/6102',
@@ -26,39 +29,37 @@ export const route: Route = {
2629

2730
async function handler(ctx) {
2831
const uid = ctx.req.param('uid');
29-
const url = `https://www.acfun.cn/u/${uid}`;
32+
const embed = !ctx.req.param('embed');
3033
const host = 'https://www.acfun.cn';
31-
const response = await got(url, {
32-
headers: {
33-
Referer: host,
34-
},
35-
});
36-
const data = response.data;
34+
const link = `${host}/u/${uid}`;
35+
const response = await ofetch(link);
3736

38-
const $ = load(data);
37+
const $ = load(response);
3938
const title = $('title').text();
4039
const description = $('.signature .complete').text();
4140
const list = $('#ac-space-video-list a').toArray();
42-
const image = $('head style')
41+
const image = $('head style:contains("user-photo")')
4342
.text()
44-
.match(/.user-photo{\n\s*background:url\((.*)\) 0% 0% \/ 100% no-repeat;/)[1];
43+
.match(/.user-photo{\n\s*background:url\((.*)\) 0% 0% \/ 100% no-repeat;/)?.[1];
4544

4645
return {
4746
title,
48-
link: url,
47+
link,
4948
description,
5049
image,
5150
item: list.map((item) => {
52-
item = $(item);
51+
const $item = $(item);
5352

54-
const itemTitle = item.find('p.title').text();
55-
const itemImg = item.find('figure img').attr('src');
56-
const itemUrl = item.attr('href');
57-
const itemDate = item.find('.date').text();
53+
const itemTitle = $item.find('p.title').text();
54+
const itemImg = $item.find('figure img').attr('src');
55+
const itemUrl = $item.attr('href')!;
56+
const itemDate = $item.find('.date').text();
57+
const wbInfo = JSON.parse(($item.data('wb-info') as string) || '{}');
58+
const aid = wbInfo.atmid || wbInfo.mediaId || itemUrl.match(/\/v\/(ac\d+)/)?.[1];
5859

5960
return {
6061
title: itemTitle,
61-
description: `<img src="${itemImg.split('?')[0]}">`,
62+
description: renderDescription({ embed, aid, img: itemImg?.split('?')[0] }),
6263
link: host + itemUrl,
6364
pubDate: parseDate(itemDate, 'YYYY/MM/DD'),
6465
};

0 commit comments

Comments
 (0)