fix: 豆瓣榜单与集合支持分页处理 (#14483)

This commit is contained in:
Kai Yang 2024-02-17 21:36:17 +08:00 committed by GitHub
parent 1886554878
commit ce19844a94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 46 additions and 32 deletions

View File

@ -8,40 +8,54 @@ module.exports = async (ctx) => {
const routeParams = Object.fromEntries(new URLSearchParams(ctx.params.routeParams));
const playable = fallback(undefined, queryToInteger(routeParams.playable), 0);
const score = fallback(undefined, queryToInteger(routeParams.score), 0);
const url = `https://m.douban.com/rexxar/api/v2/subject_collection/${type}/items?playable=${playable}`;
const response = await got({
method: 'get',
url,
headers: {
Referer: `https://m.douban.com/subject_collection/${type}`,
},
});
const description = response.data.subject_collection.description;
const items = response.data.subject_collection_items
.filter((item) => {
const rate = item.rating ? item.rating.value : 0;
return rate >= score; // 保留rate大于等于score的项and过滤无评分项
})
.map((item) => {
const title = item.title;
const link = item.url;
const description = art(path.join(__dirname, '../templates/list_description.art'), {
ranking_value: item.ranking_value,
title,
original_title: item.original_title,
rate: item.rating ? item.rating.value : null,
card_subtitle: item.card_subtitle,
description: item.cards ? item.cards[0].content : item.abstract,
cover: item.cover_url || item.cover?.url,
});
return {
title,
link,
description,
};
let start = 0;
const count = 50;
let items = [];
let title = '';
let description = '';
let total = null;
while (total === null || start < total) {
const url = `https://m.douban.com/rexxar/api/v2/subject_collection/${type}/items?playable=${playable}&start=${start}&count=${count}`;
// eslint-disable-next-line no-await-in-loop
const response = await got({
method: 'get',
url,
headers: {
Referer: `https://m.douban.com/subject_collection/${type}`,
},
});
title = response.data.subject_collection.name;
description = response.data.subject_collection.description;
total = response.data.total;
const newItems = response.data.subject_collection_items
.filter((item) => {
const rate = item.rating ? item.rating.value : 0;
return rate >= score; // 保留rate大于等于score的项and过滤无评分项
})
.map((item) => {
const title = item.title;
const link = item.url;
const description = art(path.join(__dirname, '../templates/list_description.art'), {
ranking_value: item.ranking_value,
title,
original_title: item.original_title,
rate: item.rating ? item.rating.value : null,
card_subtitle: item.card_subtitle,
description: item.cards ? item.cards[0].content : item.abstract,
cover: item.cover_url || item.cover?.url,
});
return {
title,
link,
description,
};
});
items = [...items, ...newItems];
start += count;
}
ctx.state.data = {
title: `豆瓣 - ${response.data.subject_collection.name}`,
title: `豆瓣 - ${title}`,
link: `https://m.douban.com/subject_collection/${type}`,
item: items,
description,