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): add bbs guozaoke route #17170

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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
6 changes: 6 additions & 0 deletions lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ export type Config = {
google: {
fontsApiKey?: string;
};
guozaoke: {
cookies?: string;
};
hefeng: {
key?: string;
};
Expand Down Expand Up @@ -549,6 +552,9 @@ const calculateValue = () => {
google: {
fontsApiKey: envs.GOOGLE_FONTS_API_KEY,
},
guozaoke: {
cookies: envs.GUOZAOKE_COOKIES,
},
hefeng: {
// weather
key: envs.HEFENG_KEY,
Expand Down
101 changes: 101 additions & 0 deletions lib/routes/guozaoke/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { Route } from '@/types';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseRelativeDate } from '@/utils/parse-date';
import { config } from '@/config';
import cache from '@/utils/cache';
import asyncPool from 'tiny-async-pool';

export const route: Route = {
path: '',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

path cannot be empty

categories: ['bbs'],
example: '/guozaoke',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
name: 'guozaoke',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Route name should not be the same as namespace name.

maintainers: ['xiaoshame'],
handler,
url: 'guozaoke.com/',
};

async function getContent(link) {
return await cache.tryGet(link, async () => {
const url = `https://www.guozaoke.com${link}`;
const res = await got({
method: 'get',
url,
headers: {
Cookie: config.guozaoke.cookies,
'User-Agent': config.ua,
},
});

const $ = load(res.data);
let content = $('div.ui-content').html();
content = content ? content.trim() : '';
const comments = $('.reply-item').map((i, el) => {
const $el = $(el);
const comment = $el.find('span.content').text().trim();
const author = $el.find('span.username').text();
return {
comment,
author,
};
});
if (comments && comments.length > 0) {
for (const item of comments) {
content += '<br>' + item.author + ': ' + item.comment;
}
}

return content;
});
}

async function handler() {
const url = `https://www.guozaoke.com/`;
const res = await got({
method: 'get',
url,
headers: {
Cookie: config.guozaoke.cookies,
'User-Agent': config.ua,
},
});
const $ = load(res.data);

const list = $('div.topic-item').toArray();
const maxItems = 20; // 最多取20个数据
const items = [];
for await (const data of asyncPool(1, list.slice(0, maxItems), async (i) => {
const $item = $(i);
const title = $item.find('h3.title a').text();
const link = $item.find('h3.title a').attr('href');
xiaoshame marked this conversation as resolved.
Show resolved Hide resolved
const author = $item.find('span.username a').text();
const lastTouched = $item.find('span.last-touched').text();
const time = parseRelativeDate(lastTouched);
const description = await getContent(link);
return {
title,
description,
author,
link: link.split('#')[0],
pubDate: time,
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please cache the returned object.

#17170 (comment) is commented on lines 89 to 95 so the returned object is expected to have

return {
title,
description,
author,
link: link.split('#')[0],
pubDate: time,
};

})) {
items.push(data);
}

return {
title: '过早客',
link: url,
item: items,
};
}
6 changes: 6 additions & 0 deletions lib/routes/guozaoke/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: 'guozaoke',
url: 'guozaoke.com',
};