Skip to content

Commit

Permalink
fix(route): taptap (DIYgod#17732)
Browse files Browse the repository at this point in the history
* fix(route): taptap

* fix(route): correct language code formatting in TapTap routes
  • Loading branch information
TonyRL authored Nov 27, 2024
1 parent cc7062d commit 4b64e22
Show file tree
Hide file tree
Showing 13 changed files with 722 additions and 290 deletions.
29 changes: 29 additions & 0 deletions lib/routes/taptap/changelog-cn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Route } from '@/types';
import { handler } from './common/changelog';

export const route: Route = {
path: '/changelog/:id/:lang?',
categories: ['game'],
example: '/taptap/changelog/60809/en_US',
parameters: {
id: '游戏 ID,游戏主页 URL 中获取',
lang: '语言,默认使用 `zh_CN`,亦可使用 `en_US`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.taptap.cn/app/:id'],
target: '/changelog/:id',
},
],
name: '游戏更新',
maintainers: ['hoilc', 'ETiV'],
handler,
};
34 changes: 34 additions & 0 deletions lib/routes/taptap/changelog-intl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Route } from '@/types';
import { handler } from './common/changelog';

export const route: Route = {
path: '/intl/changelog/:id/:lang?',
categories: ['game'],
example: '/taptap/intl/changelog/191001/zh_TW',
parameters: {
id: "Game's App ID, you may find it from the URL of the Game",
lang: 'Language, checkout the table below for possible values, default is `en_US`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.taptap.io/app/:id'],
target: '/intl/changelog/:id',
},
],
name: "Game's Changelog",
maintainers: ['hoilc', 'ETiV'],
handler,
description: `Language Code
| English (US) | 繁體中文 | 한국어 | 日本語 |
| ------------ | -------- | ------ | ------ |
| en_US | zh_TW | ko_KR | ja_JP |`,
};
71 changes: 0 additions & 71 deletions lib/routes/taptap/changelog.ts

This file was deleted.

40 changes: 40 additions & 0 deletions lib/routes/taptap/common/changelog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { getRootUrl, appDetail, X_UA } from '../utils';

export async function handler(ctx) {
const requestPath = ctx.req.path.replace('/taptap', '');
const isIntl = requestPath.startsWith('/intl/');
const id = ctx.req.param('id');
const lang = ctx.req.param('lang') ?? (isIntl ? 'en_US' : 'zh_CN');

const url = `${getRootUrl(isIntl)}/app/${id}`;

const detail = await appDetail(id, lang, isIntl);

const appImg = detail.app.icon.original_url;
const appName = detail.app.title;
const appDescription = `${appName}${detail.app.developers ? ' by' + detail.app.developers.map((item) => item.name).join(' & ') : ''}`;

const response = await ofetch(`${getRootUrl(isIntl)}/webapiv2/apk/v1/list-by-app?app_id=${id}&from=0&limit=10&${X_UA(lang)}`, {
headers: {
Referer: url,
},
});

const list = response.data.list;

return {
title: `TapTap 更新记录 ${appName}`,
description: appDescription,
link: url,
image: appImg,
item: list.map((item) => ({
title: `${appName} / ${item.version_label}`,
description: item.whatsnew.text,
pubDate: parseDate(item.update_date, 'X'),
link: url,
guid: item.version_label,
})),
};
}
126 changes: 126 additions & 0 deletions lib/routes/taptap/common/review.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
import { getRootUrl, appDetail, X_UA } from '../utils';

/*
const sortMap = {
default: {
en_US: 'Default',
zh_CN: '预设',
zh_TW: '預設',
},
recent: {
en_US: 'Latest',
zh_CN: '最新',
zh_TW: '最新',
},
hot: {
en_US: 'Popular',
zh_CN: '热门',
zh_TW: '熱門',
},
spent: {
en_US: 'Play Time',
zh_CN: '游戏时长',
zh_TW: '遊戲時長',
},
};
const intlSortMap = {
helpful: {
en_US: 'Most Helpful',
zh_TW: '最有幫助',
ja_JP: '最も役立つ',
ko_KR: '가장 도움이 된',
},
recent: {
en_US: 'Most Recent',
zh_TW: '最新',
ja_JP: '最も最近',
ko_KR: '최근순',
},
};
*/

const makeSortParam = (isIntl: boolean, order: string) => {
if (isIntl) {
if (order === 'helpful' || order === 'recent') {
return `type=${order}`;
}
return 'type=helpful';
} else {
if (order === 'new' || order === 'hot') {
return `sort=${order}`;
}
return 'sort=hot';
}
};

