Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): bahamut recover (巴哈姆特-创作大厅 废弃路由恢复) #18079

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions lib/routes-deprecated/bahamut/creation.js

This file was deleted.

65 changes: 0 additions & 65 deletions lib/routes-deprecated/bahamut/utils.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const utils = require('./utils');
import { processFeed } from './utils';
import { Route } from '@/types';

const type_map = {
0: '達人專欄',
Expand Down Expand Up @@ -56,18 +57,43 @@ const subcategory_map = {
34: '其他',
};

module.exports = async (ctx) => {
const type = ctx.params.type ?? '1';
const category = ctx.params.category ?? '0';
const subcategory = ctx.params.subcategory ?? '0';
async function handler(ctx) {
const { type = '1', category = '0', subcategory = '0' } = ctx.req.param();

const url = `https://home.gamer.com.tw/index.php?k1=${category}&k2=${subcategory}&vt=${type}&sm=3`;

const { items } = await utils.ProcessFeed(url, ctx);
const { items } = await processFeed(url);

ctx.state.data = {
title: `巴哈姆特创作大厅${category === '0' ? '' : ' - ' + category_map[category]}${subcategory === '0' ? '' : ' - ' + subcategory_map[subcategory]} - ${type_map[type]}`,
return {
title: `巴哈姆特的創作大廳${category === '0' ? '' : ' - ' + category_map[category]}${subcategory === '0' ? '' : ' - ' + subcategory_map[subcategory]} - ${type_map[type]}`,
link: url,
item: items,
};
}

export const route: Route = {
path: '/creation_index/:category?/:subcategory?/:type?',
categories: ['anime', 'social-media'],
example: '/bahamut/creation_index/0/0/0',
parameters: {
category: '分类',
subcategory: '子分类',
type: '类型',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['home.gamer.com.tw/index.php?k1=', 'home.gamer.com.tw/index.php?k2=', 'home.gamer.com.tw/index.php?vt='],
},
],
name: '創作大廳 - 首頁',
maintainers: ['hoilc', 'bGZo'],
handler,
};
41 changes: 41 additions & 0 deletions lib/routes/bahamut/creation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Route } from '@/types';
import { processFeed } from './utils';

async function handler(ctx) {
const { author = '', category } = ctx.req.param();
const url = category ? `https://home.gamer.com.tw/creationCategory.php?owner=${author}&c=${category}` : `https://home.gamer.com.tw/creation.php?owner=${author}`;

const { title, items } = await processFeed(url);

return {
title,
link: url,
item: items,
};
}

export const route: Route = {
path: '/creation/:author/:category?',
categories: ['anime', 'social-media'],
example: '/bahamut/creation/a1102kevin',
parameters: {
author: '作者',
category: '分类',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['home.gamer.com.tw/homeindex.php'],
},
],
name: '創作大廳',
maintainers: ['hoilc', 'bGZo'],
handler,
};
7 changes: 7 additions & 0 deletions lib/routes/bahamut/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: '巴哈姆特',
url: 'home.gamer.com.tw',
lang: 'zh-TW',
};
65 changes: 65 additions & 0 deletions lib/routes/bahamut/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import got from '@/utils/got';
import { load } from 'cheerio';
import cache from '@/utils/cache';

const base = 'https://home.gamer.com.tw/';

export const processFeed = async (url: string) => {
const response = await got.get(url);
const $ = load(response.data);

const title = $('title').text();
const list = $('.BH-lbox > .HOME-mainbox1').toArray();

const parseContent = (htmlString) => {
const $ = load(htmlString);
const content = $('.MSG-list8C');

const images = $('img');
for (const image of images) {
$(image).attr('src', $(image).attr('data-src'));
}

return {
description: content.html(),
};
};

const items = await Promise.all(
list.map(async (item) => {
const $ = load(item);
const title = $('.HOME-mainbox1b > h1 > a');
const link = base + title.attr('href');
const author = $('.HOME-mainbox1b > .ST1 > a').text();
const time = $('.HOME-mainbox1b > .ST1').text().split('│')[1];

const cacheContent = await cache.get(link);
if (cacheContent) {
return JSON.parse(cacheContent);
}

const topic = {
title: title.text().trim(),
link,
author,
pubDate: new Date(time),
description: '',
};

try {
const detail_response = await got.get(link);
const result = parseContent(detail_response.data);
if (!result.description) {
result.description = '';
}
topic.description = result.description;
} catch {
topic.description = '';
}
cache.set(link, JSON.stringify(topic));
return topic;
})
);

return { title, items };
};
Loading