Skip to content

Commit

Permalink
Merge pull request #2496 from DIYgod/master
Browse files Browse the repository at this point in the history
[pull] master from diygod:master
  • Loading branch information
pull[bot] authored Apr 25, 2024
2 parents 2bbda15 + a0e084d commit 86175d8
Show file tree
Hide file tree
Showing 20 changed files with 587 additions and 327 deletions.
59 changes: 0 additions & 59 deletions lib/routes/baai/comments.ts

This file was deleted.

31 changes: 10 additions & 21 deletions lib/routes/baai/events.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';

import { baseUrl, apiHost, parseEventDetail, parseItem } from './utils';

export const route: Route = {
path: '/hub/events',
categories: ['programming'],
example: '/baai/hub/events',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['hub.baai.ac.cn/events', 'hub.baai.ac.cn/'],
Expand All @@ -28,18 +19,16 @@ export const route: Route = {
url: 'hub.baai.ac.cn/events',
};

async function handler(ctx) {
const responses = await got.all(
Array.from(
{
// first 2 pages
length: (ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 16) / 8,
},
(_, v) => `${apiHost}/api/v1/events?page=${v + 1}`
).map((url) => got.post(url))
);
async function handler() {
const response = await ofetch(`${apiHost}/api/v1/events`, {
method: 'POST',
body: {
page: 1,
tag_id: '',
},
});

const list = responses.flatMap((response) => response.data.data).map((item) => parseItem(item));
const list = response.data.map((item) => parseItem(item));

const items = await Promise.all(
list.map((item) =>
Expand Down
88 changes: 59 additions & 29 deletions lib/routes/baai/hub.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,85 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';

import { baseUrl, apiHost, parseEventDetail, parseItem } from './utils';
import { baseUrl, apiHost, getTagsData, parseEventDetail, parseItem } from './utils';
import InvalidParameterError from '@/errors/types/invalid-parameter';

export const route: Route = {
path: ['/hub/:tagId/:sort?/:range?', '/hub/:tagId/:sort?', '/hub/:sort?'],
name: 'Unknown',
maintainers: [],
path: ['/hub/:tagId?/:sort?/:range?'],
categories: ['programming'],
example: '/baai/hub',
parameters: {
tagId: '社群 ID,可在 [社群页](https://hub.baai.ac.cn/taglist) 或 URL 中找到',
sort: '排序,见下表,默认为 `new`',
range: '时间跨度,仅在排序 `readCnt` 时有效',
},
description: `排序
| 最新 | 最热 |
| ---- | ------- |
| new | readCnt |
时间跨度
| 3 天 | 本周 | 本月 |
| ---- | ---- | ---- |
| 3 | 7 | 30 |`,
radar: [
{
source: ['baai.ac.cn/'],
target: (_params, url) => {
const searchParams = new URL(url).searchParams;
const tagId = searchParams.get('tag_id');
const sort = searchParams.get('sort');
const range = searchParams.get('time_range');
return `/baai/hub${tagId ? `/${tagId}` : ''}${sort ? `/${sort}` : ''}${range ? `/${range}` : ''}`;
},
},
],
name: '智源社区',
maintainers: ['TonyRL'],
handler,
};

async function handler(ctx) {
const { tagId, sort = 'new', range } = ctx.req.param();
const { tagId = '', sort = 'new', range } = ctx.req.param();

let title, description, brief, iconUrl;
if (tagId) {
const { data } = await got(`${apiHost}/api/v1/tags`, {
searchParams: {
id: tagId,
},
});
title = data.data[0].title;
description = data.data[0].description;
brief = data.data[0].brief;
iconUrl = data.data[0].icon_url;
const tagsData = await getTagsData();

const tag = (tagsData as Record<string, string>[]).find((tag) => tag.id === tagId);
if (tag) {
title = tag.title;
description = tag.description;
brief = tag.brief;
iconUrl = tag.iconUrl;
} else {
throw new InvalidParameterError('Tag not found');
}
}

const responses = await got.all(
Array.from(
{
// first 3 pages
length: (ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 18) / 6,
},
(_, v) => `${apiHost}/api/v1/story/list?page=${v + 1}&sort=${sort}${tagId ? `&tag_id=${tagId}` : ''}${range ? `&time_range=${range}` : ''}`
).map((url) => got.post(url))
);
const response = await ofetch(`${apiHost}/api/v1/story/list`, {
method: 'POST',
query: {
page: 1,
sort,
tag_id: tagId,
time_range: range,
},
});

const list = responses
.filter((response) => response.data.data)
.flatMap((response) => response.data.data)
.map((item) => parseItem(item));
const list = response.data.map((item) => parseItem(item));

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
if (item.eventId) {
item.description = await parseEventDetail(item);
} else {
const { data: response } = await got(item.link);
const response = await ofetch(item.link);
const $ = load(response);
item.description = item.is_event ? $('div.box2').html() : $('.post-content').html();
}
Expand Down
23 changes: 19 additions & 4 deletions lib/routes/baai/utils.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { destr } from 'destr';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
import cache from '@/utils/cache';

const baseUrl = 'https://hub.baai.ac.cn';
const eventUrl = 'https://event.baai.ac.cn';
const apiHost = 'https://hub-api.baai.ac.cn';

const getTagsData = () =>
cache.tryGet('baai:tags', async () => {
const { data } = await ofetch(`${apiHost}/api/v1/tags`);
return data.map((item) => ({
id: item.id,
title: item.title,
description: item.description,
brief: item.brief,
iconUrl: item.icon_url,
}));
});

const parseItem = (item) => ({
link: item.is_event ? `${eventUrl}/activities/${item.event_info.id}` : `${baseUrl}/view/${item.story_id}`,
title: item.is_event ? item.event_info.name : item.story_info.title,
Expand All @@ -16,12 +30,13 @@ const parseItem = (item) => ({
});

const parseEventDetail = async (item) => {
const { data } = await got(`${eventUrl}/api/api/Activity/IntroductionTypes`, {
searchParams: {
const data = await ofetch(`${eventUrl}/api/api/Activity/IntroductionTypes`, {
query: {
activityId: item.eventId,
},
parseResponse: (txt) => destr(txt),
});
return data.data.ac_desc + data.data.ac_desc_two;
};

export { baseUrl, eventUrl, apiHost, parseItem, parseEventDetail };
export { baseUrl, eventUrl, apiHost, getTagsData, parseItem, parseEventDetail };
19 changes: 5 additions & 14 deletions lib/routes/caixin/weekly.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Route } from '@/types';
import { DataItem, Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
Expand All @@ -8,15 +8,6 @@ export const route: Route = {
path: '/weekly',
categories: ['traditional-media'],
example: '/caixin/weekly',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['weekly.caixin.com/', 'weekly.caixin.com/*'],
Expand All @@ -38,16 +29,16 @@ async function handler(ctx) {
...$('.mi')
.toArray()
.map((item) => ({
link: $(item).find('a').attr('href'),
link: $(item).find('a').attr('href')?.replace('http:', 'https:'),
})),
...$('.xsjCon a')
.toArray()
.map((item) => ({
link: $(item).attr('href'),
})),
].slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 10);
].slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 10) as DataItem[];

const items = await Promise.all(
const items = (await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const { data } = await got(item.link);
Expand All @@ -73,7 +64,7 @@ async function handler(ctx) {
return item;
})
)
);
)) as DataItem[];

return {
title: $('head title')
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/douyin/live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function handler(ctx) {
await page.setRequestInterception(true);

page.on('request', (request) => {
request.resourceType() === 'document' || request.resourceType() === 'script' || request.resourceType() === 'xhr' ? request.continue() : request.abort();
request.resourceType() === 'document' || request.resourceType() === 'stylesheet' || request.resourceType() === 'script' || request.resourceType() === 'xhr' ? request.continue() : request.abort();
});
page.on('response', async (response) => {
const request = response.request();
Expand Down
67 changes: 67 additions & 0 deletions lib/routes/ecnu/jwc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';

const type = (filename) => filename.split('.').pop();

export const route: Route = {
path: '/jwc',
categories: ['university'],
example: '/ecnu/jwc',
radar: [
{
source: ['www.jwc.ecnu.edu.cn', 'www.ecnu.edu.cn'],
target: '/tzgg',
},
],
name: '教务处通知',
maintainers: ['markbang'],
handler: async () => {
const baseUrl = 'http://www.jwc.ecnu.edu.cn/';

const response = await got(`${baseUrl}tzggwwxsgg/list.htm`);
const $ = load(response.data);
const links = $('.col_news_con ul.news_list > li')
.map((_, el) => ({
pubDate: timezone(parseDate($(el).find('.news_date').text()), 8),
link: new URL($(el).find('a').attr('href'), baseUrl).toString(),
title: $(el).find('a').text(),
}))
.get();
const items = await Promise.all(
links.map((item) =>
cache.tryGet(item.link, async () => {
if (type(item.link) === 'htm') {
try {
const { data } = await got(item.link, {
https: {
rejectUnauthorized: false,
},
});
const $ = load(data);
item.description = $('div.article')?.html()?.replaceAll('src="/', `src="${baseUrl}/`)?.replaceAll('href="/', `href="${baseUrl}/`)?.trim();
return item;
} catch {
// intranet
item.description = '请进行统一身份认证之后再访问';
return item;
}
} else {
// file to download
item.description = '点击认证后访问内容';
return item;
}
})
)
);

return {
title: '教务处通知',
link: 'http://www.jwc.ecnu.edu.cn/tzggwwxsgg/list.htm',
item: items,
};
},
};
Loading

0 comments on commit 86175d8

Please sign in to comment.