-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route/mirrormedia): Add route for category. (#16138)
* feat(route/mirrormedia): Add route for category. * Fix pubDate. * . * Update filter.ts * . * . * .
- Loading branch information
Showing
3 changed files
with
123 additions
and
16 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,100 @@ | ||
import { Route } from '@/types'; | ||
|
||
import { parseDate } from '@/utils/parse-date'; | ||
import ofetch from '@/utils/ofetch'; | ||
import { getArticle } from './utils'; | ||
|
||
export const route: Route = { | ||
path: ['/category/:category', '/section/:section'], | ||
categories: ['traditional-media'], | ||
example: '/mirrormedia/category/political', | ||
parameters: { category: '分类名', section: '子板名' }, | ||
name: '分类', | ||
maintainers: ['dzx-dzx'], | ||
radar: [ | ||
{ | ||
source: ['mirrormedia.mg/category/:category', 'mirrormedia.mg/section/:section'], | ||
}, | ||
], | ||
handler, | ||
}; | ||
|
||
async function handler(ctx) { | ||
const { category, section } = ctx.req.param(); | ||
const categoryFilter = category ? { categories: { some: { slug: { equals: category } } } } : {}; | ||
const sectionFilter = section ? { sections: { some: { slug: { equals: section } } } } : {}; | ||
const rootUrl = 'https://www.mirrormedia.mg'; | ||
|
||
const response = await ofetch('https://adam-weekly-api-server-prod-ufaummkd5q-de.a.run.app/content/graphql', { | ||
method: 'POST', | ||
body: { | ||
variables: { | ||
take: ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 24, | ||
skip: 0, | ||
orderBy: { publishedDate: 'desc' }, | ||
filter: { | ||
state: { equals: 'published' }, | ||
...categoryFilter, ...sectionFilter, | ||
}, | ||
}, | ||
query: ` | ||
fragment section on Section { | ||
id | ||
name | ||
slug | ||
state | ||
__typename | ||
} | ||
fragment category on Category { | ||
id | ||
name | ||
slug | ||
state | ||
__typename | ||
} | ||
fragment listingPost on Post { | ||
id | ||
slug | ||
title | ||
brief | ||
publishedDate | ||
state | ||
sections(where: {state: {equals: "active"}}) { | ||
...section | ||
__typename | ||
} | ||
categories(where: {state: {equals: "active"}}) { | ||
...category | ||
__typename | ||
} | ||
isFeatured | ||
__typename | ||
} | ||
query ($take: Int, $skip: Int, $orderBy: [PostOrderByInput!]!, $filter: PostWhereInput!) { | ||
postsCount(where: $filter) | ||
posts(take: $take, skip: $skip, orderBy: $orderBy, where: $filter) { | ||
...listingPost | ||
__typename | ||
} | ||
}`, | ||
}, | ||
}); | ||
|
||
const items = response.data.posts.map((e) => ({ | ||
title: e.title, | ||
pubDate: parseDate(e.publishedDate), | ||
category: [...(e.sections ?? []).map((_) => `section:${_.name}`), ...(e.categories ?? []).map((_) => `category:${_.name}`)], | ||
link: `${rootUrl}/${'story'}/${e.slug}`, | ||
})); | ||
|
||
const list = await Promise.all(items.map((item) => getArticle(item))); | ||
|
||
return { | ||
title: `鏡週刊 Mirror Media - ${category}`, | ||
link: rootUrl, | ||
item: list, | ||
}; | ||
} |
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,20 @@ | ||
import { load } from 'cheerio'; | ||
import cache from '@/utils/cache'; | ||
import ofetch from '@/utils/ofetch'; | ||
|
||
export function getArticle(item) { | ||
return cache.tryGet(item.link, async () => { | ||
const detailResponse = await ofetch(item.link); | ||
|
||
const content = load(detailResponse); | ||
|
||
item.description = item.link.includes('external') | ||
? content(':is([class^=external-article-brief],[class^=external-article-content])').html() | ||
: content(':is([class^=brief__BriefContainer],[class^=article-content__Wrapper])').html(); | ||
|
||
item.category = [...item.category, ...(content("meta[name='keywords']").attr("content") ?? "").split(",")]; | ||
|
||
|
||
return item; | ||
}); | ||
} |