/
news.ts
87 lines (76 loc) 路 2.96 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
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import { parseDate } from '@/utils/parse-date';
export const route: Route = {
path: '/:category?',
categories: ['traditional-media'],
example: '/tass/politics',
parameters: { category: 'Category, can be found in URL, `politics` by default' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['tass.com/:category'],
target: '/:category',
},
],
name: 'News',
maintainers: ['TonyRL'],
handler,
description: `| Russian Politics & Diplomacy | World | Business & Economy | Military & Defense | Science & Space | Emergencies | Society & Culture | Press Review | Sports |
| ---------------------------- | ----- | ------------------ | ------------------ | --------------- | ----------- | ----------------- | ------------ | ------ |
| politics | world | economy | defense | science | emergencies | society | pressreview | sports |`,
};
async function handler(ctx) {
const { category = 'politics' } = ctx.req.param();
const { data: categoryPage, url: link } = await got(`https://tass.com/${category}`);
const $ = load(categoryPage);
const sectionId = $('.container .section-page')
.attr('ng-init')
.match(/sectionId\s*=\s*(\d+?);/);
const { data: response } = await got.post('https://tass.com/userApi/categoryNewsList', {
json: {
sectionId: sectionId[1],
limit: 20,
type: 'all',
},
});
const list = response.newsList.map((item) => ({
title: item.title,
description: item.lead,
link: `https://tass.com${item.link}`,
pubDate: parseDate(item.date, 'X'),
}));
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const { data: response } = await got(item.link);
const $ = load(response);
$('.news-media img').each((_, ele) => {
if (ele.attribs.src) {
ele.attribs.src = ele.attribs.src.replaceAll('/width/1020_b9261fa1', '');
}
});
item.description = $('.news-header__lead').prop('outerHTML') + ($('.news-media').prop('outerHTML') ?? '') + $('.text-block').html();
return item;
})
)
);
return {
title: $('head title').text(),
link,
language: 'en',
image: $('head meta[property="og:image"]').attr('content'),
icon: $('head link[rel="apple-touch-icon"]').attr('href'),
logo: $('head link[rel="apple-touch-icon"]').attr('href'),
item: items,
};
}