aob-lemmy-bot/src/main.rs

105 lines
2.8 KiB
Rust
Raw Permalink Normal View History

2023-12-17 19:35:37 +00:00
use chrono::{DateTime, Duration, Utc};
use once_cell::sync::Lazy;
2023-12-17 19:18:03 +00:00
use reqwest::{Client};
2023-12-17 19:35:37 +00:00
use std::{collections::HashMap, vec};
2023-12-17 19:18:03 +00:00
use std::fmt::Debug;
use std::sync::{Arc};
use tokio::sync::{RwLock};
use dotenv::dotenv;
use strum_macros::Display;
2023-12-17 19:35:37 +00:00
use tokio::time::sleep;
2023-12-17 19:18:03 +00:00
use crate::config::Config;
use crate::post_history::{SeriesHistory};
mod config;
2023-12-17 19:18:03 +00:00
mod jnovel;
mod bot;
mod lemmy;
mod tui;
mod post_history;
2023-12-17 19:18:03 +00:00
pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
2023-12-17 19:35:37 +00:00
Client::builder()
2023-09-18 21:01:22 +00:00
.timeout(Duration::seconds(30).to_std().unwrap())
.connect_timeout(Duration::seconds(30).to_std().unwrap())
.build()
2023-12-17 19:35:37 +00:00
.expect("build client")
});
2023-12-17 19:18:03 +00:00
#[derive(Clone, Debug)]
pub(crate) struct SharedData {
messages: Vec<Message>,
config: Config,
2023-12-17 19:18:03 +00:00
post_history: SeriesHistory,
start: DateTime<Utc>,
}
2023-12-17 19:18:03 +00:00
impl SharedData {
pub(crate) fn new() -> Self {
SharedData {
messages: vec![],
config: Config {
instance: "".to_string(),
status_post_url: None,
config_reload_seconds: 0,
2023-12-18 10:23:47 +00:00
protected_communities: vec![],
2023-12-17 19:18:03 +00:00
series: vec![],
},
post_history: SeriesHistory {
series: HashMap::new(),
},
start: Utc::now(),
}
}
2023-12-17 19:18:03 +00:00
pub(crate) fn get_messages(&self, errors: bool, warnings: bool, infos: bool) -> Vec<Message> {
self.messages.iter().filter(|msg| {
match msg {
2023-12-17 19:35:37 +00:00
Message::Error(_) => errors,
Message::Warning(_) => warnings,
Message::Info(_) => infos,
2023-09-18 21:01:22 +00:00
}
2023-12-17 19:18:03 +00:00
}).cloned().collect()
2023-07-29 00:32:58 +00:00
}
}
2023-12-17 22:37:18 +00:00
#[derive(Clone, Debug, Display, PartialEq)]
2023-12-17 19:18:03 +00:00
pub(crate) enum Message {
Info(String),
Warning(String),
Error(String),
}
2023-12-17 21:29:18 +00:00
impl Message {
pub(crate) fn content(&self) -> String {
match self {
Message::Info(msg) => msg.clone(),
Message::Warning(msg) => msg.clone(),
Message::Error(msg) => msg.clone(),
}
}
}
#[tokio::main]
async fn main() {
2023-12-17 19:18:03 +00:00
dotenv().ok();
let mut data = SharedData::new();
2023-09-18 21:01:22 +00:00
2023-12-17 19:18:03 +00:00
loop {
let write_data = Arc::new(RwLock::new(data.clone()));
let read_data = write_data.clone();
let persistent_data = write_data.clone();
2023-12-17 19:18:03 +00:00
let tui_thread = tokio::spawn(async move { tui::run(read_data).await });
let bot_thread = tokio::spawn(async move { bot::run(write_data).await });
let _ = bot_thread.await;
tui_thread.abort();
2023-12-17 19:18:03 +00:00
data = persistent_data.read().await.clone();
2023-12-17 19:35:37 +00:00
data.messages.push(Message::Error("Bot crashed due to unknown Error, restarting thread after wait...".to_string()));
sleep(Duration::seconds(5).to_std().expect("Conversion should always work since static")).await;
}
}