9171c791eb 2021-11-13 1: use anyhow::{anyhow, bail, Context, Result};
cb86e770f9 2022-03-15 2: use async_std::task;
9171c791eb 2021-11-13 3: use chrono::DateTime;
9910c2209c 2023-08-04 4: use sqlx::postgres::PgPoolOptions;
9171c791eb 2021-11-13 5: use std::{
c1e27b74ed 2021-11-13 6: borrow::Cow,
9171c791eb 2021-11-13 7: collections::{
9171c791eb 2021-11-13 8: BTreeMap,
9171c791eb 2021-11-13 9: HashSet,
9171c791eb 2021-11-13 10: },
9171c791eb 2021-11-13 11: sync::{Arc, Mutex},
9171c791eb 2021-11-13 12: };
9171c791eb 2021-11-13 13:
9171c791eb 2021-11-13 14: #[derive(Clone)]
9171c791eb 2021-11-13 15: pub struct Core {
9171c791eb 2021-11-13 16: owner_chat: telegram_bot::UserId,
9171c791eb 2021-11-13 17: pub tg: telegram_bot::Api,
9171c791eb 2021-11-13 18: pub my: telegram_bot::User,
9171c791eb 2021-11-13 19: pool: sqlx::Pool<sqlx::Postgres>,
9171c791eb 2021-11-13 20: sources: Arc<Mutex<HashSet<Arc<i32>>>>,
45e34762e4 2023-05-28 21: http_client: reqwest::Client,
9171c791eb 2021-11-13 22: }
9171c791eb 2021-11-13 23:
9171c791eb 2021-11-13 24: impl Core {
cb86e770f9 2022-03-15 25: pub fn new(settings: config::Config) -> Result<Arc<Core>> {
9171c791eb 2021-11-13 26: let owner = settings.get_int("owner")?;
28da2e2a00 2022-03-12 27: let api_key = settings.get_string("api_key")?;
aae1b6f580 2023-05-27 28: let tg = telegram_bot::Api::new(api_key);
cb86e770f9 2022-03-15 29: let tg_cloned = tg.clone();
45e34762e4 2023-05-28 30:
45e34762e4 2023-05-28 31: let mut client = reqwest::Client::builder();
9d8a6738fd 2023-07-30 32: if let Ok(proxy) = settings.get_string("proxy") {
45e34762e4 2023-05-28 33: let proxy = reqwest::Proxy::all(proxy)?;
45e34762e4 2023-05-28 34: client = client.proxy(proxy);
45e34762e4 2023-05-28 35: }
45e34762e4 2023-05-28 36: let http_client = client.build()?;
f988dfd28f 2022-02-13 37: let core = Arc::new(Core {
f988dfd28f 2022-02-13 38: tg,
cb86e770f9 2022-03-15 39: my: task::block_on(async {
cb86e770f9 2022-03-15 40: tg_cloned.send(telegram_bot::GetMe).await
cb86e770f9 2022-03-15 41: })?,
9171c791eb 2021-11-13 42: owner_chat: telegram_bot::UserId::new(owner),
9171c791eb 2021-11-13 43: pool: PgPoolOptions::new()
9171c791eb 2021-11-13 44: .max_connections(5)
e7effaf7fb 2022-07-26 45: .acquire_timeout(std::time::Duration::new(300, 0))
9171c791eb 2021-11-13 46: .idle_timeout(std::time::Duration::new(60, 0))
28da2e2a00 2022-03-12 47: .connect_lazy(&settings.get_string("pg")?)?,
9171c791eb 2021-11-13 48: sources: Arc::new(Mutex::new(HashSet::new())),
45e34762e4 2023-05-28 49: http_client,
f988dfd28f 2022-02-13 50: });
9171c791eb 2021-11-13 51: let clone = core.clone();
cb86e770f9 2022-03-15 52: task::spawn(async move {
26339860ce 2022-02-13 53: loop {
26339860ce 2022-02-13 54: let delay = match &clone.autofetch().await {
26339860ce 2022-02-13 55: Err(err) => {
26339860ce 2022-02-13 56: if let Err(err) = clone.send(format!("š {:?}", err), None, None).await {
26339860ce 2022-02-13 57: eprintln!("Autofetch error: {}", err);
26339860ce 2022-02-13 58: };
cb86e770f9 2022-03-15 59: std::time::Duration::from_secs(60)
26339860ce 2022-02-13 60: },
26339860ce 2022-02-13 61: Ok(time) => *time,
9171c791eb 2021-11-13 62: };
cb86e770f9 2022-03-15 63: task::sleep(delay).await;
9171c791eb 2021-11-13 64: }
9171c791eb 2021-11-13 65: });
9171c791eb 2021-11-13 66: Ok(core)
9171c791eb 2021-11-13 67: }
9171c791eb 2021-11-13 68:
9171c791eb 2021-11-13 69: pub fn stream(&self) -> telegram_bot::UpdatesStream {
9171c791eb 2021-11-13 70: self.tg.stream()
9171c791eb 2021-11-13 71: }
9171c791eb 2021-11-13 72:
26339860ce 2022-02-13 73: pub async fn send<'a, S>(&self, msg: S, target: Option<telegram_bot::UserId>, mode: Option<telegram_bot::types::ParseMode>) -> Result<()>
c1e27b74ed 2021-11-13 74: where S: Into<Cow<'a, str>> {
26339860ce 2022-02-13 75: let mode = mode.unwrap_or(telegram_bot::types::ParseMode::Html);
26339860ce 2022-02-13 76: let target = target.unwrap_or(self.owner_chat);
26339860ce 2022-02-13 77: self.tg.send(telegram_bot::SendMessage::new(target, msg).parse_mode(mode)).await?;
9171c791eb 2021-11-13 78: Ok(())
9171c791eb 2021-11-13 79: }
9171c791eb 2021-11-13 80:
659724c658 2021-12-08 81: pub async fn check<S>(&self, id: &i32, owner: S, real: bool) -> Result<Cow<'_, str>>
9171c791eb 2021-11-13 82: where S: Into<i64> {
c1e27b74ed 2021-11-13 83: let owner = owner.into();
c1e27b74ed 2021-11-13 84:
9171c791eb 2021-11-13 85: let mut posted: i32 = 0;
9171c791eb 2021-11-13 86: let id = {
9171c791eb 2021-11-13 87: let mut set = self.sources.lock().unwrap();
9171c791eb 2021-11-13 88: match set.get(id) {
9171c791eb 2021-11-13 89: Some(id) => id.clone(),
9171c791eb 2021-11-13 90: None => {
9171c791eb 2021-11-13 91: let id = Arc::new(*id);
9171c791eb 2021-11-13 92: set.insert(id.clone());
9171c791eb 2021-11-13 93: id.clone()
9171c791eb 2021-11-13 94: },
9171c791eb 2021-11-13 95: }
9171c791eb 2021-11-13 96: };
9171c791eb 2021-11-13 97: let count = Arc::strong_count(&id);
9171c791eb 2021-11-13 98: if count == 2 {
9910c2209c 2023-08-04 99: let source = sqlx::query!("select source_id, channel_id, url, iv_hash, owner, url_re from rsstg_source where source_id = $1 and owner = $2",
9910c2209c 2023-08-04 100: *id, owner).fetch_one(&mut self.pool.acquire().await?).await?;
9171c791eb 2021-11-13 101: let destination = match real {
9910c2209c 2023-08-04 102: true => telegram_bot::UserId::new(source.channel_id),
9910c2209c 2023-08-04 103: false => telegram_bot::UserId::new(source.owner),
9171c791eb 2021-11-13 104: };
9171c791eb 2021-11-13 105: let mut this_fetch: Option<DateTime<chrono::FixedOffset>> = None;
9171c791eb 2021-11-13 106: let mut posts: BTreeMap<DateTime<chrono::FixedOffset>, String> = BTreeMap::new();
45e34762e4 2023-05-28 107:
9910c2209c 2023-08-04 108: let response = self.http_client.get(&source.url).send().await?;
613a665847 2021-11-15 109: let status = response.status();
613a665847 2021-11-15 110: let content = response.bytes().await?;
9171c791eb 2021-11-13 111: match rss::Channel::read_from(&content[..]) {
9171c791eb 2021-11-13 112: Ok(feed) => {
9171c791eb 2021-11-13 113: for item in feed.items() {
f988dfd28f 2022-02-13 114: if let Some(link) = item.link() {
f988dfd28f 2022-02-13 115: let date = match item.pub_date() {
f988dfd28f 2022-02-13 116: Some(feed_date) => DateTime::parse_from_rfc2822(feed_date),
f988dfd28f 2022-02-13 117: None => DateTime::parse_from_rfc3339(&item.dublin_core_ext().unwrap().dates()[0]),
f988dfd28f 2022-02-13 118: }?;
f988dfd28f 2022-02-13 119: let url = link;
26339860ce 2022-02-13 120: posts.insert(date, url.to_string());
9171c791eb 2021-11-13 121: }
9171c791eb 2021-11-13 122: };
9171c791eb 2021-11-13 123: },
9171c791eb 2021-11-13 124: Err(err) => match err {
9171c791eb 2021-11-13 125: rss::Error::InvalidStartTag => {
9171c791eb 2021-11-13 126: let feed = atom_syndication::Feed::read_from(&content[..])
9910c2209c 2023-08-04 127: .with_context(|| format!("Problem opening feed url:\n{}\n{}", &source.url, status))?;
9171c791eb 2021-11-13 128: for item in feed.entries() {
9171c791eb 2021-11-13 129: let date = item.published().unwrap();
9171c791eb 2021-11-13 130: let url = item.links()[0].href();
26339860ce 2022-02-13 131: posts.insert(*date, url.to_string());
9171c791eb 2021-11-13 132: };
9171c791eb 2021-11-13 133: },
9171c791eb 2021-11-13 134: rss::Error::Eof => (),
9910c2209c 2023-08-04 135: _ => bail!("Unsupported or mangled content:\n{:?}\n{:#?}\n{:#?}\n", &source.url, err, status)
9171c791eb 2021-11-13 136: }
9171c791eb 2021-11-13 137: };
9171c791eb 2021-11-13 138: for (date, url) in posts.iter() {
9910c2209c 2023-08-04 139: let post_url: Cow<str> = match source.url_re {
9910c2209c 2023-08-04 140: Some(ref x) => sedregex::ReplaceCommand::new(x)?.execute(&source.url),
659724c658 2021-12-08 141: None => url.into(),
659724c658 2021-12-08 142: };
9910c2209c 2023-08-04 143: if let Some(exists) = sqlx::query!("select exists(select true from rsstg_post where url = $1 and source_id = $2) as exists;",
9910c2209c 2023-08-04 144: &post_url, *id).fetch_one(&mut self.pool.acquire().await?).await?.exists {
9910c2209c 2023-08-04 145: if ! exists {
9910c2209c 2023-08-04 146: if this_fetch.is_none() || *date > this_fetch.unwrap() {
9910c2209c 2023-08-04 147: this_fetch = Some(*date);
9910c2209c 2023-08-04 148: };
9910c2209c 2023-08-04 149: self.tg.send( match &source.iv_hash {
9910c2209c 2023-08-04 150: Some(hash) => telegram_bot::SendMessage::new(destination, format!("<a href=\"https://t.me/iv?url={}&rhash={}\"> </a>{0}", &post_url, hash)),
9910c2209c 2023-08-04 151: None => telegram_bot::SendMessage::new(destination, format!("{}", post_url)),
9910c2209c 2023-08-04 152: }.parse_mode(telegram_bot::types::ParseMode::Html)).await
9910c2209c 2023-08-04 153: .context("Can't post message:")?;
9910c2209c 2023-08-04 154: sqlx::query!("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);",
9910c2209c 2023-08-04 155: *id, date, &post_url).execute(&mut self.pool.acquire().await?).await?;
9910c2209c 2023-08-04 156: task::sleep(std::time::Duration::new(4, 0)).await;
659724c658 2021-12-08 157: };
9171c791eb 2021-11-13 158: };
9171c791eb 2021-11-13 159: posted += 1;
9171c791eb 2021-11-13 160: };
9171c791eb 2021-11-13 161: posts.clear();
9171c791eb 2021-11-13 162: };
9910c2209c 2023-08-04 163: sqlx::query!("update rsstg_source set last_scrape = now() where source_id = $1;",
9910c2209c 2023-08-04 164: *id).execute(&mut self.pool.acquire().await?).await?;
659724c658 2021-12-08 165: Ok(format!("Posted: {}", &posted).into())
659724c658 2021-12-08 166: }
659724c658 2021-12-08 167:
659724c658 2021-12-08 168: pub async fn delete<S>(&self, source_id: &i32, owner: S) -> Result<Cow<'_, str>>
659724c658 2021-12-08 169: where S: Into<i64> {
659724c658 2021-12-08 170: let owner = owner.into();
659724c658 2021-12-08 171:
9910c2209c 2023-08-04 172: match sqlx::query!("delete from rsstg_source where source_id = $1 and owner = $2;",
9910c2209c 2023-08-04 173: source_id, owner).execute(&mut self.pool.acquire().await?).await?.rows_affected() {
659724c658 2021-12-08 174: 0 => { Ok("No data found found.".into()) },
659724c658 2021-12-08 175: x => { Ok(format!("{} sources removed.", x).into()) },
659724c658 2021-12-08 176: }
659724c658 2021-12-08 177: }
659724c658 2021-12-08 178:
659724c658 2021-12-08 179: pub async fn clean<S>(&self, source_id: &i32, owner: S) -> Result<Cow<'_, str>>
659724c658 2021-12-08 180: where S: Into<i64> {
659724c658 2021-12-08 181: let owner = owner.into();
659724c658 2021-12-08 182:
9910c2209c 2023-08-04 183: match sqlx::query!("delete from rsstg_post p using rsstg_source s where p.source_id = $1 and owner = $2 and p.source_id = s.source_id;",
9910c2209c 2023-08-04 184: source_id, owner).execute(&mut self.pool.acquire().await?).await?.rows_affected() {
659724c658 2021-12-08 185: 0 => { Ok("No data found found.".into()) },
659724c658 2021-12-08 186: x => { Ok(format!("{} posts purged.", x).into()) },
9171c791eb 2021-11-13 187: }
9171c791eb 2021-11-13 188: }
9171c791eb 2021-11-13 189:
9171c791eb 2021-11-13 190: pub async fn enable<S>(&self, source_id: &i32, owner: S) -> Result<&str>
9171c791eb 2021-11-13 191: where S: Into<i64> {
c1e27b74ed 2021-11-13 192: let owner = owner.into();
c1e27b74ed 2021-11-13 193:
9910c2209c 2023-08-04 194: match sqlx::query!("update rsstg_source set enabled = true where source_id = $1 and owner = $2",
9910c2209c 2023-08-04 195: source_id, owner).execute(&mut self.pool.acquire().await?).await?.rows_affected() {
10c25017bb 2021-11-13 196: 1 => { Ok("Source enabled.") },
10c25017bb 2021-11-13 197: 0 => { Ok("Source not found.") },
9171c791eb 2021-11-13 198: _ => { Err(anyhow!("Database error.")) },
9171c791eb 2021-11-13 199: }
9171c791eb 2021-11-13 200: }
9171c791eb 2021-11-13 201:
9171c791eb 2021-11-13 202: pub async fn disable<S>(&self, source_id: &i32, owner: S) -> Result<&str>
9171c791eb 2021-11-13 203: where S: Into<i64> {
c1e27b74ed 2021-11-13 204: let owner = owner.into();
c1e27b74ed 2021-11-13 205:
9910c2209c 2023-08-04 206: match sqlx::query!("update rsstg_source set enabled = false where source_id = $1 and owner = $2",
9910c2209c 2023-08-04 207: source_id, owner).execute(&mut self.pool.acquire().await?).await?.rows_affected() {
10c25017bb 2021-11-13 208: 1 => { Ok("Source disabled.") },
10c25017bb 2021-11-13 209: 0 => { Ok("Source not found.") },
9171c791eb 2021-11-13 210: _ => { Err(anyhow!("Database error.")) },
9171c791eb 2021-11-13 211: }
9171c791eb 2021-11-13 212: }
9171c791eb 2021-11-13 213:
26339860ce 2022-02-13 214: pub async fn update<S>(&self, update: Option<i32>, channel: &str, channel_id: i64, url: &str, iv_hash: Option<&str>, url_re: Option<&str>, owner: S) -> Result<&str>
26339860ce 2022-02-13 215: where S: Into<i64> {
26339860ce 2022-02-13 216: let owner = owner.into();
9171c791eb 2021-11-13 217:
9171c791eb 2021-11-13 218: match match update {
9171c791eb 2021-11-13 219: Some(id) => {
9910c2209c 2023-08-04 220: sqlx::query!("update rsstg_source set channel_id = $2, url = $3, iv_hash = $4, owner = $5, channel = $6, url_re = $7 where source_id = $1",
9910c2209c 2023-08-04 221: id, channel_id, url, iv_hash, owner, channel, url_re).execute(&mut self.pool.acquire().await?).await
9171c791eb 2021-11-13 222: },
9171c791eb 2021-11-13 223: None => {
9910c2209c 2023-08-04 224: sqlx::query!("insert into rsstg_source (channel_id, url, iv_hash, owner, channel, url_re) values ($1, $2, $3, $4, $5, $6)",
9910c2209c 2023-08-04 225: channel_id, url, iv_hash, owner, channel, url_re).execute(&mut self.pool.acquire().await?).await
9171c791eb 2021-11-13 226: },
9910c2209c 2023-08-04 227: } {
f988dfd28f 2022-02-13 228: Ok(_) => Ok(match update {
10c25017bb 2021-11-13 229: Some(_) => "Channel updated.",
10c25017bb 2021-11-13 230: None => "Channel added.",
659724c658 2021-12-08 231: }),
9171c791eb 2021-11-13 232: Err(sqlx::Error::Database(err)) => {
9171c791eb 2021-11-13 233: match err.downcast::<sqlx::postgres::PgDatabaseError>().routine() {
9171c791eb 2021-11-13 234: Some("_bt_check_unique", ) => {
f988dfd28f 2022-02-13 235: Ok("Duplicate key.")
9171c791eb 2021-11-13 236: },
9171c791eb 2021-11-13 237: Some(_) => {
f988dfd28f 2022-02-13 238: Ok("Database error.")
9171c791eb 2021-11-13 239: },
9171c791eb 2021-11-13 240: None => {
f988dfd28f 2022-02-13 241: Ok("No database error extracted.")
9171c791eb 2021-11-13 242: },
f988dfd28f 2022-02-13 243: }
9171c791eb 2021-11-13 244: },
9171c791eb 2021-11-13 245: Err(err) => {
9171c791eb 2021-11-13 246: bail!("Sorry, unknown error:\n{:#?}\n", err);
9171c791eb 2021-11-13 247: },
f988dfd28f 2022-02-13 248: }
f988dfd28f 2022-02-13 249: }
f988dfd28f 2022-02-13 250:
26339860ce 2022-02-13 251: async fn autofetch(&self) -> Result<std::time::Duration> {
f988dfd28f 2022-02-13 252: let mut delay = chrono::Duration::minutes(1);
26339860ce 2022-02-13 253: let now = chrono::Local::now();
9910c2209c 2023-08-04 254: let mut queue = sqlx::query!(r#"select source_id, next_fetch as "next_fetch: DateTime<chrono::Local>", owner from rsstg_order natural left join rsstg_source where next_fetch < now() + interval '1 minute';"#)
9910c2209c 2023-08-04 255: .fetch_all(&mut self.pool.acquire().await?).await?;
26339860ce 2022-02-13 256: for row in queue.iter() {
9910c2209c 2023-08-04 257: if let Some(next_fetch) = row.next_fetch {
9910c2209c 2023-08-04 258: if next_fetch < now {
9910c2209c 2023-08-04 259: if let (Some(owner), Some(source_id)) = (row.owner, row.source_id) {
9910c2209c 2023-08-04 260: let clone = Core {
9910c2209c 2023-08-04 261: owner_chat: telegram_bot::UserId::new(owner),
9910c2209c 2023-08-04 262: ..self.clone()
9910c2209c 2023-08-04 263: };
9910c2209c 2023-08-04 264: task::spawn(async move {
9910c2209c 2023-08-04 265: if let Err(err) = clone.check(&source_id, owner, true).await {
9910c2209c 2023-08-04 266: if let Err(err) = clone.send(&format!("š {:?}", err), None, None).await {
9910c2209c 2023-08-04 267: eprintln!("Check error: {}", err);
9910c2209c 2023-08-04 268: };
9910c2209c 2023-08-04 269: };
9910c2209c 2023-08-04 270: });
9910c2209c 2023-08-04 271: }
9910c2209c 2023-08-04 272: } else if next_fetch - now < delay {
9910c2209c 2023-08-04 273: delay = next_fetch - now;
9910c2209c 2023-08-04 274: }
26339860ce 2022-02-13 275: }
26339860ce 2022-02-13 276: };
26339860ce 2022-02-13 277: queue.clear();
26339860ce 2022-02-13 278: Ok(delay.to_std()?)
a7f91033c0 2021-11-13 279: }
a7f91033c0 2021-11-13 280:
a7f91033c0 2021-11-13 281: pub async fn list<S>(&self, owner: S) -> Result<String>
a7f91033c0 2021-11-13 282: where S: Into<i64> {
a7f91033c0 2021-11-13 283: let owner = owner.into();
c1e27b74ed 2021-11-13 284:
659724c658 2021-12-08 285: let mut reply: Vec<Cow<str>> = vec![];
659724c658 2021-12-08 286: reply.push("Channels:".into());
9910c2209c 2023-08-04 287: let rows = sqlx::query!("select source_id, channel, enabled, url, iv_hash, url_re from rsstg_source where owner = $1 order by source_id",
9910c2209c 2023-08-04 288: owner).fetch_all(&mut self.pool.acquire().await?).await?;
a7f91033c0 2021-11-13 289: for row in rows.iter() {
9910c2209c 2023-08-04 290: reply.push(format!("\n\\#ļøā£ {} \\*ļøā£ `{}` {}\nš `{}`", row.source_id, row.channel,
9910c2209c 2023-08-04 291: match row.enabled {
a7f91033c0 2021-11-13 292: true => "š enabled",
a7f91033c0 2021-11-13 293: false => "ā disabled",
9910c2209c 2023-08-04 294: }, row.url).into());
9910c2209c 2023-08-04 295: if let Some(hash) = &row.iv_hash {
659724c658 2021-12-08 296: reply.push(format!("IV: `{}`", hash).into());
c1e27b74ed 2021-11-13 297: }
9910c2209c 2023-08-04 298: if let Some(re) = &row.url_re {
659724c658 2021-12-08 299: reply.push(format!("RE: `{}`", re).into());
a7f91033c0 2021-11-13 300: }
a7f91033c0 2021-11-13 301: };
a7f91033c0 2021-11-13 302: Ok(reply.join("\n"))
9171c791eb 2021-11-13 303: }
9171c791eb 2021-11-13 304: }