feat(route/qwen): replace deprecated qwen blog route with new one (#21944)
* feat: replace deprecated qwen blog route * fix: replace wrong field and add param desc * fix: remove unnecessary param * fix: use import type * refactor: qwen blog * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * error handling and tidy * tidy * fix: remove unnecessary header --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot Autofix powered by AI
parent
314320c588
commit
0b2b8e0e66
@@ -0,0 +1,54 @@
|
||||
import { load } from 'cheerio';
|
||||
|
||||
import type { Data, DataItem, Route } from '@/types';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
export const buildBlogItem = (item: any, base: string): DataItem => {
|
||||
const $ = load(item.content || item.extra.introduction || '');
|
||||
const content = $('.post-content').html() ?? (item.extra.introduction || '');
|
||||
|
||||
return {
|
||||
title: item.title,
|
||||
image: item.extra.cover_small,
|
||||
pubDate: parseDate(item.extra.date),
|
||||
author: item.extra.author,
|
||||
link: `${base}/blog?id=${item.path}`,
|
||||
description: content,
|
||||
category: item.extra.tags,
|
||||
};
|
||||
};
|
||||
|
||||
export const route: Route = {
|
||||
path: '/blog/:lang?',
|
||||
categories: ['programming'],
|
||||
example: '/qwen/blog',
|
||||
parameters: {
|
||||
lang: '语言,`zh-CN`, `en-US`',
|
||||
},
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
name: 'Blog',
|
||||
maintainers: ['Kjasn'],
|
||||
handler: async (ctx): Promise<Data> => {
|
||||
const base = 'https://qwen.ai';
|
||||
const { lang = 'zh-CN' } = ctx.req.param();
|
||||
|
||||
const blogUrl = `${base}/api/v2/article/retrieval?language=${lang}&type=qwen_ai`;
|
||||
|
||||
const response = await ofetch(blogUrl);
|
||||
const articles = response.data?.articles;
|
||||
|
||||
return {
|
||||
title: 'Qwen Blog',
|
||||
link: `${base}/research`,
|
||||
item: (articles || []).map((item: any) => buildBlogItem(item, base)),
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { Namespace } from '@/types';
|
||||
|
||||
export const namespace: Namespace = {
|
||||
name: 'Qwen Blog',
|
||||
url: 'qwenlm.github.io',
|
||||
name: 'Qwen Research',
|
||||
url: 'qwen.ai',
|
||||
};
|
||||
@@ -1,76 +0,0 @@
|
||||
import { load } from 'cheerio';
|
||||
|
||||
import type { Route } from '@/types';
|
||||
import cache from '@/utils/cache';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
|
||||
export const route: Route = {
|
||||
path: '/blog/:lang?',
|
||||
categories: ['blog'],
|
||||
example: '/qwenlm/blog/zh',
|
||||
parameters: { lang: 'Blog language' },
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['qwenlm.github.io/blog/', 'qwenlm.github.io/:lang/blog/'],
|
||||
target: '/qwenlm/blog/:lang',
|
||||
},
|
||||
],
|
||||
name: 'Blog',
|
||||
maintainers: ['Kjasn'],
|
||||
handler: async (ctx) => {
|
||||
const { lang } = ctx.req.param();
|
||||
|
||||
const blogUrl = lang ? `https://qwenlm.github.io/${lang}/blog` : 'https://qwenlm.github.io/blog';
|
||||
|
||||
const response = await ofetch(blogUrl);
|
||||
const $ = load(response);
|
||||
|
||||
// get blog list
|
||||
const list = $('article.post-entry')
|
||||
.toArray()
|
||||
.map((item) => {
|
||||
item = $(item);
|
||||
|
||||
const dateString = item
|
||||
.find('.entry-footer span')
|
||||
.attr('title')
|
||||
.trim()
|
||||
.replace(/\+0800$/, '');
|
||||
// const pubDate = timezone(parseDate(dateString, 'YYYY-MM-DD HH:mm:ss ZZ'), +8);
|
||||
const pubDate = parseDate(dateString);
|
||||
|
||||
return {
|
||||
title: item.find('header.entry-header h2').text().trim(),
|
||||
link: item.find('.entry-link').attr('href'),
|
||||
pubDate,
|
||||
};
|
||||
});
|
||||
|
||||
// get blog content
|
||||
const items = await Promise.all(
|
||||
list.map((item) =>
|
||||
cache.tryGet(item.link, async () => {
|
||||
const response = await ofetch(item.link);
|
||||
const $ = load(response);
|
||||
item.description = $('main').html();
|
||||
return item;
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
title: 'Qwen Blog',
|
||||
link: blogUrl,
|
||||
item: items,
|
||||
};
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user