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 GDUFS news route and GDUFS xwxy news #17822

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions lib/routes/gdufs/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: 'gdufs.edu.cn',
lang: 'zh-CN',
};
89 changes: 89 additions & 0 deletions lib/routes/gdufs/news.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Route } from '@/types';
import { load } from 'cheerio';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

const site = 'https://www.gdufs.edu.cn';

export const route: Route = {
path: '/news',
categories: ['university'],
example: '/gdufs/news',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['https://www.gdufs.edu.cn/gwxw/gwxw1.htm', 'https://www.gdufs.edu.cn/'],
gz4zzxc marked this conversation as resolved.
Show resolved Hide resolved
},
],
name: '广东外语外贸大学-新闻',
gz4zzxc marked this conversation as resolved.
Show resolved Hide resolved
maintainers: ['gz4zzxc'],
handler,
url: 'https://www.gdufs.edu.cn/gwxw/gwxw1.htm',
gz4zzxc marked this conversation as resolved.
Show resolved Hide resolved
};

async function handler() {
const link = 'https://www.gdufs.edu.cn/gwxw/gwxw1.htm';

const response = await got(link);
const $ = load(response.body);
const list = $('ul.list_luntan li');

const items = await Promise.all(
list.toArray().map(async (element) => {
const item = $(element);
const href = item.find('a').attr('href') || '';
const title = item.find('h5').text().trim();
const day = item.find('h3').text().trim();
const yearMonth = item.find('h6').text().trim();
const dateString = yearMonth + '/' + day;
const fullLink = href.startsWith('http') ? href : new URL(href, site).href;

const pubDate = parseDate(dateString).toUTCString();

let description = '';
let author = '';

try {
const articleRes = await got(fullLink);
const $$ = load(articleRes.body);
description = $$('.v_news_content').html()?.trim() || '';

// 提取作者信息
const authorSpans = $$('.nav01 h6 .ll span');
authorSpans.each((_, el) => {
const text = $$(el).text().trim();
if (text.includes('责任编辑:')) {
author = text.replace('责任编辑:', '').trim();
} else if (text.includes('文字:')) {
author = text.replace('文字:', '').trim();
}
});
} catch {
description = '内容获取失败。';
}

return {
title,
link: fullLink,
description,
pubDate,
author,
};
})
);
gz4zzxc marked this conversation as resolved.
Show resolved Hide resolved

return {
title: '广东外语外贸大学-新闻',
link,
description: '广东外语外贸大学-新闻',
item: items,
};
}
89 changes: 89 additions & 0 deletions lib/routes/gdufs/xwxy/xwxy-news.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Route } from '@/types';
import { load } from 'cheerio';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/xwxy-news',
categories: ['university'],
example: '/gdufs/xwxy-news',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['xwxy.gdufs.edu.cn/xwzx/xyxw', 'xwxy.gdufs.edu.cn/'],
},
],
name: '广东外语外贸大学新闻学院-学院新闻',
gz4zzxc marked this conversation as resolved.
Show resolved Hide resolved
maintainers: ['gz4zzxc'],
handler,
url: 'xwxy.gdufs.edu.cn/xwzx/xyxw',
};

async function handler() {
const BASE_URL = 'https://xwxy.gdufs.edu.cn';
const link = `${BASE_URL}/xwzx/xyxw.htm`;

// 获取列表页面
const response = await got(link);
if (!response.body) {
throw new Error('No response body');
}
const $ = load(response.body);
const list = $('div.flex-center a.clearfix');

const items = list.toArray().map((element) => {
const item = $(element);
const href = item.attr('href') || '';
const dateText = item.find('i').text().trim();
const pubDate = parseDate(dateText).toUTCString();
return {
title: item.find('h5').text().trim(),
link: href.startsWith('http') ? href : new URL(href, BASE_URL).href,
pubDate,
};
});

// 获取文章详情
const fetchArticleDetail = async (item) => {
try {
const articleResponse = await got(item.link);
if (!articleResponse.body) {
throw new Error('No article body');
}
const $$ = load(articleResponse.body);
const content = $$('#vsb_content .v_news_content').html() || '';
const authors = $$('.show01 p i')
.map((_, el) => $$(el).text().trim())
.toArray();
Fixed Show fixed Hide fixed

return {
...item,
description: content,
author: authors.join(' '),
};
} catch {
return {
...item,
description: '无法获取内容',
author: '',
};
}
};

const enhancedItems = await Promise.all(items.map((item) => fetchArticleDetail(item)));

return {
title: '广外新传学院-学院新闻',
link,
description: '广东外语外贸大学新闻与传播学院官网-学院新闻',
item: enhancedItems,
};
}
Loading