Overview
Comment: | 0.1.5: logging, mostly |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
ec616a2a43efaf4946a3e4c9e3598930 |
User & Date: | arcade on 2020-11-19 17:43:05.356 |
Other Links: | manifest | tags |
Context
2020-11-20
| ||
16:23 | force cast to utilize index check-in: 84130ab6bf user: arcade tags: trunk | |
2020-11-19
| ||
17:43 | 0.1.5: logging, mostly check-in: ec616a2a43 user: arcade tags: trunk | |
2020-11-18
| ||
19:46 | 0.1.4: fetch less data, sort posts by post date before posting check-in: 6ac5737a72 user: arcade tags: trunk | |
Changes
Modified Cargo.toml
from [a133f234ff]
to [32385ff949].
1 2 | [package] name = "rsstg" | | | 1 2 3 4 5 6 7 8 9 10 | [package] name = "rsstg" version = "0.1.5" authors = ["arcade"] edition = "2018" [dependencies] config = "*" futures = "*" |
︙ | ︙ |
Modified src/main.rs
from [363e6b408a]
to [b8785d39ca].
︙ | ︙ | |||
69 70 71 72 73 74 75 | }; let url: &str = row.try_get("url")?; let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None; let iv_hash: Option<&str> = row.try_get("iv_hash")?; let mut posts: BTreeMap<DateTime<chrono::FixedOffset>, String> = BTreeMap::new(); match rss::Channel::from_url(url) { Ok(feed) => { | < | 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | }; let url: &str = row.try_get("url")?; let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None; let iv_hash: Option<&str> = row.try_get("iv_hash")?; let mut posts: BTreeMap<DateTime<chrono::FixedOffset>, String> = BTreeMap::new(); match rss::Channel::from_url(url) { Ok(feed) => { for item in feed.items() { let date = match item.pub_date() { 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(); posts.insert(date.clone(), url.clone()); |
︙ | ︙ | |||
120 121 122 123 124 125 126 | self.debug(&err.to_string())?; }, }; }; posts.clear(); }, Err(err) => { | | | 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | self.debug(&err.to_string())?; }, }; }; posts.clear(); }, Err(err) => { self.debug(&format!("Problem opening feed url:\n{}\n{}", &url, &err.to_string()))?; }, }; match sqlx::query("update rsstg_source set last_scrape = now() where source_id = $1;") .bind(id) .execute(&self.pool).await { Ok(_) => {}, Err(err) => { |
︙ | ︙ | |||
180 181 182 183 184 185 186 | } async fn autofetch(&self) -> Result<()> { let mut delay = chrono::Duration::minutes(5); let mut next_fetch: DateTime<chrono::Local>; let mut now; loop { | < | 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | } async fn autofetch(&self) -> Result<()> { let mut delay = chrono::Duration::minutes(5); let mut next_fetch: DateTime<chrono::Local>; let mut now; loop { now = chrono::Local::now(); let mut rows = sqlx::query("select source_id, next_fetch from rsstg_order natural left join rsstg_source natural left join rsstg_channel where next_fetch < now();") .fetch(&self.pool); while let Some(row) = rows.try_next().await.unwrap() { let source_id: i32 = row.try_get("source_id")?; next_fetch = row.try_get("next_fetch")?; if next_fetch < now { |
︙ | ︙ | |||
216 217 218 219 220 221 222 | delay = chrono::Duration::minutes(5); } //Ok(()) } } | | | > | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | > > > > > > | 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 270 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 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 | delay = chrono::Duration::minutes(5); } //Ok(()) } } #[tokio::main] async fn main() -> Result<()> { let mut settings = config::Config::default(); settings.merge(config::File::with_name("rsstg"))?; 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}$")?; let core = Core::new(settings).await?; let mut stream = core.stream(); while let Some(update) = stream.next().await { match update { Ok(update) => { match update.kind { UpdateKind::Message(message) => { let mut reply: Vec<String> = vec![]; match message.kind { MessageKind::Text { ref data, .. } => { let mut words = data.split_whitespace(); let cmd = words.next().unwrap(); match cmd { // start "/start" => { reply.push("Not in service yet\\. Try later\\.".to_string()); }, // list "/list" => { reply.push("Channels:".to_string()); let mut rows = sqlx::query("select source_id, username, enabled, url, iv_hash from rsstg_source left join rsstg_channel using (channel_id) where owner = $1 order by source_id") .bind(i64::from(message.from.id)) .fetch(&core.pool); while let Some(row) = rows.try_next().await? { let source_id: i32 = row.try_get("source_id")?; let username: &str = row.try_get("username")?; let enabled: bool = row.try_get("enabled")?; let url: &str = row.try_get("url")?; let iv_hash: Option<&str> = row.try_get("iv_hash")?; reply.push(format!("\n\\#️⃣ {} \\*️⃣ `{}` {}\n🔗 `{}`", source_id, username, match enabled { true => "🔄 enabled", false => "⛔ disabled", }, url)); if let Some(hash) = iv_hash { reply.push(format!("IV `{}`", hash)); } } }, // add "/add" | "/update" => { let mut source_id: i32 = 0; if cmd == "/update" { source_id = words.next().unwrap().parse::<i32>()?; } 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()); 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 { let chan: Option<i64> = match sqlx::query("select channel_id from rsstg_channel where username = $1") .bind(channel) .fetch_one(&core.pool).await { Ok(chan) => Some(chan.try_get("channel_id")?), Err(sqlx::Error::RowNotFound) => { reply.push("Sorry, I don't know about that channel. Please, add a channel with /addchan.".to_string()); None }, Err(err) => { reply.push("Sorry, unknown error\\.".to_string()); core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?; None }, }; if let Some(chan) = chan { match if cmd == "/update" { sqlx::query("update rsstg_source set channel_id = $2, url = $3, iv_hash = $4, owner = $4 where source_id = $1").bind(source_id) } else { sqlx::query("insert into rsstg_source (channel_id, url, iv_hash, owner) values ($1, $2, $3, $4)") } .bind(chan) .bind(url) .bind(iv_hash) .bind(i64::from(message.from.id)) .execute(&core.pool).await { Ok(_) => reply.push("Channel added\\.".to_string()), Err(sqlx::Error::Database(err)) => { match err.downcast::<sqlx::postgres::PgDatabaseError>().routine() { Some("_bt_check_unique", ) => { reply.push("Duplicate key\\.".to_string()); }, Some(_) => { reply.push("Database error\\.".to_string()); }, None => { reply.push("No database error extracted\\.".to_string()); }, }; }, Err(err) => { reply.push("Sorry, unknown error\\.".to_string()); core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?; }, }; }; }; }, // addchan "/addchan" => { let channel = words.next().unwrap(); if ! re_username.is_match(&channel) { reply.push("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?".to_string()); } else { let chan: Option<i64> = match sqlx::query("select channel_id from rsstg_channel where username = $1") .bind(channel) .fetch_one(&core.pool).await { Ok(chan) => Some(chan.try_get("channel_id")?), Err(sqlx::Error::RowNotFound) => None, Err(err) => { reply.push("Sorry, unknown error\\.".to_string()); core.debug(&format!("Sorry, unknown error: {:#?}\n", err))?; None }, }; match chan { Some(chan) => { let new_chan = core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatId::new(chan))).await?; if i64::from(new_chan.id()) == chan { reply.push("I already know that channel\\.".to_string()); } else { reply.push("Hmm, channel has changed… I'll fix it later\\.".to_string()); }; }, None => { match core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await { Ok(chan_adm) => { let (mut me, mut user) = (false, false); for admin in &chan_adm { if admin.user.id == core.my.id { me = true; }; if admin.user.id == message.from.id { user = true; }; }; if ! me { reply.push("I need to be admin on that channel\\.".to_string()); }; if ! user { reply.push("You should be admin on that channel\\.".to_string()); }; if me && user { let chan_id = core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await?; sqlx::query("insert into rsstg_channel (channel_id, username) values ($1, $2);") .bind(i64::from(chan_id.id())) .bind(channel) .execute(&core.pool).await?; reply.push("Good, I know that channel now\\.\n".to_string()); }; }, Err(_) => { reply.push("Sorry, I have no access to that chat\\.".to_string()); }, }; }, }; }; }, // check "/check" => { &core.check(&words.next().unwrap().parse::<i32>()?, None).await?; }, // clear "/clean" => { if core.owner != i64::from(message.from.id) { reply.push("Reserved for testing\\.".to_string()); } else { let source_id = words.next().unwrap().parse::<i32>().unwrap_or(0); &core.clean(source_id).await?; } }, // enable "/enable" => { match core.enable(&words.next().unwrap().parse::<i32>()?).await { Ok(_) => { reply.push("Channel enabled\\.".to_string()); } Err(err) => { core.debug(&err.to_string())?; }, }; }, // disable "/disable" => { match core.disable(&words.next().unwrap().parse::<i32>()?).await { Ok(_) => { reply.push("Channel disabled\\.".to_string()); } Err(err) => { core.debug(&err.to_string())?; }, }; }, _ => { }, }; }, _ => { }, }; if reply.len() > 0 { match core.tg.send(message.text_reply(reply.join("\n")).parse_mode(types::ParseMode::MarkdownV2)).await { Ok(_) => {}, Err(err) => { dbg!(reply.join("\n")); println!("{}", err); }, } } }, _ => {}, }; }, Err(err) => { core.debug(&err.to_string())?; }, }; } Ok(()) } |