-
Notifications
You must be signed in to change notification settings - Fork 295
/
handle-rss.ts
131 lines (120 loc) · 3.72 KB
/
handle-rss.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
119
120
121
122
123
124
125
126
127
128
129
130
131
import Parser from 'rss-parser';
import dotenv from 'dotenv';
import { JSDOM } from 'jsdom';
import { consola } from 'consola';
import {
ALL_STATUS,
RATING_TEXT,
SeeState,
ReadState,
PlayState,
ListenState,
} from './const';
import {
ItemCategory,
ItemStatus,
type RSSFeedItem,
type FeedItem,
} from './types';
type ItemInfo = {
category: ItemCategory;
id: string;
status: ItemStatus;
}
dotenv.config();
export async function fetchRSSFeeds(): Promise<RSSFeedItem[]> {
const DOUBAN_USER_ID = process.env.DOUBAN_USER_ID;
const parser = new Parser();
try {
const feeds = await parser.parseURL(
`https://www.douban.com/feed/people/${DOUBAN_USER_ID}/interests`
);
return feeds.items;
} catch (error) {
consola.error('Failed to parse RSS url: ', error);
process.exit(1);
}
}
/**
* Normalize the given array of RSS feed items.
*
* @param {RSSFeedItem[]} feeds - The array of RSS feed items to be normalized.
* @return {FeedItem[]} The normalized array of feed items.
*/
export function handleRSSFeeds(feeds: RSSFeedItem[]): FeedItem[] {
const normalizedFeeds: FeedItem[] = [];
feeds.forEach((item) => {
const itemInfo = extractItemInfo(item.title!, item.link!);
if (!itemInfo) {
return;
}
const { category, id, status } = itemInfo;
const dom = new JSDOM(item.content!.trim());
const contents = [...dom.window.document.querySelectorAll('td p')];
const ratingElements = contents.filter((el) => el.textContent!.startsWith('推荐'));
let ratingNumber = 0;
if (ratingElements.length) {
const rating = ratingElements[0].textContent!.replace(/^推荐: /, '').trim();
ratingNumber = RATING_TEXT[rating];
}
const commentElements = contents.filter((el) => el.textContent!.startsWith('备注'));
let comment = '';
if (commentElements.length) {
comment = commentElements[0].textContent!.replace(/^备注: /, '').trim();
}
const result = {
id,
link: item.link,
rating: ratingNumber || null,
comment: typeof comment === 'string' ? comment : null, // 备注:XXX -> 短评
time: item.isoDate, // '2021-05-30T06:49:34.000Z'
status,
category,
} as FeedItem;
normalizedFeeds.push(result);
});
return normalizedFeeds;
}
/**
* Extracts the category, ID, and status from the given title and link
* which are from RSS feed item.
*
* @param {string} title - The title to extract the information from.
* @param {string} link - The link to extract the information from.
* @return {ItemInfo} An object containing the extracted category, ID, and status.
*/
export function extractItemInfo(title: string, link: string): ItemInfo | undefined {
const m = title.match(ALL_STATUS)?.[1];
if (!m) {
return;
}
if (Object.keys(SeeState).includes(m)) {
const isMovie = link.startsWith('http://movie.douban.com/');
return {
category: isMovie ? ItemCategory.Movie : ItemCategory.Drama,
id: isMovie
? link.match(/movie\.douban\.com\/subject\/(\d+)\/?/)?.[1]!
: link.match(/www\.douban\.com\/location\/drama\/(\d+)\/?/)?.[1]!,
status: SeeState[m],
};
} else if (Object.keys(ReadState).includes(m)) {
return {
category: ItemCategory.Book,
id: link.match(/book\.douban\.com\/subject\/(\d+)\/?/)?.[1]!,
status: ReadState[m],
};
} else if (Object.keys(ListenState).includes(m)) {
return {
category: ItemCategory.Music,
id: link.match(/music\.douban\.com\/subject\/(\d+)\/?/)?.[1]!,
status: ListenState[m],
};
} else if (Object.keys(PlayState).includes(m)) {
return {
category: ItemCategory.Game,
id: link.match(/www\.douban\.com\/game\/(\d+)\/?/)?.[1]!,
status: PlayState[m],
};
}
return;
}