/
news.ts
118 lines (105 loc) 路 3.99 KB
/
news.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
import timezone from '@/utils/timezone';
const map = new Map([
['top-stories', { title: 'NL Times -- Top Stories', suffix: '/top-stories' }],
['health', { title: 'NL Times -- Health', suffix: '/categories/health' }],
['crime', { title: 'NL Times -- Crime', suffix: '/categories/crime' }],
['politics', { title: 'NL Times -- Politics', suffix: '/categories/politics' }],
['business', { title: 'NL Times -- Business', suffix: '/categories/business' }],
['tech', { title: 'NL Times -- Tech', suffix: '/categories/tech' }],
['culture', { title: 'NL Times -- Culture', suffix: '/categories/culture' }],
['sports', { title: 'NL Times -- Sports', suffix: '/categories/sports' }],
['weird', { title: 'NL Times -- Weird', suffix: '/categories/weird' }],
['1-1-2', { title: 'NL Times -- 1-1-2', suffix: '/categories/1-1-2' }],
]);
export const route: Route = {
path: '/news/:category?',
categories: ['new-media'],
example: '/nltimes/news/top-stories',
parameters: { category: 'category' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['nltimes.nl/categories/:category'],
target: '/news/:category',
},
],
name: 'News',
maintainers: ['Hivol'],
handler,
description: `| Top Stories (default) | Health | Crime | Politics | Business | Tech | Culture | Sports | Weird | 1-1-2 |
| --------------------- | ------ | ----- | -------- | -------- | ---- | ------- | ------ | ----- | ----- |
| top-stories | health | crime | politics | business | tech | culture | sports | weird | 1-1-2 |`,
};
async function handler(ctx) {
const category = ctx.req.param('category') ?? 'top-stories';
const suffix = map.get(category).suffix;
const rootUrl = 'https://www.nltimes.nl';
const apiUrl = rootUrl + suffix;
const response = await got({
method: 'get',
url: apiUrl,
});
const $ = load(response.data);
const list = $('.news-card')
.slice(0, 10)
.map((_, elem) => {
const item = {
link: $(elem).children('.news-card__title').first().children('a').first().attr('href'),
title: $(elem).children('.news-card__title').first().children('a').first().text(),
date: $(elem).children('.news-card__date').first().text(),
category: $(elem)
.children('.news-card__categories')
.first()
.children('a')
.map((_, elem) => $(elem).text())
.get(),
};
return item;
})
.get();
const ProcessFeed = (data) => {
const $ = load(data);
return $('.news-article--body').html();
};
const items = await Promise.all(
list.map((item) => {
const title = item.title;
const date = timezone(parseDate(item.date, 'DD MMMM YYYY - HH:mm'), +1); // Central European Time
const link = rootUrl + item.link;
const category = item.category;
return cache.tryGet(link, async () => {
const response = await got({
method: 'get',
url: link,
});
const description = ProcessFeed(response.data);
return {
title,
category,
description,
pubDate: date,
link,
};
});
})
);
return {
title: map.get(category).title,
language: 'en',
link: apiUrl,
description: map.get(category).title,
item: items,
};
}