feat(routes/iplaysoft): use wp-api, add tag category routes (#18332)
* feat(routes/iplaysoft): use wp-api, add tag category routes * fix(routes/iplaysoft): change categories to 'program-update' * fix(routes/iplaysoft): fix index url * fix(routes/iplaysoft): use date_gmt ---------
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { Data, DataItem, Route, ViewType } from '@/types';
|
||||
import { fetchNewsItems, fetchCategory } from './utils';
|
||||
|
||||
export const handler = async (ctx): Promise<Data> => {
|
||||
const slug = ctx.req.param('slug');
|
||||
|
||||
const { id, name } = await fetchCategory(slug);
|
||||
|
||||
const rootUrl = 'https://www.iplaysoft.com/';
|
||||
const postApiUrl = `${rootUrl}wp-json/wp/v2/posts?_embed&categories=${id}`;
|
||||
|
||||
const items: DataItem[] = await fetchNewsItems(postApiUrl);
|
||||
|
||||
return {
|
||||
title: `${name} - 异次元软件世界`,
|
||||
description: '软件改变生活',
|
||||
language: 'zh-CN',
|
||||
link: `${rootUrl}category/${slug}`,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
||||
export const route: Route = {
|
||||
path: '/category/:slug',
|
||||
name: '分类',
|
||||
url: 'www.iplaysoft.com',
|
||||
maintainers: ['cscnk52'],
|
||||
handler,
|
||||
example: '/iplaysoft/category/system',
|
||||
parameters: { slug: '分类名称' },
|
||||
description: undefined,
|
||||
categories: ['program-update'],
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportRadar: true,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['www.iplaysoft.com/category/:slug'],
|
||||
target: '/category/:slug',
|
||||
},
|
||||
],
|
||||
view: ViewType.Articles,
|
||||
};
|
||||
@@ -1,19 +1,36 @@
|
||||
import cache from '@/utils/cache';
|
||||
import { load } from 'cheerio';
|
||||
import { parseDate } from '@/utils/parse-date';
|
||||
import { DataItem, Route } from '@/types';
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import parser from '@/utils/rss-parser';
|
||||
import { Data, DataItem, Route, ViewType } from '@/types';
|
||||
import { fetchNewsItems } from './utils';
|
||||
|
||||
export const handler = async (): Promise<Data> => {
|
||||
const rootUrl = 'https://www.iplaysoft.com/';
|
||||
const postApiUrl = `${rootUrl}wp-json/wp/v2/posts?_embed`;
|
||||
|
||||
const items: DataItem[] = await fetchNewsItems(postApiUrl);
|
||||
|
||||
return {
|
||||
title: '异次元软件世界',
|
||||
description: '软件改变生活',
|
||||
language: 'zh-CN',
|
||||
link: rootUrl,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
||||
export const route: Route = {
|
||||
path: '/',
|
||||
categories: ['program-update'],
|
||||
name: '首页',
|
||||
url: 'www.iplaysoft.com',
|
||||
maintainers: ['williamgateszhao', 'cscnk52'],
|
||||
handler,
|
||||
example: '/iplaysoft',
|
||||
parameters: {},
|
||||
parameters: undefined,
|
||||
description: undefined,
|
||||
categories: ['program-update'],
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportRadar: true,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
@@ -21,48 +38,8 @@ export const route: Route = {
|
||||
radar: [
|
||||
{
|
||||
source: ['www.iplaysoft.com'],
|
||||
target: '/',
|
||||
},
|
||||
],
|
||||
name: '全部文章',
|
||||
maintainers: ['williamgateszhao'],
|
||||
handler,
|
||||
view: ViewType.Articles,
|
||||
};
|
||||
|
||||
async function handler(ctx) {
|
||||
const feed = await parser.parseURL('https://www.iplaysoft.com/feed/atom');
|
||||
const filteredItems = feed.items
|
||||
.filter((item) => item?.link && item?.pubDate && new URL(item.link).hostname.match(/.*\.iplaysoft\.com$/))
|
||||
.slice(0, ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 20) as DataItem[];
|
||||
|
||||
const items: DataItem[] = await Promise.all(
|
||||
filteredItems.map(
|
||||
(item) =>
|
||||
cache.tryGet(item.link as string, async () => {
|
||||
const response = await ofetch(item.link as string);
|
||||
const $ = load(response);
|
||||
const content = $('.entry-content');
|
||||
content
|
||||
.find('div')
|
||||
.filter((_index, element) => {
|
||||
const style = $(element).attr('style');
|
||||
return style !== undefined && style.includes('overflow:hidden');
|
||||
})
|
||||
.remove();
|
||||
return {
|
||||
title: item.title,
|
||||
description: content.html(),
|
||||
link: item.link,
|
||||
author: item.author,
|
||||
pubDate: parseDate(item.pubDate as string),
|
||||
} as DataItem;
|
||||
}) as Promise<DataItem>
|
||||
)
|
||||
);
|
||||
|
||||
return {
|
||||
title: '异次元软件世界',
|
||||
link: 'https://www.iplaysoft.com',
|
||||
language: 'zh-CN',
|
||||
item: items,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,5 +3,6 @@ import type { Namespace } from '@/types';
|
||||
export const namespace: Namespace = {
|
||||
name: '异次元软件世界',
|
||||
url: 'www.iplaysoft.com',
|
||||
categories: ['new-media'],
|
||||
lang: 'zh-CN',
|
||||
};
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { Data, DataItem, Route, ViewType } from '@/types';
|
||||
import { fetchNewsItems, fetchTag } from './utils';
|
||||
|
||||
export const handler = async (ctx): Promise<Data> => {
|
||||
const slug = ctx.req.param('slug');
|
||||
|
||||
const { id, name } = await fetchTag(slug);
|
||||
|
||||
const rootUrl = 'https://www.iplaysoft.com/';
|
||||
const postApiUrl = `${rootUrl}wp-json/wp/v2/posts?_embed&tags=${id}`;
|
||||
|
||||
const items: DataItem[] = await fetchNewsItems(postApiUrl);
|
||||
|
||||
return {
|
||||
title: `${name} - 异次元软件世界`,
|
||||
description: '软件改变生活',
|
||||
language: 'zh-CN',
|
||||
link: `${rootUrl}tag/${slug}`,
|
||||
item: items,
|
||||
};
|
||||
};
|
||||
|
||||
export const route: Route = {
|
||||
path: '/tag/:slug',
|
||||
name: '标签',
|
||||
url: 'www.iplaysoft.com',
|
||||
maintainers: ['cscnk52'],
|
||||
handler,
|
||||
example: '/iplaysoft/tag/windows',
|
||||
parameters: { slug: '标签名称' },
|
||||
description: undefined,
|
||||
categories: ['program-update'],
|
||||
features: {
|
||||
requireConfig: false,
|
||||
requirePuppeteer: false,
|
||||
antiCrawler: false,
|
||||
supportRadar: true,
|
||||
supportBT: false,
|
||||
supportPodcast: false,
|
||||
supportScihub: false,
|
||||
},
|
||||
radar: [
|
||||
{
|
||||
source: ['www.iplaysoft.com/tag/:slug'],
|
||||
target: '/tag/:slug',
|
||||
},
|
||||
],
|
||||
view: ViewType.Articles,
|
||||
};
|
||||
@@ -0,0 +1,34 @@
|
||||
import ofetch from '@/utils/ofetch';
|
||||
import cache from '@/utils/cache';
|
||||
|
||||
const rootUrl = 'https://www.iplaysoft.com/';
|
||||
|
||||
const fetchTaxonomy = async (slug: string, type: 'categories' | 'tags') => {
|
||||
const taxonomyUrl = `${rootUrl}wp-json/wp/v2/${type}?slug=${slug}`;
|
||||
const cachedTaxonomy = await cache.tryGet(taxonomyUrl, async () => {
|
||||
const taxonomyData = await ofetch(taxonomyUrl);
|
||||
if (!taxonomyData[0] || !taxonomyData[0].id || !taxonomyData[0].name) {
|
||||
throw new Error(`${type} ${slug} not found`);
|
||||
}
|
||||
return { id: taxonomyData[0].id, name: taxonomyData[0].name };
|
||||
});
|
||||
return cachedTaxonomy;
|
||||
};
|
||||
|
||||
const fetchCategory = async (categorySlug: string) => await fetchTaxonomy(categorySlug, 'categories');
|
||||
const fetchTag = async (tagSlug: string) => await fetchTaxonomy(tagSlug, 'tags');
|
||||
|
||||
async function fetchNewsItems(apiUrl: string) {
|
||||
const data = await ofetch(apiUrl);
|
||||
|
||||
return data.map((item) => ({
|
||||
title: item.title.rendered,
|
||||
description: item.content.rendered,
|
||||
link: item.link,
|
||||
pubDate: new Date(item.date_gmt).toUTCString(),
|
||||
author: item._embedded.author[0].name,
|
||||
category: item._embedded['wp:term'].flat().map((term) => term.name),
|
||||
}));
|
||||
}
|
||||
|
||||
export { fetchCategory, fetchTag, fetchNewsItems };
|
||||
Reference in New Issue
Block a user