Skip to content

Commit

Permalink
feat(route/gis): Add route (DIYgod#16461)
Browse files Browse the repository at this point in the history
* feat(route/gis): Add route

* Update namespace.ts

* Update lib/routes/gis/namespace.ts

Co-authored-by: Tony <[email protected]>

* Update index.ts

* .

---------
  • Loading branch information
dzx-dzx authored Aug 19, 2024
1 parent 1fe1ddc commit ecba4f7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
60 changes: 60 additions & 0 deletions lib/routes/gisreportsonline/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Route } from '@/types';

import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import cache from '@/utils/cache';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/:path{.*}',
categories: ['new-media'],
example: '/gis/c/security-challenges/',
parameters: { path: '包含"Reports"页面下的路径' },
name: '报告',
maintainers: ['dzx-dzx'],
radar: [
{
source: ['www.gisreportsonline.com'],
},
],
handler,
};

async function handler(ctx) {
const rootUrl = 'https://www.gisreportsonline.com';
const currentUrl = `${rootUrl}/${ctx.req.param('path')}`;
const response = await ofetch(currentUrl);

const $ = load(response);

const list = $('article h3 a')
.toArray()
.map((e) => ({ link: $(e).attr('href'), title: $(e).text() }));

const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const content = load(await ofetch(item.link));
const ldjson = JSON.parse(content('script.rank-math-schema-pro').text())['@graph'].find((e) => e['@type'] === 'NewsArticle');

item.pubDate = parseDate(ldjson.datePublished);
item.updated = parseDate(ldjson.dateModified);
item.author = [ldjson.author];
item.category = ldjson.keywords.split(',');
item.language = ldjson.inLanguage;

item.description = content('header.entry-header ~ :not(#pos-conclusion ~ *)')
.toArray()
.map((e) => content(e).html())
.join('');

return item;
})
)
);
return {
title: $('title').text(),
link: currentUrl,
item: items,
};
}
6 changes: 6 additions & 0 deletions lib/routes/gisreportsonline/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'GIS Reports',
url: 'www.gisreportsonline.com',
};

0 comments on commit ecba4f7

Please sign in to comment.