refactor: split bangumi (#17293)
* refactor: split bangumi * fix: template path * feat!: remove `/user/wish/:id` since it's equivalent to /user/collections/:id/2/1 * refactor: use ofetch
This commit is contained in:
@@ -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;
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: '萌番组',
|
||||
url: 'bangumi.online',
|
||||
};
|
||||
@@ -2,5 +2,5 @@ import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'アニメ新番組',
|
||||
url: 'bangumi.moe',
|
||||
url: 'bangumi.online',
|
||||
};
|
||||
@@ -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,
|
||||
@@ -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} 集`,
|
||||
}),
|
||||
@@ -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,
|
||||
@@ -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];
|
||||
@@ -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,
|
||||
});
|
||||
@@ -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,
|
||||
@@ -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')
|
||||
@@ -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,
|
||||
@@ -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(),
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'Bangumi 番组计划',
|
||||
url: 'bangumi.tv',
|
||||
};
|
||||
+2
-7
@@ -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,
|
||||
@@ -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);
|
||||
|
||||
@@ -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,
|
||||
@@ -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')
|
||||
+2
-2
@@ -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')
|
||||
@@ -1,7 +1,7 @@
|
||||
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';
|
||||
@@ -9,14 +9,8 @@ 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),
|
||||
@@ -24,7 +18,7 @@ const getEps = async (subjectID, showOriginalName) => {
|
||||
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,
|
||||
}),
|
||||
@@ -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,
|
||||
+2
-2
@@ -1,4 +1,4 @@
|
||||
import got from '@/utils/got';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { getLocalName } from './utils';
|
||||
|
||||
@@ -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}`,
|
||||
@@ -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,
|
||||
@@ -22,6 +22,9 @@ export const route: Route = {
|
||||
{
|
||||
source: ['bgm.tv/user/:id'],
|
||||
},
|
||||
{
|
||||
source: ['bangumi.tv/user/:id'],
|
||||
},
|
||||
],
|
||||
name: '用户日志',
|
||||
maintainers: ['nczitzk'],
|
||||
@@ -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()
|
||||
@@ -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;
|
||||
+27
-13
@@ -1,5 +1,5 @@
|
||||
import { Route } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import timezone from '@/utils/timezone';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { config } from '@/config';
|
||||
@@ -53,9 +53,9 @@ const getTypeNames = (subjectType) => {
|
||||
};
|
||||
|
||||
export const route: Route = {
|
||||
path: '/tv/user/collections/:id/:subjectType/:type',
|
||||
path: '/user/collections/:id/:subjectType/:type',
|
||||
categories: ['anime'],
|
||||
example: '/bangumi/tv/user/collections/sai/1/1',
|
||||
example: '/bangumi.tv/user/collections/sai/1/1',
|
||||
parameters: {
|
||||
id: '用户 id, 在用户页面地址栏查看',
|
||||
subjectType: {
|
||||
@@ -89,9 +89,26 @@ export const route: Route = {
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [],
|
||||
radar: [
|
||||
{
|
||||
source: ['bgm.tv/anime/list/:id'],
|
||||
target: '/bangumi.tv/user/collections/:id/all/all',
|
||||
},
|
||||
{
|
||||
source: ['bangumi.tv/anime/list/:id'],
|
||||
target: '/bangumi.tv/user/collections/:id/all/all',
|
||||
},
|
||||
{
|
||||
source: ['bgm.tv/anime/list/:id/wish'],
|
||||
target: '/bangumi.tv/user/collections/:id/2/1',
|
||||
},
|
||||
{
|
||||
source: ['bangumi.tv/anime/list/:id/wish'],
|
||||
target: '/bangumi.tv/user/collections/:id/2/1',
|
||||
},
|
||||
],
|
||||
name: 'Bangumi 用户收藏列表',
|
||||
maintainers: ['youyou-sudo'],
|
||||
maintainers: ['youyou-sudo', 'honue'],
|
||||
handler,
|
||||
};
|
||||
|
||||
@@ -125,30 +142,28 @@ async function handler(ctx) {
|
||||
}
|
||||
|
||||
const userDataUrl = `https://api.bgm.tv/v0/users/${userId}`;
|
||||
const userData = await got(userDataUrl, {
|
||||
method: 'get',
|
||||
const userData = await ofetch(userDataUrl, {
|
||||
headers: {
|
||||
'User-Agent': config.trueUA,
|
||||
},
|
||||
});
|
||||
|
||||
const collectionDataUrl = `https://api.bgm.tv/v0/users/${userId}/collections?${subjectType && subjectType !== 'all' ? `subject_type=${subjectType}` : ''}${type && type !== 'all' ? `&type=${type}` : ''}`;
|
||||
const collectionData = await got(collectionDataUrl, {
|
||||
method: 'get',
|
||||
const collectionData = await ofetch(collectionDataUrl, {
|
||||
headers: {
|
||||
'User-Agent': config.trueUA,
|
||||
},
|
||||
});
|
||||
|
||||
const userNickname = userData.data.nickname;
|
||||
const items = collectionData.data.data.map((item) => {
|
||||
const userNickname = userData.nickname;
|
||||
const items = collectionData.data.map((item) => {
|
||||
const titles = item.subject.name_cn || item.subject.name;
|
||||
const updateTime = item.updated_at;
|
||||
const subjectId = item.subject_id;
|
||||
|
||||
return {
|
||||
title: `${type === 'all' ? `${getTypeNames(item.subject_type)[item.type]}:` : ''}${titles}`,
|
||||
description: art(path.join(__dirname, '../../templates/tv/subject.art'), {
|
||||
description: art(path.join(__dirname, '../templates/subject.art'), {
|
||||
routeSubjectType: subjectType,
|
||||
subjectTypeName: subjectTypeNames[item.subject_type],
|
||||
subjectType: item.subject_type,
|
||||
@@ -160,7 +175,6 @@ async function handler(ctx) {
|
||||
}),
|
||||
link: `https://bgm.tv/subject/${subjectId}`,
|
||||
pubDate: timezone(parseDate(updateTime), 0),
|
||||
urls: `https://bgm.tv/subject/${subjectId}`,
|
||||
};
|
||||
});
|
||||
return {
|
||||
@@ -1,62 +0,0 @@
|
||||
import { Route } from '@/types';
|
||||
import got from '@/utils/got';
|
||||
import { load } from 'cheerio';
|
||||
import timezone from '@/utils/timezone';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { config } from '@/config';
|
||||
export const route: Route = {
|
||||
path: '/tv/user/wish/:id',
|
||||
categories: ['anime'],
|
||||
example: '/bangumi/tv/user/wish/sai',
|
||||
parameters: { id: '用户 id, 在用户页面地址栏查看' },
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['bgm.tv/anime/list/:id/wish'],
|
||||
},
|
||||
],
|
||||
name: '用户想看',
|
||||
maintainers: ['honue'],
|
||||
handler,
|
||||
};
|
||||
|
||||
async function handler(ctx) {
|
||||
const userid = ctx.req.param('id');
|
||||
const url = `https://bgm.tv/anime/list/${userid}/wish`;
|
||||
const response = await got({
|
||||
url,
|
||||
method: 'get',
|
||||
headers: {
|
||||
'User-Agent': config.trueUA,
|
||||
},
|
||||
});
|
||||
const $ = load(response.body);
|
||||
|
||||
const username = $('.name').find('a').html();
|
||||
const items = $('#browserItemList')
|
||||
.find('li')
|
||||
.toArray()
|
||||
.map((item) => {
|
||||
const aTag = $(item).find('h3').children('a');
|
||||
const jdate = $(item).find('.collectInfo').find('span').html();
|
||||
return {
|
||||
title: aTag.html(),
|
||||
link: 'https://bgm.tv' + aTag.attr('href'),
|
||||
pubDate: timezone(parseDate(jdate), 0),
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
title: `${username}想看的动画`,
|
||||
link: url,
|
||||
item: items,
|
||||
description: `${username}想看的动画列表`,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user