Skip to content

Commit

Permalink
refactor: split bangumi (#17293)
Browse files Browse the repository at this point in the history
* refactor: split bangumi

* fix: template path

* feat!: remove `/user/wish/:id` since it's equivalent to /user/collections/:id/2/1

* refactor: use ofetch
  • Loading branch information
TonyRL authored Oct 24, 2024
1 parent 7a74a13 commit 9a651fe
Show file tree
Hide file tree
Showing 23 changed files with 105 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@ import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/moe/*',
path: '/*',
categories: ['anime'],
radar: [
{
source: ['bangumi.moe/'],
target: '/moe',
},
],
name: 'Unknown',
maintainers: [],
name: 'Latest',
example: '/bangumi.moe',
maintainers: ['nczitzk'],
handler,
url: 'bangumi.moe/',
};

async function handler(ctx) {
const isLatest = getSubPath(ctx) === '/moe';
const isLatest = getSubPath(ctx) === '/';
const rootUrl = 'https://bangumi.moe';

let response;
Expand Down
6 changes: 6 additions & 0 deletions lib/routes/bangumi.moe/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '萌番组',
url: 'bangumi.online',
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'アニメ新番組',
url: 'bangumi.moe',
url: 'bangumi.online',
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { parseDate } from '@/utils/parse-date';
import path from 'node:path';

export const route: Route = {
path: '/online',
path: '/',
categories: ['anime'],
example: '/bangumi/online',
example: '/bangumi.online',
parameters: {},
features: {
requireConfig: false,
Expand Down Expand Up @@ -40,7 +40,7 @@ async function handler() {

const items = list.map((item) => ({
title: `${item.title.zh ?? item.title.ja} - 第 ${item.volume} 集`,
description: art(path.join(__dirname, '../templates/online/image.art'), {
description: art(path.join(__dirname, 'templates/image.art'), {
src: `https:${item.cover}`,
alt: `${item.title_zh} - 第 ${item.volume} 集`,
}),
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { art } from '@/utils/render';
import path from 'node:path';

export const route: Route = {
path: '/tv/calendar/today',
path: '/calendar/today',
categories: ['anime'],
example: '/bangumi/tv/calendar/today',
example: '/bangumi.tv/calendar/today',
parameters: {},
features: {
requireConfig: false,
Expand Down Expand Up @@ -42,10 +42,10 @@ async function handler() {

const todayList = list.find((l) => l.weekday.id % 7 === day);
const todayBgmId = new Set(todayList.items.map((t) => t.id.toString()));
const images = todayList.items.reduce((p, c) => {
p[c.id] = (c.images || {}).large;
return p;
}, {});
const images: { [key: string]: string } = {};
for (const item of todayList.items) {
images[item.id] = (item.images || {}).large;
}
const todayBgm = data.items.filter((d) => todayBgmId.has(d.bgmId));
for (const bgm of todayBgm) {
bgm.image = images[bgm.bgmId];
Expand All @@ -65,7 +65,7 @@ async function handler() {
const link = `https://bangumi.tv/subject/${bgm.bgmId}`;
const id = `${link}#${new Intl.DateTimeFormat('zh-CN').format(updated)}`;

const html = art(path.resolve(__dirname, '../../templates/tv/today.art'), {
const html = art(path.join(__dirname, '../templates/today.art'), {
bgm,
siteMeta,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

export const route: Route = {
path: '/tv/topic/:id',
path: '/topic/:id',
categories: ['anime'],
example: '/bangumi/tv/topic/367032',
example: '/bangumi.tv/topic/367032',
parameters: { id: '话题 id, 在话题页面地址栏查看' },
features: {
requireConfig: false,
Expand All @@ -31,7 +31,7 @@ async function handler(ctx) {
// bangumi.tv未提供获取小组话题的API,因此仍需要通过抓取网页来获取
const topicID = ctx.req.param('id');
const link = `https://bgm.tv/group/topic/${topicID}`;
const { data: html } = await got(link);
const html = await ofetch(link);
const $ = load(html);
const title = $('#pageHeader h1').text();
const latestReplies = $('.row_reply')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
const base_url = 'https://bgm.tv';
const baseUrl = 'https://bgm.tv';

export const route: Route = {
path: '/tv/group/:id',
path: '/group/:id',
categories: ['anime'],
example: '/bangumi/tv/group/boring',
example: '/bangumi.tv/group/boring',
parameters: { id: '小组 id, 在小组页面地址栏查看' },
features: {
requireConfig: false,
Expand All @@ -30,29 +30,29 @@ export const route: Route = {

async function handler(ctx) {
const groupID = ctx.req.param('id');
const link = `${base_url}/group/${groupID}/forum`;
const { data: html } = await got(link);
const link = `${baseUrl}/group/${groupID}/forum`;
const html = await ofetch(link);
const $ = load(html);
const title = 'Bangumi - ' + $('.SecondaryNavTitle').text();

const items = await Promise.all(
$('.topic_list .topic')
.toArray()
.map(async (elem) => {
const link = new URL($('.subject a', elem).attr('href'), base_url).href;
const fullText = await cache.tryGet(link, async () => {
const { data: html } = await got(link);
.map((elem) => {
const link = new URL($('.subject a', elem).attr('href'), baseUrl).href;
return cache.tryGet(link, async () => {
const html = await ofetch(link);
const $ = load(html);
return $('.postTopic .topic_content').html();
const fullText = $('.postTopic .topic_content').html();
const summary = 'Reply: ' + $('.posts', elem).text();
return {
link,
title: $('.subject a', elem).attr('title'),
pubDate: parseDate($('.lastpost .time', elem).text()),
description: fullText ? summary + '<br><br>' + fullText : summary,
author: $('.author a', elem).text(),
};
});
const summary = 'Reply: ' + $('.posts', elem).text();
return {
link,
title: $('.subject a', elem).attr('title'),
pubDate: parseDate($('.lastpost .time', elem).text()),
description: fullText ? summary + '<br><br>' + fullText : summary,
author: $('.author a', elem).text(),
};
})
);

Expand Down
6 changes: 6 additions & 0 deletions lib/routes/bangumi.tv/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Bangumi 番组计划',
url: 'bangumi.tv',
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Route } from '@/types';
import { load } from 'cheerio';
import { config } from '@/config';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/:type/followrank',
categories: ['anime'],
example: '/bangumi/anime/followrank',
example: '/bangumi.tv/anime/followrank',
parameters: { type: '类型:anime - 动画,book - 图书,music - 音乐,game - 游戏,real - 三次元' },
features: {
requireConfig: false,
Expand Down Expand Up @@ -34,11 +33,7 @@ async function handler(ctx) {
}
const url = `https://bgm.tv/${type}`;

const response = await ofetch(url, {
headers: {
'User-Agent': config.trueUA,
},
});
const response = await ofetch(url);

const $ = load(response);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Route } from '@/types';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/tv/person/:id',
path: '/person/:id',
categories: ['anime'],
example: '/bangumi/tv/person/32943',
example: '/bangumi.tv/person/32943',
parameters: { id: '人物 id, 在人物页面的地址栏查看' },
features: {
requireConfig: false,
Expand All @@ -30,7 +30,7 @@ async function handler(ctx) {
// bangumi.tv未提供获取“人物信息”的API,因此仍需要通过抓取网页来获取
const personID = ctx.req.param('id');
const link = `https://bgm.tv/person/${personID}/works?sort=date`;
const { data: html } = await got(link);
const html = await ofetch(link);
const $ = load(html);
const personName = $('.nameSingle a').text();
const works = $('.item')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate, parseRelativeDate } from '@/utils/parse-date';

const getComments = async (subjectID, minLength) => {
// bangumi.tv未提供获取“吐槽(comments)”的API,因此仍需要通过抓取网页来获取
const link = `https://bgm.tv/subject/${subjectID}/comments`;
const { data: html } = await got(link);
const html = await ofetch(link);
const $ = load(html);
const title = $('.nameSingle').find('a').text();
const comments = $('.item')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);

import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';
import { getLocalName } from './utils';

const getEps = async (subjectID, showOriginalName) => {
const url = `https://api.bgm.tv/subject/${subjectID}?responseGroup=large`;
const { data: epsInfo } = await got(url);
const activeEps = [];

for (const e of epsInfo.eps) {
if (e.status === 'Air') {
activeEps.push(e);
}
}
const epsInfo = await ofetch(url);
const activeEps = epsInfo.eps.filter((e) => e.status === 'Air');

return {
title: getLocalName(epsInfo, showOriginalName),
link: `https://bgm.tv/subject/${subjectID}`,
description: epsInfo.summary,
item: activeEps.map((e) => ({
title: `ep.${e.sort} ${getLocalName(e, showOriginalName)}`,
description: art(path.resolve(__dirname, '../../templates/tv/ep.art'), {
description: art(path.join(__dirname, '../templates/ep.art'), {
e,
epsInfo,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { queryToBoolean } from '@/utils/readable-social';
import InvalidParameterError from '@/errors/types/invalid-parameter';

export const route: Route = {
path: '/tv/subject/:id/:type?/:showOriginalName?',
path: '/subject/:id/:type?/:showOriginalName?',
categories: ['anime'],
example: '/bangumi/tv/subject/328609/ep/true',
example: '/bangumi.tv/subject/328609/ep/true',
parameters: { id: '条目 id, 在条目页面的地址栏查看', type: '条目类型,可选值为 `ep`, `comments`, `blogs`, `topics`,默认为 `ep`', showOriginalName: '显示番剧标题原名,可选值 0/1/false/true,默认为 false' },
features: {
requireConfig: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { getLocalName } from './utils';

Expand All @@ -17,7 +17,7 @@ const getFromAPI = (type) => {
return async (subjectID, showOriginalName) => {
// 官方提供的条目API文档见 https://github.com/bangumi/api/blob/3f3fa6390c468816f9883d24be488e41f8946159/docs-raw/Subject-API.md
const url = `https://api.bgm.tv/subject/${subjectID}?responseGroup=large`;
const { data: subjectInfo } = await got(url);
const subjectInfo = await ofetch(url);
return {
title: `${getLocalName(subjectInfo, showOriginalName)}的 Bangumi ${mapping[type].cn}`,
link: `https://bgm.tv/subject/${subjectInfo.id}/${mapping[type].en}`,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

export const route: Route = {
path: '/tv/user/blog/:id',
path: '/user/blog/:id',
categories: ['anime'],
example: '/bangumi/tv/user/blog/sai',
example: '/bangumi.tv/user/blog/sai',
parameters: { id: '用户 id, 在用户页面地址栏查看' },
features: {
requireConfig: false,
Expand All @@ -22,6 +22,9 @@ export const route: Route = {
{
source: ['bgm.tv/user/:id'],
},
{
source: ['bangumi.tv/user/:id'],
},
],
name: '用户日志',
maintainers: ['nczitzk'],
Expand All @@ -30,11 +33,8 @@ export const route: Route = {

async function handler(ctx) {
const currentUrl = `https://bgm.tv/user/${ctx.req.param('id')}/blog`;
const response = await got({
method: 'get',
url: currentUrl,
});
const $ = load(response.data);
const response = await ofetch(currentUrl);
const $ = load(response);
const list = $('#entry_list div.item')
.find('h2.title')
.toArray()
Expand All @@ -51,8 +51,8 @@ async function handler(ctx) {
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const res = await got({ method: 'get', url: item.link });
const content = load(res.data);
const res = await ofetch(item.link);
const content = load(res);

item.description = content('#entry_content').html();
return item;
Expand Down
Loading

0 comments on commit 9a651fe

Please sign in to comment.