-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.nv
155 lines (130 loc) · 3.85 KB
/
main.nv
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use std.{env, fs, io.{self, StringBuffer}, net.http, regex.Regex, time, xml, yaml};
use feishu.send_message;
use state;
const CI = env.get("CI") != nil;
const BR_RE = try! Regex.new(r`<br\s*/?>`);
struct Config {
feeds: [FeedConfig],
}
struct FeedConfig {
/// RSS feed URL
url: string,
/// Unique key for this feed
key: string,
}
/// A RSS 2.0 feed
struct RssFeed {
channel: Channel,
}
#[serde(rename_all = "camelCase")]
struct Channel {
title: string,
link: string,
description: string,
#[serde(rename = "item")]
items: [Item],
}
/// A RSS 2.0 item
#[serde(rename_all = "camelCase")]
struct Item {
guid: string,
title: string,
link: string,
pub_date: string,
description: string,
date: time.DateTime? = nil,
}
impl Item {
/// Parse the pubDate field into a DateTime
fn date(self): time.DateTime {
if (let date = self.date) {
return date;
}
self.date = try! time.parse(self.pub_date, format: "%a, %d %b %Y %H:%M:%S %z");
return self.date!;
}
fn formated_title(self): string {
return BR_RE.replace_all(self.title, "\n");
}
fn to_message(self): string {
return `${self.formated_title()}\n\n${self.pub_date}\n\n${self.link}\n\nFrom: https://github.com/longbridgeapp/hkex-rss-monitor`;
}
}
fn load_config(): Config throws {
let config = try yaml.parse::<Config>(fs.read_to_string("./config.yml"));
return config;
}
fn fetch_rss(url: string): [Item] throws {
let res = try http.get(url);
let rss = try xml.from_reader::<RssFeed>(res.body());
return rss.channel.items;
}
fn process_item(item: Item, feed_config: FeedConfig) throws {
// println(`item: ${item.date()}`);
if (!(try state.is_new(feed_config.key, item.date()))) {
return;
}
println(`New Item: ${item.title}`);
let message: string = item.to_message();
if (CI) {
try send_message(message);
} else {
println(message);
}
}
fn main() throws {
let config = try load_config();
for (let feed_config in config.feeds) {
let last_date: time.DateTime? = nil;
println(`Fetching ${feed_config.url}`);
let items = try fetch_rss(feed_config.url);
items.reverse();
for (let item in items) {
try process_item(item, feed_config);
if (last_date == nil || item.date() > last_date!) {
last_date = item.date();
}
}
try state.save(feed_config.key, last_date!);
}
}
test "Item.date" {
let item = Item {
guid: "guid",
title: "title",
link: "link",
pub_date: "Tue, 09 Jan 2024 17:37:24 +0800",
description: "description",
};
let t = item.date();
assert t.year() == 2024;
assert t.month() == 1;
assert t.day() == 9;
assert t.hour() == 17;
assert t.minute() == 37;
assert t.second() == 24;
}
test "Item.to_message" {
let item = Item {
guid: "guid",
title: "This is title<br>Next line",
link: "https://github.com",
pub_date: "Tue, 09 Jan 2024 17:37:24 +0800",
description: "description",
};
assert_eq item.to_message(), `This is title\nNext line\n\nTue, 09 Jan 2024 17:37:24 +0800\n\nhttps://github.com\n\nFrom: https://github.com/longbridgeapp/hkex-rss-monitor`;
}
test "Item.formated_title" {
let item = Item {
guid: "guid",
title: "This is title<br>Next line",
link: "https://github.com",
pub_date: "Tue, 09 Jan 2024 17:37:24 +0800",
description: "description",
};
assert_eq item.formated_title(), `This is title\nNext line`;
item.title = "This is title<br/>Next line";
assert_eq item.formated_title(), `This is title\nNext line`;
item.title = "This is title<br />Next line";
assert_eq item.formated_title(), `This is title\nNext line`;
}