const fetchMainlandItems = async (params) => {
const id = params.id;
const order = params.order ?? 'hot';
const lang = params.lang ?? 'zh_CN';

let url = `${getRootUrl(false)}/webapiv2/review/v2/list-by-app?app_id=${id}&limit=10`;
url += `&${makeSortParam(false, order)}`;
url += `&${X_UA(lang)}`;

const reviewListResponse = await ofetch(url);

return reviewListResponse.data.list.map((review) => {
const author = review.moment.author.user.name;
const score = review.moment.review.score;
return {
title: `${author} - ${score}星`,
author,
description: review.moment.review.contents.text + (review.moment.review.contents.images ? review.moment.review.contents.images.map((img) => `<img src="${img.original_url}">`).join('') : ''),
link: `${getRootUrl(false)}/review/${review.moment.review.id}`,
pubDate: parseDate(review.moment.publish_time, 'X'),
};
});
};

const fetchIntlItems = async (params) => {
const id = params.id;
const order = params.order ?? 'helpful';
const lang = params.lang ?? 'en_US';

let url = `${getRootUrl(true)}/webapiv2/feeds/v3/by-app?app_id=${id}&limit=10`;
url += `&${makeSortParam(true, order)}`;
url += `&${X_UA(lang)}`;

const reviewListResponse = await ofetch(url);

return reviewListResponse.data.list.map((review) => {
const author = review.post.user.name;
const score = review.post.list_fields.app_ratings[id].score;
return {
title: `${author} - ${'★'.repeat(score)}`,
author,
description: review.post.list_fields.summary || review.post.list_fields.title,
link: `${getRootUrl(true)}/post/${review.post.id_str}`,
pubDate: parseDate(review.post.published_time, 'X'),
};
});
};

export async function handler(ctx) {
const requestPath = ctx.req.path.replace('/taptap', '');
const isIntl = requestPath.startsWith('/intl/');
const id = ctx.req.param('id');
const order = ctx.req.param('order') ?? 'default';
const lang = ctx.req.param('lang') ?? (isIntl ? 'en_US' : 'zh_CN');

const detail = await appDetail(id, lang, isIntl);
const appImg = detail.app.icon.original_url;
const appName = detail.app.title;

const items = isIntl ? await fetchIntlItems({ id, order, lang }) : await fetchMainlandItems({ id, order, lang });

return {
title: `TapTap 评价 ${appName}`,
link: `${getRootUrl(isIntl)}/app/${id}/review?${makeSortParam(isIntl, order)}`,
image: appImg,
item: items,
};
}
4 changes: 2 additions & 2 deletions lib/routes/taptap/namespace.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'TapTap 中国',
url: 'taptap.com',
name: 'TapTap',
url: 'www.taptap.io',
description: `:::warning
由于区域限制,需要在有国内 IP 的机器上自建才能正常获取 RSS。\
而对于《TapTap 国际版》则需要部署在具有海外出口的 IP 上才可正常获取 RSS。
Expand Down
33 changes: 33 additions & 0 deletions lib/routes/taptap/review-cn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Route } from '@/types';
import { handler } from './common/review';

export const route: Route = {
path: '/review/:id/:order?/:lang?',
categories: ['game'],
example: '/taptap/review/142793/hot',
parameters: {
id: '游戏 ID,游戏主页 URL 中获取',
order: '排序方式,空为综合,可选如下',
lang: '语言,`zh-CN` 或 `zh-TW`,默认为 `zh-CN`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.taptap.cn/app/:id/review', 'www.taptap.cn/app/:id'],
target: '/review/:id',
},
],
name: '游戏评价',
maintainers: ['hoilc', 'TonyRL'],
handler,
description: `| 最新 | 综合 |
| --- | --- |
| new | hot |`,
};
41 changes: 41 additions & 0 deletions lib/routes/taptap/review-intl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Route } from '@/types';
import { handler } from './common/review';

export const route: Route = {
path: '/intl/review/:id/:order?/:lang?',
categories: ['game'],
example: '/taptap/intl/review/82354/recent',
parameters: {
id: "Game's App ID, you may find it from the URL of the Game",
order: 'Sort Method, default is `helpful`, checkout the table below for possible values',
lang: 'Language, checkout the table below for possible values, default is `en_US`',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['www.taptap.io/app/:id/review', 'www.taptap.io/app/:id'],
target: '/intl/review/:id',
},
],
name: 'Ratings & Reviews',
maintainers: ['hoilc', 'TonyRL', 'ETiV'],
handler,
description: `Sort Method
| Most Helpful | Most Recent |
| ------------ | ----------- |
| helpful | recent |
Language Code
| English (US) | 繁體中文 | 한국어 | 日本語 |
| ------------ | -------- | ------ | ------ |
| en_US | zh_TW | ko_KR | ja_JP |`,
};
Loading

0 comments on commit 4b64e22

Please sign in to comment.