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
|
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
|
-
-
-
-
+
-
+
+
+
-
+
+
+
-
+
-
-
-
+
-
-
-
-
-
-
-
-
-
+
+
+
+
-
+
+
-
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
+
+
+
+
+
+
+
+
+
+
-
+
|
use config;
use tokio;
use rss;
use chrono::DateTime;
use regex::Regex;
//use tbot;
//use tbot::prelude::*;
use futures::StreamExt;
use tokio::stream::StreamExt;
use futures::TryStreamExt;
use telegram_bot::*;
use sqlx::postgres::PgPoolOptions;
use sqlx::Row;
type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[derive(Clone)]
struct Core {
owner: i64,
api_key: String,
owner_chat: UserId,
tg: telegram_bot::Api,
my: User,
pool: sqlx::Pool<sqlx::Postgres>,
}
impl Core {
async fn new(settings: config::Config) -> Result<Core> {
let owner = settings.get_int("owner")?;
let api_key = settings.get_str("api_key")?;
let tg = Api::new(settings.get_str("api_key")?);
let tg = Api::new(&api_key);
let core = Core {
owner: owner,
api_key: api_key.clone(),
my: tg.send(telegram_bot::GetMe).await?,
tg: tg,
owner_chat: UserId::new(owner),
pool: PgPoolOptions::new().max_connections(5).connect(&settings.get_str("pg")?).await?,
};
let clone = core.clone();
tokio::spawn(async move {
if let Err(err) = &core.autofetch().await {
if let Err(err) = clone.autofetch().await {
eprintln!("connection error: {}", err);
}
});
let tg = Api::new(settings.get_str("api_key")?);
Ok(Core {
Ok(core)
owner: owner,
my: tg.send(telegram_bot::GetMe).await?,
tg: tg,
owner_chat: UserId::new(owner),
pool: PgPoolOptions::new().max_connections(5).connect(&settings.get_str("pg")?).await?,
})
}
fn stream(&self) -> telegram_bot::UpdatesStream {
self.tg.stream()
}
fn debug(&self, msg: &str) -> Result<()> {
self.tg.spawn(SendMessage::new(self.owner_chat, msg));
Ok(())
}
async fn check(&self, id: i32, real: Option<bool>) -> Result<()> {
match sqlx::query("select channel_id, url, last_fetch, iv_hash from rsstg_source where source_id = $1")
.bind(id)
async fn check(&self, channel: &str, real: Option<bool>) -> Result<()> {
match sqlx::query("select source_id, channel_id, url, last_fetch, iv_hash, owner from rsstg_source natural left join rsstg_channel where username = $1")
.bind(channel)
.fetch_one(&self.pool).await {
Ok(row) => {
let id: i32 = row.try_get("source_id")?;
let channel_id: i64 = row.try_get("channel_id")?;
let destination = match real {
Some(true) => UserId::new(channel_id),
Some(false) | None => self.owner_chat,
Some(false) | None => UserId::new(row.try_get("owner")?),
};
let url: &str = row.try_get("url")?;
let last_fetch: Option<DateTime<chrono::FixedOffset>> = row.try_get("last_fetch")?;
let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
let iv_hash: Option<&str> = row.try_get("iv_hash")?;
match rss::Channel::from_url(url) {
Ok(feed) => {
self.debug(&format!("# title:{:?} ttl:{:?} hours:{:?} days:{:?}", feed.title(), feed.ttl(), feed.skip_hours(), feed.skip_days()))?;
for item in feed.items() {
let date = match item.pub_date() {
let date = DateTime::parse_from_rfc2822(item.pub_date().unwrap()).unwrap();
Some(feed_date) => DateTime::parse_from_rfc2822(feed_date),
None => DateTime::parse_from_rfc3339(&item.dublin_core_ext().unwrap().dates()[0]),
}?;
let url = item.link().unwrap().to_string();
if last_fetch == None || date > last_fetch.unwrap() {
match sqlx::query("select exists(select true from rsstg_post where url = $1 and source_id = $2) as exists;")
.bind(&url)
.bind(id)
.fetch_one(&self.pool).await {
Ok(row) => {
let exists: bool = row.try_get("exists")?;
if ! exists {
if this_fetch == None || date > this_fetch.unwrap() {
this_fetch = Some(date);
}
match self.tg.send( match iv_hash {
Some(x) => SendMessage::new(destination, format!("<a href=\"https://t.me/iv?url={}&rhash={}\"> </a>{0}", url, x)),
None => SendMessage::new(destination, format!("{}", url)),
}.parse_mode(types::ParseMode::Html)).await {
Ok(_) => {
match sqlx::query("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);")
.bind(id)
.bind(date)
.bind(url)
.execute(&self.pool).await {
Ok(_) => {},
Err(err) => {
self.debug(&err.to_string())?;
},
};
},
Err(err) => {
self.debug(&err.to_string())?;
},
if this_fetch == None || date > this_fetch.unwrap() {
this_fetch = Some(date);
}
match self.tg.send( match iv_hash {
Some(x) => SendMessage::new(destination, format!("<a href=\"https://t.me/iv?url={}&rhash={}\"> </a>{0}", url, x)),
None => SendMessage::new(destination, format!("{}", url)),
}.parse_mode(types::ParseMode::Html)).await {
Ok(_) => {
match sqlx::query("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);")
.bind(id)
.bind(date)
.bind(url)
.execute(&self.pool).await {
Ok(_) => {},
Err(err) => {
self.debug(&err.to_string())?;
},
};
},
Err(err) => {
self.debug(&err.to_string())?;
},
}
};
tokio::time::delay_for(std::time::Duration::new(4, 0)).await;
};
};
tokio::time::delay_for(std::time::Duration::new(4, 0)).await;
}
},
Err(err) => {
self.debug(&err.to_string())?;
},
};
};
};
// update last_fetch
if this_fetch != None && (last_fetch == None || this_fetch.unwrap() > last_fetch.unwrap()) {
match sqlx::query("update rsstg_source set last_fetch = $1 where source_id = $2;")
match sqlx::query("update rsstg_source set last_fetch = case when (last_fetch < $1) then $1 else last_fetch end where source_id = $2;")
.bind(this_fetch.unwrap())
.bind(id)
.execute(&self.pool).await {
Ok(_) => {},
Err(err) => {
self.debug(&err.to_string())?;
},
|
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
-
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
-
+
-
-
-
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
},
}
Ok(())
}
async fn autofetch(&self) -> Result<()> {
let mut delay = chrono::Duration::minutes(5);
let mut source_id;
let mut next_fetch: DateTime<chrono::Local>;
let mut now;
loop {
let mut rows = sqlx::query("select source_id, next_fetch from rsstg_order limit 1;")
let mut rows = sqlx::query("select source_id, username, next_fetch from rsstg_order natural left join rsstg_source natural left join rsstg_channel;")
.fetch(&self.pool);
while let Some(row) = rows.try_next().await.unwrap() {
now = chrono::Local::now();
source_id = row.try_get("source_id")?;
let source_id: i32 = row.try_get("source_id")?;
next_fetch = row.try_get("next_fetch")?;
if next_fetch < now {
match sqlx::query("update rsstg_source set last_scrape = now() + interval '1 hour' where source_id = $1;")
.bind(source_id)
.execute(&self.pool).await {
Ok(_) => {},
Err(err) => {
self.debug(&err.to_string())?;
},
};
let clone = self.clone();
let username: String = row.try_get("username")?;
let username = username.clone();
tokio::spawn(async move {
if let Err(err) = clone.check(&username, Some(true)).await {
eprintln!("connection error: {}", err);
}
});
&self.check(source_id, Some(true)).await?;
//&self.check(row.try_get("username")?, Some(true)).await?;
} else {
if next_fetch - now < delay {
delay = next_fetch - now;
delay = next_fetch - now;
if delay > chrono::Duration::minutes(5) {
delay = chrono::Duration::minutes(5);
}
}
};
tokio::time::delay_for(delay.to_std()?).await;
}
//Ok(())
}
}
#[tokio::main]
#[tokio::main(basic_scheduler)]
async fn main() -> Result<()> {
let mut settings = config::Config::default();
settings.merge(config::File::with_name("rsstg"))?;
let re_username = Regex::new(r"^@[a-z][a-z0-9_]+$")?;
let re_link = Regex::new(r"^https?://[a-z.0-9]+/[-_a-z.0-9/]+$")?;
let re_username = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$")?;
let re_link = Regex::new(r"^https?://[a-zA-Z.0-9]+/[-_a-zA-Z.0-9/?=]+$")?;
let re_iv_hash = Regex::new(r"^[a-f0-9]{14}$")?;
/*
tokio::spawn(async move {
if let Err(e) = connection.await {
eprintln!("connection error: {}", e);
}
}); */
let core = Core::new(settings).await?;
/*
let mut bot = tbot::Bot::new(settings.get_str("api_key")?).event_loop();
bot.command("start", //"Start working.",
|context| async move {
context.send_message_in_reply("Not in service yet. Try later.").call().await.unwrap();
},
);
bot.command("list", //"List channels.",
|context| async move {
dbg!(&context.chat);
let mut res = "Channels:\n".to_owned();
let mut rows = sqlx::query("select username, channel_id, url, iv_hash from rsstg_source left join rsstg_channel using (channel_id) where owner = $1")
.bind(context.chat.id.0)
.fetch(&pool);
while let Some(row) = rows.try_next().await.unwrap() {
let username: &str = row.try_get("username").unwrap();
let channel_id: &str = row.try_get("channel_id").unwrap();
let url: &str = row.try_get("url").unwrap();
let iv_hash: &str = row.try_get("iv_hash").unwrap();
res.push_str(&format!("`{}`: `{}` iv:`{}`\n", username, url, iv_hash));
//match row.get(3) as str {
//Some(x) => x,
//_ => "None"
//}));
}
context.send_message_in_reply(&res).call().await.unwrap();
},
);
*/
let mut stream = core.stream();
while let Some(update) = stream.next().await {
let update = update?;
match update.kind {
UpdateKind::Message(message) => {
let mut reply: Vec<String> = vec![];
|
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
-
-
+
+
-
+
-
-
+
-
-
+
+
-
+
|
let mut rows = sqlx::query("select username, enabled, url, iv_hash from rsstg_source left join rsstg_channel using (channel_id) where owner = $1")
.bind(i64::from(message.from.id))
.fetch(&core.pool);
while let Some(row) = rows.try_next().await? {
let username: &str = row.try_get("username")?;
let enabled: bool = row.try_get("enabled")?;
let url: &str = row.try_get("url")?;
let iv_hash: &str = row.try_get("iv_hash")?;
reply.push(format!("\n\\*ļøā£ `{}` {}\nš `{}`\nIV `{}`", username,
let iv_hash: Option<&str> = row.try_get("iv_hash")?;
reply.push(format!("\n\\*ļøā£ `{}` {}\nš `{}`", username,
match enabled {
true => "š enabled",
false => "ā disabled",
}, url, iv_hash));
}, url));
//match row.get(3) as str {
//Some(x) => x,
if let Some(hash) = iv_hash {
//_ => "None"
//}));
reply.push(format!("IV `{}`", hash));
}
}
},
// add
"/add" => {
let (channel, url, iv_hash) = (words.next().unwrap(), words.next().unwrap(), words.next());
let ok_link = re_link.is_match(&url);
let ok_hash = match iv_hash {
Some(hash) => re_iv_hash.is_match(&hash),
None => true,
};
if ! ok_link {
reply.push("Link should be link to atom/rss feed, something like \"https://domain/path\".".to_string());
reply.push("Link should be link to atom/rss feed, something like \"https://domain/path\"\\.".to_string());
core.debug(&format!("Url: {:?}", &url))?;
}
if ! ok_hash {
reply.push("IV hash should be 14 hex digits.".to_string());
core.debug(&format!("IV: {:?}", &iv_hash))?;
}
if ok_link && ok_hash {
|
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
485
486
487
488
489
490
491
492
493
494
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
|
},
}
}
},
_ => {},
};
}
/*
loop {
println!("cycle");
for _ in botdb.query("select owner from rsstg_updates where owner is NULL limit 1;", &[])? {
for row in botdb.query("update rsstg_updates set owner = $1 where update->>'update_id' = ( select update->>'update_id' from rsstg_updates where owner is NULL limit 1 for update skip locked ) returning update;", &[owner])? {
let u :types::Update = serde_json::from_value(row.get(0))?;
println!("update: {:?}", &u);
/*
if let Some(message) = &u.message {
//if u["message"] != None {
if let (Some(entities), Some(text)) = (&message.entities, &message.text) {
//if u["message"]["entities"] {
for entry in entities {
if &entry.type_ == "bot_command" {
println!("command: {:?}", &text.chars().skip(entry.offset as usize).take(entry.length as usize).collect::<String>());
}
println!("entity: {:?}", &entry);
}
}
}
*/
}
}
std::process::exit(0);
}
*/
//bot.polling().start().await.unwrap();
Ok(())
}
|