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(route): add nielsberglund (DIYgod#17398)
fix(route): fix pr issue
- Loading branch information
1 parent
6fc4814
commit f17408d
Showing
2 changed files
with
85 additions
and
0 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,78 @@ | ||
import { Route, DataItem } from '@/types'; | ||
import got from '@/utils/got'; | ||
import { load } from 'cheerio'; | ||
import { parseDate } from '@/utils/parse-date'; | ||
import cache from '@/utils/cache'; | ||
|
||
export const route: Route = { | ||
path: '/blog', | ||
categories: ['blog'], | ||
example: '/nielsberglund/blog', | ||
radar: [ | ||
{ | ||
source: ['nielsberglund.com/'], | ||
}, | ||
], | ||
url: 'nielsberglund.com/', | ||
name: 'Blog', | ||
maintainers: ['liyaozhong'], | ||
handler, | ||
description: 'Niels Berglund Blog Posts', | ||
}; | ||
|
||
async function handler() { | ||
const rootUrl = 'https://nielsberglund.com'; | ||
const currentUrl = rootUrl; | ||
|
||
const response = await got(currentUrl); | ||
const $ = load(response.data); | ||
|
||
let items = $('.post-preview') | ||
.toArray() | ||
.map((item) => { | ||
const $item = $(item); | ||
const $link = $item.find('a').first(); | ||
const href = $link.attr('href'); | ||
const title = $item.find('.post-title').first().text().trim(); | ||
const dateStr = $item.find('.post-meta').text().trim(); | ||
|
||
if (!href || !title) { | ||
return null; | ||
} | ||
|
||
const link = new URL(href, rootUrl).href; | ||
const pubDate = parseDate(dateStr); | ||
|
||
return { | ||
title, | ||
link, | ||
pubDate, | ||
} as DataItem; | ||
}) | ||
.filter((item): item is DataItem => item !== null); | ||
|
||
items = ( | ||
await Promise.all( | ||
items.map((item) => | ||
cache.tryGet(item.link as string, async () => { | ||
try { | ||
const detailResponse = await got(item.link); | ||
const $detail = load(detailResponse.data); | ||
|
||
item.description = $detail('.post-container').html() || ''; | ||
|
||
return item; | ||
} catch { | ||
return item; | ||
} | ||
}) | ||
) | ||
) | ||
).filter((item): item is DataItem => item !== null); | ||
|
||
return { | ||
title: 'Niels Berglund Blog', | ||
link: rootUrl, | ||
item: items, | ||
}; | ||
} |
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,7 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: 'Niels Berglund Blog', | ||
url: 'nielsberglund.com', | ||
lang: 'en', | ||
}; |