Skip to content

Commit

Permalink
feat(routes/iplaysoft): use wp-api, add tag category routes (DIYgod#1…
Browse files Browse the repository at this point in the history
…8332)

* 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

---------
  • Loading branch information
cscnk52 authored Feb 12, 2025
1 parent 201fac2 commit 87b27c2
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 50 deletions.
49 changes: 49 additions & 0 deletions lib/routes/iplaysoft/category.ts
Original file line number Diff line number Diff line change
@@ -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,
};
77 changes: 27 additions & 50 deletions lib/routes/iplaysoft/index.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,45 @@
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,
},
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,
};
}
1 change: 1 addition & 0 deletions lib/routes/iplaysoft/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import type { Namespace } from '@/types';
export const namespace: Namespace = {
name: '异次元软件世界',
url: 'www.iplaysoft.com',
categories: ['new-media'],
lang: 'zh-CN',
};
49 changes: 49 additions & 0 deletions lib/routes/iplaysoft/tag.ts
Original file line number Diff line number Diff line change
@@ -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,
};
34 changes: 34 additions & 0 deletions lib/routes/iplaysoft/utils.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 87b27c2

Please sign in to comment.