forked from DIYgod/RSSHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routes/landiannews): use wp-api, add category and tag routes (DI…
…Ygod#18292) * feat(routes/landiannews): use wp-api, add category and tag routes * fix(routes/landiannews): use '_embed' search param instead * feat(routes/landiannews/tag|category): use slug instead of id * fix(routes/landiannews): cameCase Co-authored-by: Tony <[email protected]> * fix(routes/landiannews): apply review suggestion * fix(routes/landiannews): fix example
- Loading branch information
Showing
5 changed files
with
141 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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.landiannews.com/'; | ||
const postApiUrl = `${rootUrl}wp-json/wp/v2/posts?_embed&categories=${id}`; | ||
|
||
const items: DataItem[] = await fetchNewsItems(postApiUrl); | ||
|
||
return { | ||
title: `${name} - 蓝点网`, | ||
description: '给你感兴趣的内容!', | ||
link: `${rootUrl}${slug}`, | ||
item: items, | ||
}; | ||
}; | ||
|
||
export const route: Route = { | ||
path: '/category/:slug', | ||
name: '分类', | ||
url: 'www.landiannews.com', | ||
maintainers: ['cscnk52'], | ||
handler, | ||
example: '/landiannews/category/sells', | ||
parameters: { slug: '分类名称' }, | ||
description: undefined, | ||
categories: ['new-media'], | ||
features: { | ||
requireConfig: false, | ||
requirePuppeteer: false, | ||
antiCrawler: false, | ||
supportRadar: true, | ||
supportBT: false, | ||
supportPodcast: false, | ||
supportScihub: false, | ||
}, | ||
radar: [ | ||
{ | ||
source: ['www.landiannews.com/:slug'], | ||
target: '/category/:slug', | ||
}, | ||
], | ||
view: ViewType.Articles, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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.landiannews.com/'; | ||
const postApiUrl = `${rootUrl}wp-json/wp/v2/posts?_embed&tags=${id}`; | ||
|
||
const items: DataItem[] = await fetchNewsItems(postApiUrl); | ||
|
||
return { | ||
title: `${name} - 蓝点网`, | ||
description: '给你感兴趣的内容!', | ||
link: `${rootUrl}archives/tag/${slug}`, | ||
item: items, | ||
}; | ||
}; | ||
|
||
export const route: Route = { | ||
path: '/tag/:slug', | ||
name: '标签', | ||
url: 'www.landiannews.com', | ||
maintainers: ['cscnk52'], | ||
handler, | ||
example: '/landiannews/tag/linux-kernel', | ||
parameters: { slug: '标签名称' }, | ||
description: undefined, | ||
categories: ['new-media'], | ||
features: { | ||
requireConfig: false, | ||
requirePuppeteer: false, | ||
antiCrawler: false, | ||
supportRadar: true, | ||
supportBT: false, | ||
supportPodcast: false, | ||
supportScihub: false, | ||
}, | ||
radar: [ | ||
{ | ||
source: ['www.landiannews.com/archives/tag/:slug'], | ||
target: '/tag/:slug', | ||
}, | ||
], | ||
view: ViewType.Articles, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.landiannews.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).toUTCString(), | ||
author: item._embedded.author[0].name, | ||
category: item._embedded['wp:term'].flat().map((term) => term.name), | ||
})); | ||
} | ||
|
||
export { fetchCategory, fetchTag, fetchNewsItems }; |