Compare commits
30 commits
Author | SHA1 | Date | |
---|---|---|---|
041590a559 | |||
d6f883f890 | |||
b07420e0bd | |||
0fc71f0a7d | |||
2ae6468ad8 | |||
2ecfe88cb9 | |||
7dcc7bfee2 | |||
94d8a4e673 | |||
1b585eab7e | |||
b6f5c38e4a | |||
5d708bdb82 | |||
6a8c1662f0 | |||
e02cd900ed | |||
32ea83a7bb | |||
4297860b9e | |||
affe62b973 | |||
6520cc65a3 | |||
aefceda628 | |||
ee5a159431 | |||
85f8b97607 | |||
966dd8f359 | |||
17e161bc27 | |||
3928367692 | |||
070eae961a | |||
92103e28ba | |||
cd78d3c1c7 | |||
22bbaaa002 | |||
9ee9db5792 | |||
23ac0de189 | |||
2dc695577e |
9 changed files with 1340 additions and 668 deletions
|
@ -137,7 +137,7 @@ jobs:
|
||||||
run: rm release_blobs/build.env
|
run: rm release_blobs/build.env
|
||||||
-
|
-
|
||||||
name: Release New Version
|
name: Release New Version
|
||||||
uses: actions/forgejo-release@v1
|
uses: actions/forgejo-release@v2
|
||||||
with:
|
with:
|
||||||
direction: upload
|
direction: upload
|
||||||
url: https://forgejo.neshweb.net
|
url: https://forgejo.neshweb.net
|
||||||
|
|
721
Cargo.lock
generated
721
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
33
Cargo.toml
33
Cargo.toml
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
authors = ["Neshura"]
|
authors = ["Neshura"]
|
||||||
name = "aob-lemmy-bot"
|
name = "aob-lemmy-bot"
|
||||||
version = "2.2.2"
|
version = "3.2.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
description = "Bot for automatically posting new chapters of 'Ascendance of a Bookworm' released by J-Novel Club"
|
description = "Bot for automatically posting new chapters of 'Ascendance of a Bookworm' released by J-Novel Club"
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
|
@ -16,19 +16,20 @@ systemd-units = { enable = false }
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = "^0.4.26"
|
chrono = "^0.4"
|
||||||
lemmy_api_common = "0.19.3"
|
lemmy_api_common = "0.19.5"
|
||||||
lemmy_db_schema = "0.19.3"
|
lemmy_db_schema = "0.19.5"
|
||||||
once_cell = "^1.18.0"
|
once_cell = "^1.19"
|
||||||
reqwest = { version = "^0.11.18", features = ["blocking", "json"] }
|
reqwest = { version = "^0.12", features = ["blocking", "json"] }
|
||||||
serde = "^1.0.164"
|
serde = "^1.0"
|
||||||
serde_derive = "^1.0.164"
|
serde_derive = "^1.0"
|
||||||
serde_json = "^1.0.97"
|
serde_json = "^1.0"
|
||||||
strum_macros = "^0.25.0"
|
strum_macros = "^0.26"
|
||||||
tokio = { version = "^1.32.0", features = ["rt", "rt-multi-thread", "macros"] }
|
tokio = { version = "^1.37", features = ["rt", "rt-multi-thread", "macros"] }
|
||||||
url = "^2.4.0"
|
url = "^2.5"
|
||||||
confy = "^0.5.1"
|
confy = "^0.6"
|
||||||
toml = "^0.8.8"
|
toml = "^0.8"
|
||||||
systemd-journal-logger = "^2.1.1"
|
systemd-journal-logger = "^2.1.1"
|
||||||
log = "^0.4.20"
|
log = "^0.4"
|
||||||
async-trait = "^0.1.77"
|
async-trait = "^0.1"
|
||||||
|
notify = "6.1.1"
|
317
src/bot.rs
317
src/bot.rs
|
@ -1,16 +1,21 @@
|
||||||
use crate::{config::{Config, PostBody, SeriesConfig}, fetchers::{jnovel}, lemmy};
|
use crate::{config::{Config}, HTTP_CLIENT};
|
||||||
use crate::fetchers::jnovel::JPostInfo;
|
use crate::lemmy::{Lemmy};
|
||||||
use crate::lemmy::{Lemmy, PostInfo};
|
use crate::post_history::{SeriesHistory};
|
||||||
use crate::post_history::SeriesHistory;
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use chrono::{DateTime, Duration, Timelike, Utc};
|
use std::sync::{Arc, RwLock};
|
||||||
use lemmy_api_common::post::CreatePost;
|
use notify::{Event, EventKind, event::{AccessKind, AccessMode}, RecursiveMode, Watcher};
|
||||||
use lemmy_db_schema::newtypes::{CommunityId, LanguageId};
|
|
||||||
use lemmy_db_schema::PostFeatureType;
|
|
||||||
use std::collections::HashMap;
|
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
use crate::fetchers::Fetcher;
|
|
||||||
use systemd_journal_logger::connected_to_journal;
|
use systemd_journal_logger::connected_to_journal;
|
||||||
|
|
||||||
|
macro_rules! debug {
|
||||||
|
($msg:tt) => {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => log::debug!("[DEBUG] {}", $msg),
|
||||||
|
false => println!("[DEBUG] {}", $msg),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! info {
|
macro_rules! info {
|
||||||
($msg:tt) => {
|
($msg:tt) => {
|
||||||
match connected_to_journal() {
|
match connected_to_journal() {
|
||||||
|
@ -20,15 +25,6 @@ macro_rules! info {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! warn {
|
|
||||||
($msg:tt) => {
|
|
||||||
match connected_to_journal() {
|
|
||||||
true => log::warn!("[WARN] {}", $msg),
|
|
||||||
false => println!("[WARN] {}", $msg),
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! error {
|
macro_rules! error {
|
||||||
($msg:tt) => {
|
($msg:tt) => {
|
||||||
match connected_to_journal() {
|
match connected_to_journal() {
|
||||||
|
@ -38,226 +34,103 @@ macro_rules! error {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn run() {
|
pub(crate) struct Bot {
|
||||||
let mut last_reload: DateTime<Utc>;
|
shared_config: Arc<RwLock<Config>>,
|
||||||
let mut lemmy: Lemmy;
|
history: SeriesHistory,
|
||||||
let mut login_error: bool;
|
run_start_time: DateTime<Utc>
|
||||||
let mut communities: HashMap<String, CommunityId>;
|
}
|
||||||
let mut post_history: SeriesHistory;
|
|
||||||
let mut start: DateTime<Utc>;
|
|
||||||
let mut config: Config = Config::load();
|
|
||||||
last_reload = Utc::now();
|
|
||||||
|
|
||||||
lemmy = match lemmy::login(&config).await {
|
enum Wait {
|
||||||
Ok(data) => data,
|
Absolute,
|
||||||
Err(_) => panic!(),
|
Buffer
|
||||||
};
|
}
|
||||||
login_error = false;
|
|
||||||
|
|
||||||
communities = match lemmy.get_communities().await {
|
impl Bot {
|
||||||
Ok(data) => data,
|
pub(crate) fn new() -> Self {
|
||||||
Err(_) => panic!(),
|
let config = Config::load();
|
||||||
};
|
let shared_config: Arc<RwLock<Config>> = Arc::new(RwLock::new(config));
|
||||||
|
|
||||||
start = Utc::now();
|
let shared_config_copy = shared_config.clone();
|
||||||
|
let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| {
|
||||||
|
match res {
|
||||||
|
Ok(event) => {
|
||||||
|
if event.kind == EventKind::Access(AccessKind::Close(AccessMode::Write)) {
|
||||||
|
let mut write = shared_config_copy.write().expect("Write Lock Failed");
|
||||||
|
let new_config = Config::load();
|
||||||
|
write.series = new_config.series;
|
||||||
|
write.instance = new_config.instance;
|
||||||
|
write.protected_communities = new_config.protected_communities;
|
||||||
|
write.status_post_url = new_config.status_post_url;
|
||||||
|
info!("Reloaded Configuration");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
let msg = format!("Error watching files: {e}");
|
||||||
|
error!(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).expect("Watcher Error");
|
||||||
|
|
||||||
let info_msg = "Bot init successful, starting normal operations".to_owned();
|
watcher.watch(&Config::get_path(), RecursiveMode::NonRecursive).expect("Error in watcher");
|
||||||
info!(info_msg);
|
|
||||||
|
|
||||||
loop {
|
let history: SeriesHistory = SeriesHistory::load_history();
|
||||||
idle(&start, &config).await;
|
|
||||||
start = Utc::now();
|
|
||||||
|
|
||||||
// replace with watcher
|
Bot { shared_config, history, run_start_time: Utc::now() }
|
||||||
if start - last_reload >= Duration::seconds(config.config_reload_seconds as i64) {
|
}
|
||||||
config = Config::load();
|
pub(crate) async fn run(&mut self) {
|
||||||
let message = "Config reloaded".to_owned();
|
loop {
|
||||||
info!(message);
|
let mut lemmy = match Lemmy::new(&self.shared_config).await {
|
||||||
}
|
|
||||||
|
|
||||||
if login_error {
|
|
||||||
let info_msg = "Login invalid, refreshing session";
|
|
||||||
info!(info_msg);
|
|
||||||
lemmy = match lemmy::login(&config).await {
|
|
||||||
Ok(data) => data,
|
Ok(data) => data,
|
||||||
Err(_) => continue,
|
Err(_) => continue,
|
||||||
};
|
};
|
||||||
login_error = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if start - last_reload >= Duration::seconds(config.config_reload_seconds as i64) {
|
lemmy.get_communities().await;
|
||||||
communities = match lemmy.get_communities().await {
|
|
||||||
Ok(data) => data,
|
self.history = SeriesHistory::load_history();
|
||||||
Err(_) => {
|
|
||||||
login_error = true;
|
let start: DateTime<Utc> = Utc::now();
|
||||||
continue;
|
while Utc::now() - start <= Duration::minutes(60) {
|
||||||
|
self.run_start_time = Utc::now();
|
||||||
|
self.ping_status().await;
|
||||||
|
let read_copy = self.shared_config.read().expect("Read Lock Failed").clone();
|
||||||
|
for series in read_copy.series {
|
||||||
|
series.update(&mut self.history, &lemmy, &self.shared_config).await;
|
||||||
|
debug!("Done Updating Series");
|
||||||
|
self.wait(1, Wait::Absolute).await;
|
||||||
}
|
}
|
||||||
};
|
debug!("Awaiting Timeout");
|
||||||
let message = "Communities reloaded".to_owned();
|
self.wait(30, Wait::Buffer).await;
|
||||||
info!(message);
|
debug!("Pinging Server");
|
||||||
last_reload = Utc::now();
|
self.ping_status().await;
|
||||||
}
|
debug!("Awaiting Timeout 2");
|
||||||
|
self.wait(30, Wait::Absolute).await;
|
||||||
post_history = SeriesHistory::load_history();
|
|
||||||
|
|
||||||
let series = config.series.clone();
|
|
||||||
for series in series {
|
|
||||||
if handle_series(&series, &communities, &lemmy, &config, &mut post_history)
|
|
||||||
.await
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
login_error = true;
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
idle(&start, &config).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn idle(start: &DateTime<Utc>, config: &Config) {
|
|
||||||
let mut sleep_duration = Duration::seconds(30);
|
|
||||||
if Utc::now() - start > sleep_duration {
|
|
||||||
sleep_duration = Duration::seconds(60);
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(status_url) = config.status_post_url.clone() {
|
|
||||||
match reqwest::get(status_url).await {
|
|
||||||
Ok(_) => {}
|
|
||||||
Err(e) => {
|
|
||||||
let err_msg = format!("{e}");
|
|
||||||
error!(err_msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lemmy.logout().await;
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
while Utc::now() - start < sleep_duration {
|
|
||||||
sleep(Duration::milliseconds(100).to_std().unwrap()).await;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn handle_series(series: &SeriesConfig, communities: &HashMap<String, CommunityId>, lemmy: &Lemmy, config: &Config, post_history: &mut SeriesHistory ) -> Result<(), ()> {
|
|
||||||
let jnc = jnovel::JFetcherOptions::new(series.slug.clone(), series.parted);
|
|
||||||
let post_list = match jnc.check_feed().await {
|
|
||||||
Ok(data) => data,
|
|
||||||
Err(_) => return Err(()),
|
|
||||||
};
|
|
||||||
|
|
||||||
if post_list.is_empty() && Utc::now().minute() % 10 == 0 {
|
|
||||||
let info_msg = "No Updates found";
|
|
||||||
info!(info_msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for post_info in post_list.clone().iter() {
|
async fn ping_status(&self) {
|
||||||
let post_part_info = post_info.get_part_info();
|
let read_config = &self.shared_config.read().expect("Read Lock Failed").clone();
|
||||||
let post_lemmy_info = post_info.get_info();
|
if let Some(status_url) = &read_config.status_post_url {
|
||||||
|
match HTTP_CLIENT.get(status_url).send().await {
|
||||||
if post_history.check_for_post(
|
Ok(_) => {},
|
||||||
series.slug.as_str(),
|
Err(e) => {
|
||||||
post_part_info.as_string().as_str(),
|
let err_msg = format!("While pinging status URL: {e}");
|
||||||
post_lemmy_info.title.as_str(),
|
error!(err_msg);
|
||||||
) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let post_series_config = match post_info {
|
|
||||||
JPostInfo::Chapter { .. } => &series.prepub_community,
|
|
||||||
JPostInfo::Volume { .. } => &series.volume_community,
|
|
||||||
};
|
|
||||||
|
|
||||||
let community_id = *communities
|
|
||||||
.get(post_series_config.name.as_str())
|
|
||||||
.expect("Given community is invalid");
|
|
||||||
|
|
||||||
let post_body = match &post_series_config.post_body {
|
|
||||||
PostBody::None => None,
|
|
||||||
PostBody::Description => post_info.get_description(),
|
|
||||||
PostBody::Custom(text) => Some(text.clone()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let post_data = CreatePost {
|
|
||||||
name: post_lemmy_info.title.clone(),
|
|
||||||
community_id,
|
|
||||||
url: Some(post_lemmy_info.url),
|
|
||||||
body: post_body,
|
|
||||||
honeypot: None,
|
|
||||||
nsfw: None,
|
|
||||||
language_id: Some(LanguageId(37)), // TODO get this id once every few hours per API request, the ordering of IDs suggests that the EN Id might change in the future
|
|
||||||
};
|
|
||||||
|
|
||||||
let info = format!(
|
|
||||||
"Posting '{}' to {}",
|
|
||||||
post_lemmy_info.title.as_str(),
|
|
||||||
post_series_config.name.as_str()
|
|
||||||
);
|
|
||||||
info!(info);
|
|
||||||
let post_id = lemmy.post(post_data).await?;
|
|
||||||
|
|
||||||
if post_series_config.pin_settings.pin_new_post_community
|
|
||||||
&& config
|
|
||||||
.protected_communities
|
|
||||||
.contains(&post_series_config.name)
|
|
||||||
{
|
|
||||||
let info = format!(
|
|
||||||
"Pinning '{}' to {}",
|
|
||||||
post_lemmy_info.title,
|
|
||||||
post_series_config.name.as_str()
|
|
||||||
);
|
|
||||||
info!(info);
|
|
||||||
let pinned_posts = lemmy.get_community_pinned(community_id).await?;
|
|
||||||
if !pinned_posts.is_empty() {
|
|
||||||
let community_pinned_post = &pinned_posts[0];
|
|
||||||
lemmy
|
|
||||||
.unpin(community_pinned_post.post.id, PostFeatureType::Community)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
lemmy.pin(post_id, PostFeatureType::Community).await?;
|
|
||||||
} else if config
|
|
||||||
.protected_communities
|
|
||||||
.contains(&post_series_config.name)
|
|
||||||
{
|
|
||||||
let message = format!(
|
|
||||||
"Community '{}' for Series '{}' is protected. Is this intended?",
|
|
||||||
&post_series_config.name, series.slug
|
|
||||||
);
|
|
||||||
warn!(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
if post_series_config.pin_settings.pin_new_post_local {
|
|
||||||
let info = format!("Pinning '{}' to Instance", post_lemmy_info.title);
|
|
||||||
info!(info);
|
|
||||||
let pinned_posts = lemmy.get_local_pinned().await?;
|
|
||||||
if !pinned_posts.is_empty() {
|
|
||||||
for pinned_post in pinned_posts {
|
|
||||||
if config
|
|
||||||
.protected_communities
|
|
||||||
.contains(&pinned_post.community.name)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
} else {
|
|
||||||
let community_pinned_post = &pinned_post;
|
|
||||||
lemmy
|
|
||||||
.unpin(community_pinned_post.post.id, PostFeatureType::Local)
|
|
||||||
.await?;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lemmy.pin(post_id, PostFeatureType::Local).await?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut series_history = post_history.get_series(series.slug.as_str());
|
|
||||||
let mut part_history = series_history.get_part(post_part_info.as_string().as_str());
|
|
||||||
|
|
||||||
match post_info {
|
|
||||||
JPostInfo::Chapter { .. } => part_history.chapter = post_info.get_info().title,
|
|
||||||
JPostInfo::Volume { .. } => part_history.volume = post_info.get_info().title,
|
|
||||||
}
|
|
||||||
|
|
||||||
series_history.set_part(post_part_info.as_string().as_str(), part_history);
|
|
||||||
post_history
|
|
||||||
.set_series(series.slug.as_str(), series_history);
|
|
||||||
post_history.save_history();
|
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
|
async fn wait(&self, seconds: i64, start_time: Wait) {
|
||||||
|
let duration: Duration = Duration::seconds(seconds);
|
||||||
|
let start_time: DateTime<Utc> = match start_time {
|
||||||
|
Wait::Absolute => Utc::now(),
|
||||||
|
Wait::Buffer => self.run_start_time,
|
||||||
|
};
|
||||||
|
while Utc::now() - start_time < duration {
|
||||||
|
sleep(Duration::milliseconds(100).to_std().unwrap()).await
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
223
src/config.rs
223
src/config.rs
|
@ -1,12 +1,57 @@
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
use chrono::{Timelike, Utc};
|
||||||
use crate::config::PostBody::Description;
|
use crate::config::PostBody::Description;
|
||||||
use lemmy_api_common::sensitive::Sensitive;
|
use lemmy_db_schema::PostFeatureType;
|
||||||
|
use lemmy_db_schema::sensitive::SensitiveString;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
use crate::lemmy::{Lemmy, PartInfo, PostType};
|
||||||
|
use crate::post_history::{SeriesHistory};
|
||||||
|
use systemd_journal_logger::connected_to_journal;
|
||||||
|
use crate::fetchers::{FetcherTrait, Fetcher};
|
||||||
|
use crate::fetchers::jnovel::{JNovelFetcher};
|
||||||
|
|
||||||
|
macro_rules! debug {
|
||||||
|
($msg:tt) => {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => log::debug!("[DEBUG] {}", $msg),
|
||||||
|
false => println!("[DEBUG] {}", $msg),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! info {
|
||||||
|
($msg:tt) => {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => log::info!("[INFO] {}", $msg),
|
||||||
|
false => println!("[INFO] {}", $msg),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! warn {
|
||||||
|
($msg:tt) => {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => log::warn!("[WARN] {}", $msg),
|
||||||
|
false => println!("[WARN] {}", $msg),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! error {
|
||||||
|
($msg:tt) => {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => log::error!("[ERROR] {}", $msg),
|
||||||
|
false => eprintln!("[ERROR] {}", $msg),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
pub(crate) struct Config {
|
pub(crate) struct Config {
|
||||||
pub(crate) instance: String,
|
pub(crate) instance: String,
|
||||||
username: String,
|
username: SensitiveString,
|
||||||
password: String,
|
password: SensitiveString,
|
||||||
pub(crate) status_post_url: Option<String>,
|
pub(crate) status_post_url: Option<String>,
|
||||||
pub(crate) config_reload_seconds: u32,
|
pub(crate) config_reload_seconds: u32,
|
||||||
pub(crate) protected_communities: Vec<String>,
|
pub(crate) protected_communities: Vec<String>,
|
||||||
|
@ -40,12 +85,16 @@ impl Config {
|
||||||
cfg
|
cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_username(&self) -> Sensitive<String> {
|
pub(crate) fn get_path() -> PathBuf {
|
||||||
Sensitive::new(self.username.clone())
|
confy::get_configuration_file_path(env!("CARGO_PKG_NAME"), "config").expect("Application will not without confy")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_password(&self) -> Sensitive<String> {
|
pub(crate) fn get_username(&self) -> SensitiveString {
|
||||||
Sensitive::new(self.password.clone())
|
self.username.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_password(&self) -> SensitiveString {
|
||||||
|
self.password.clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,8 +102,8 @@ impl Default for Config {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Config {
|
Config {
|
||||||
instance: "".to_owned(),
|
instance: "".to_owned(),
|
||||||
username: "".to_owned(),
|
username: SensitiveString::from("".to_owned()),
|
||||||
password: "".to_owned(),
|
password: SensitiveString::from("".to_owned()),
|
||||||
status_post_url: None,
|
status_post_url: None,
|
||||||
config_reload_seconds: 21600,
|
config_reload_seconds: 21600,
|
||||||
protected_communities: vec![],
|
protected_communities: vec![],
|
||||||
|
@ -69,6 +118,162 @@ pub(crate) struct SeriesConfig {
|
||||||
pub(crate) parted: bool,
|
pub(crate) parted: bool,
|
||||||
pub(crate) prepub_community: PostConfig,
|
pub(crate) prepub_community: PostConfig,
|
||||||
pub(crate) volume_community: PostConfig,
|
pub(crate) volume_community: PostConfig,
|
||||||
|
pub(crate) fetcher: Fetcher
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SeriesConfig {
|
||||||
|
pub(crate) async fn update(&self, history: &mut SeriesHistory, lemmy: &Lemmy, config: &Arc<RwLock<Config>>) {
|
||||||
|
let info_msg = format!("Checking {} for Updates", self.slug);
|
||||||
|
info!(info_msg);
|
||||||
|
|
||||||
|
let mut fetcher: Fetcher = match &self.fetcher {
|
||||||
|
Fetcher::Jnc(_) => {
|
||||||
|
Fetcher::Jnc(JNovelFetcher::new())
|
||||||
|
},
|
||||||
|
/*default => {
|
||||||
|
let err_msg = format!("Fetcher {default} not implemented");
|
||||||
|
error!(err_msg);
|
||||||
|
return;
|
||||||
|
}*/
|
||||||
|
};
|
||||||
|
|
||||||
|
match fetcher {
|
||||||
|
Fetcher::Jnc(ref mut jnc) => {
|
||||||
|
jnc.set_series(self.slug.clone());
|
||||||
|
jnc.set_part_option(self.parted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let post_list = match fetcher.check_feed().await {
|
||||||
|
Ok(data) => data,
|
||||||
|
Err(_) => {
|
||||||
|
let err_msg = format!("While checking feed for {}", self.slug);
|
||||||
|
error!(err_msg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if post_list.is_empty() && Utc::now().minute() % 10 == 0 {
|
||||||
|
let info_msg = "No Updates found";
|
||||||
|
info!(info_msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
for post_info in post_list.iter() {
|
||||||
|
if history.check_for_post(
|
||||||
|
self.slug.as_str(),
|
||||||
|
post_info.get_part_info().unwrap_or(PartInfo::NoParts).as_string().as_str(),
|
||||||
|
post_info.get_info().title.as_str()
|
||||||
|
) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let post_data = post_info.get_post_data(self, lemmy);
|
||||||
|
|
||||||
|
let info = format!(
|
||||||
|
"Posting '{}' to {}",
|
||||||
|
post_info.get_info().title.as_str(),
|
||||||
|
post_info.get_post_config(self).name.as_str()
|
||||||
|
);
|
||||||
|
info!(info);
|
||||||
|
|
||||||
|
let post_id = match lemmy.post(post_data).await {
|
||||||
|
Some(data) => data,
|
||||||
|
None=> {
|
||||||
|
error!("Error posting chapter");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let read_config = config.read().expect("Read Lock Failed").clone();
|
||||||
|
|
||||||
|
if post_info.get_post_config(self).pin_settings.pin_new_post_community
|
||||||
|
&& !read_config
|
||||||
|
.protected_communities
|
||||||
|
.contains(&post_info.get_post_config(self).name)
|
||||||
|
{
|
||||||
|
let info = format!(
|
||||||
|
"Pinning '{}' to {}",
|
||||||
|
post_info.get_info().title,
|
||||||
|
post_info.get_post_config(self).name.as_str()
|
||||||
|
);
|
||||||
|
info!(info);
|
||||||
|
let pinned_posts = lemmy.get_community_pinned(lemmy.get_community_id(&post_info.get_post_config(self).name)).await.unwrap_or_else(|| {
|
||||||
|
error!("Pinning of Post to community failed");
|
||||||
|
vec![]
|
||||||
|
});
|
||||||
|
if !pinned_posts.is_empty() {
|
||||||
|
let community_pinned_post = &pinned_posts[0];
|
||||||
|
if lemmy.unpin(community_pinned_post.post.id, PostFeatureType::Community).await.is_none() {
|
||||||
|
error!("Error un-pinning post");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lemmy.pin(post_id, PostFeatureType::Community).await.is_none() {
|
||||||
|
error!("Error pinning post");
|
||||||
|
}
|
||||||
|
} else if read_config
|
||||||
|
.protected_communities
|
||||||
|
.contains(&post_info.get_post_config(self).name)
|
||||||
|
{
|
||||||
|
let message = format!(
|
||||||
|
"Community '{}' for Series '{}' is protected. Is this intended?",
|
||||||
|
&post_info.get_post_config(self).name, self.slug
|
||||||
|
);
|
||||||
|
warn!(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if post_info.get_post_config(self).pin_settings.pin_new_post_local {
|
||||||
|
let info = format!("Pinning '{}' to Instance", post_info.get_info().title);
|
||||||
|
info!(info);
|
||||||
|
let pinned_posts = match lemmy.get_local_pinned().await {
|
||||||
|
Some(data) => {data}
|
||||||
|
None => {
|
||||||
|
error!("Error fetching pinned posts");
|
||||||
|
vec![]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if !pinned_posts.is_empty() {
|
||||||
|
for pinned_post in pinned_posts {
|
||||||
|
if read_config
|
||||||
|
.protected_communities
|
||||||
|
.contains(&pinned_post.community.name)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
let community_pinned_post = &pinned_post;
|
||||||
|
if lemmy.unpin(community_pinned_post.post.id, PostFeatureType::Local).await.is_none() {
|
||||||
|
error!("Error pinning post");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if lemmy.pin(post_id, PostFeatureType::Local).await.is_none() {
|
||||||
|
error!("Error pinning post");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut series_history = history.get_series(self.slug.as_str());
|
||||||
|
let mut part_history = series_history.get_part(post_info.get_part_info().unwrap_or(PartInfo::NoParts).as_string().as_str());
|
||||||
|
|
||||||
|
match post_info.post_type {
|
||||||
|
Some(post_type) => {
|
||||||
|
match post_type {
|
||||||
|
PostType::Chapter => part_history.chapter = post_info.get_info().title,
|
||||||
|
PostType::Volume => part_history.volume = post_info.get_info().title,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => part_history.chapter = post_info.get_info().title,
|
||||||
|
}
|
||||||
|
|
||||||
|
series_history.set_part(post_info.get_part_info().unwrap_or(PartInfo::NoParts).as_string().as_str(), part_history);
|
||||||
|
history
|
||||||
|
.set_series(self.slug.as_str(), series_history);
|
||||||
|
debug!("Saving History");
|
||||||
|
history.save_history();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||||
|
|
|
@ -1,16 +1,13 @@
|
||||||
use crate::{HTTP_CLIENT, lemmy};
|
use crate::{HTTP_CLIENT};
|
||||||
use chrono::{DateTime, Duration, Utc};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use std::cmp::Ordering;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::ops::Sub;
|
use std::ops::Sub;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use url::Url;
|
use crate::fetchers::{FetcherTrait};
|
||||||
use crate::fetchers::Fetcher;
|
use crate::lemmy::{PartInfo, PostInfo, PostInfoInner, PostType};
|
||||||
use crate::fetchers::jnovel::JPostInfo::{Chapter, Volume};
|
|
||||||
use crate::fetchers::jnovel::PartInfo::{NoParts, Part};
|
|
||||||
use crate::lemmy::{PostInfo, PostInfoInner};
|
|
||||||
use systemd_journal_logger::connected_to_journal;
|
use systemd_journal_logger::connected_to_journal;
|
||||||
|
use crate::lemmy::PartInfo::{NoParts, Part};
|
||||||
|
|
||||||
macro_rules! error {
|
macro_rules! error {
|
||||||
($msg:tt) => {
|
($msg:tt) => {
|
||||||
|
@ -34,7 +31,7 @@ static PAST_DAYS_ELIGIBLE: u8 = 4;
|
||||||
|
|
||||||
macro_rules! api_url {
|
macro_rules! api_url {
|
||||||
() => {
|
() => {
|
||||||
"https://labs.j-novel.club/app/v1".to_owned()
|
"https://labs.j-novel.club/app/v2".to_owned()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,191 +88,41 @@ pub(crate) struct ChapterDetail {
|
||||||
pub(crate) cover: Option<Cover>,
|
pub(crate) cover: Option<Cover>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||||
pub(crate) enum PartInfo {
|
pub(crate) struct JNovelFetcher {
|
||||||
NoParts,
|
|
||||||
Part(u8),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartInfo {
|
|
||||||
pub(crate) fn as_u8(&self) -> u8 {
|
|
||||||
match self {
|
|
||||||
Part(number) => *number,
|
|
||||||
NoParts => 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn as_string(&self) -> String {
|
|
||||||
self.as_u8().to_string()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for PartInfo {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
let self_numeric = self.as_u8();
|
|
||||||
let other_numeric = other.as_u8();
|
|
||||||
self_numeric == other_numeric
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for PartInfo {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
if self.gt(other) {
|
|
||||||
Some(Ordering::Greater)
|
|
||||||
} else if self.eq(other) {
|
|
||||||
Some(Ordering::Equal)
|
|
||||||
} else {
|
|
||||||
Some(Ordering::Less)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lt(&self, other: &Self) -> bool {
|
|
||||||
let self_numeric = self.as_u8();
|
|
||||||
let other_numeric = other.as_u8();
|
|
||||||
|
|
||||||
self_numeric < other_numeric
|
|
||||||
}
|
|
||||||
|
|
||||||
fn le(&self, other: &Self) -> bool {
|
|
||||||
!self.gt(other)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn gt(&self, other: &Self) -> bool {
|
|
||||||
let self_numeric = self.as_u8();
|
|
||||||
let other_numeric = other.as_u8();
|
|
||||||
|
|
||||||
self_numeric > other_numeric
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ge(&self, other: &Self) -> bool {
|
|
||||||
!self.lt(other)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
pub(crate) enum JPostInfo {
|
|
||||||
Chapter {
|
|
||||||
part: PartInfo,
|
|
||||||
lemmy_info: PostInfoInner,
|
|
||||||
},
|
|
||||||
Volume {
|
|
||||||
part: PartInfo,
|
|
||||||
description: String,
|
|
||||||
lemmy_info: PostInfoInner,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
impl JPostInfo {
|
|
||||||
pub(crate) fn get_part_info(&self) -> PartInfo {
|
|
||||||
match self {
|
|
||||||
Chapter {
|
|
||||||
part: part_info, ..
|
|
||||||
} => *part_info,
|
|
||||||
Volume {
|
|
||||||
part: part_info, ..
|
|
||||||
} => *part_info,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PostInfo for JPostInfo {
|
|
||||||
fn get_info(&self) -> PostInfoInner {
|
|
||||||
match self {
|
|
||||||
Chapter { lemmy_info, .. } => lemmy_info.clone(),
|
|
||||||
Volume { lemmy_info, .. } => lemmy_info.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_description(&self) -> Option<String> {
|
|
||||||
match self {
|
|
||||||
Chapter { .. } => None,
|
|
||||||
Volume { description, .. } => Some(description.clone()),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialEq for JPostInfo {
|
|
||||||
fn eq(&self, other: &Self) -> bool {
|
|
||||||
let self_part = match self {
|
|
||||||
Chapter { part, .. } => part,
|
|
||||||
Volume { part, .. } => part,
|
|
||||||
};
|
|
||||||
|
|
||||||
let other_part = match other {
|
|
||||||
Chapter { part, .. } => part,
|
|
||||||
Volume { part, .. } => part,
|
|
||||||
};
|
|
||||||
|
|
||||||
self_part.eq(other_part)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PartialOrd for JPostInfo {
|
|
||||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
|
||||||
if self.gt(other) {
|
|
||||||
Some(Ordering::Greater)
|
|
||||||
} else if self.eq(other) {
|
|
||||||
Some(Ordering::Equal)
|
|
||||||
} else {
|
|
||||||
Some(Ordering::Less)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn lt(&self, other: &Self) -> bool {
|
|
||||||
let self_part = match self {
|
|
||||||
Chapter { part, .. } => part,
|
|
||||||
Volume { part, .. } => part,
|
|
||||||
};
|
|
||||||
|
|
||||||
let other_part = match other {
|
|
||||||
Chapter { part, .. } => part,
|
|
||||||
Volume { part, .. } => part,
|
|
||||||
};
|
|
||||||
|
|
||||||
self_part < other_part
|
|
||||||
}
|
|
||||||
|
|
||||||
fn le(&self, other: &Self) -> bool {
|
|
||||||
!self.gt(other)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn gt(&self, other: &Self) -> bool {
|
|
||||||
let self_part = match self {
|
|
||||||
Chapter { part, .. } => part,
|
|
||||||
Volume { part, .. } => part,
|
|
||||||
};
|
|
||||||
|
|
||||||
let other_part = match other {
|
|
||||||
Chapter { part, .. } => part,
|
|
||||||
Volume { part, .. } => part,
|
|
||||||
};
|
|
||||||
|
|
||||||
self_part > other_part
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ge(&self, other: &Self) -> bool {
|
|
||||||
!self.lt(other)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct JFetcherOptions {
|
|
||||||
series_slug: String,
|
series_slug: String,
|
||||||
series_has_parts: bool
|
series_has_parts: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JFetcherOptions {
|
impl Default for JNovelFetcher {
|
||||||
pub(crate) fn new(series_slug: String, series_has_parts: bool) -> Self {
|
fn default() -> Self {
|
||||||
JFetcherOptions {
|
Self {
|
||||||
series_slug,
|
series_slug: "".to_owned(),
|
||||||
series_has_parts
|
series_has_parts: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl JNovelFetcher {
|
||||||
|
pub(crate) fn set_series(&mut self, series: String) {
|
||||||
|
self.series_slug = series;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn set_part_option(&mut self, has_parts: bool) {
|
||||||
|
self.series_has_parts = has_parts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Fetcher for JFetcherOptions {
|
impl FetcherTrait for JNovelFetcher {
|
||||||
type Return = JPostInfo;
|
fn new() -> Self {
|
||||||
async fn check_feed(&self) -> Result<Vec<Self::Return>, ()> {
|
JNovelFetcher {
|
||||||
|
series_slug: "".to_owned(),
|
||||||
|
series_has_parts: false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn check_feed(&self) -> Result<Vec<PostInfo>, ()> {
|
||||||
let response = match HTTP_CLIENT
|
let response = match HTTP_CLIENT
|
||||||
.get(api_url!() + "/series/" + self.series_slug.as_str() + "/volumes?format=json")
|
.get(api_url!() + "/series/" + self.series_slug.as_str() + "/volumes?format=json")
|
||||||
.send()
|
.send()
|
||||||
|
@ -284,7 +131,7 @@ impl Fetcher for JFetcherOptions {
|
||||||
Ok(data) => match data.text().await {
|
Ok(data) => match data.text().await {
|
||||||
Ok(data) => data,
|
Ok(data) => data,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let err_msg = format!("{e}");
|
let err_msg = format!("While checking feed: {e}");
|
||||||
error!(err_msg);
|
error!(err_msg);
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
|
@ -307,8 +154,8 @@ impl Fetcher for JFetcherOptions {
|
||||||
volume_brief_data.volumes.reverse(); // Makes breaking out of the volume loop easier
|
volume_brief_data.volumes.reverse(); // Makes breaking out of the volume loop easier
|
||||||
|
|
||||||
// If no parts just use 0 as Part indicator as no Series with Parts has a Part 0
|
// If no parts just use 0 as Part indicator as no Series with Parts has a Part 0
|
||||||
let mut volume_map: HashMap<u8, JPostInfo> = HashMap::new();
|
let mut volume_map: HashMap<u8, PostInfo> = HashMap::new();
|
||||||
let mut prepub_map: HashMap<u8, JPostInfo> = HashMap::new();
|
let mut prepub_map: HashMap<u8, PostInfo> = HashMap::new();
|
||||||
|
|
||||||
for volume in volume_brief_data.volumes.iter() {
|
for volume in volume_brief_data.volumes.iter() {
|
||||||
let publishing_date = DateTime::parse_from_rfc3339(&volume.publishing).unwrap();
|
let publishing_date = DateTime::parse_from_rfc3339(&volume.publishing).unwrap();
|
||||||
|
@ -351,14 +198,16 @@ impl Fetcher for JFetcherOptions {
|
||||||
self.series_slug.as_str(),
|
self.series_slug.as_str(),
|
||||||
volume.number
|
volume.number
|
||||||
);
|
);
|
||||||
let post_details = lemmy::PostInfoInner {
|
let post_details = PostInfoInner {
|
||||||
title: volume.title.clone(),
|
title: volume.title.clone(),
|
||||||
url: Url::parse(&post_url).unwrap(),
|
url: post_url.clone(),
|
||||||
|
thumbnail: Some(volume.cover.thumbnail.clone())
|
||||||
};
|
};
|
||||||
|
|
||||||
let new_post_info = Volume {
|
let new_post_info = PostInfo {
|
||||||
part: new_part_info,
|
post_type: Some(PostType::Volume),
|
||||||
description: volume.short_description.clone(),
|
part: Some(new_part_info),
|
||||||
|
description: Some(volume.short_description.clone()),
|
||||||
lemmy_info: post_details,
|
lemmy_info: post_details,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -375,10 +224,12 @@ impl Fetcher for JFetcherOptions {
|
||||||
.or_insert(new_post_info);
|
.or_insert(new_post_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Some(prepub_info) = get_latest_prepub(&volume.slug).await? {
|
if let Some(prepub_info) = get_latest_prepub(&volume.slug).await {
|
||||||
let prepub_post_info = Chapter {
|
let prepub_post_info = PostInfo {
|
||||||
part: new_part_info,
|
post_type: Some(PostType::Chapter),
|
||||||
|
part: Some(new_part_info),
|
||||||
lemmy_info: prepub_info,
|
lemmy_info: prepub_info,
|
||||||
|
description: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
prepub_map
|
prepub_map
|
||||||
|
@ -392,8 +243,8 @@ impl Fetcher for JFetcherOptions {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut result_vec: Vec<JPostInfo> = volume_map.values().cloned().collect();
|
let mut result_vec: Vec<PostInfo> = volume_map.values().cloned().collect();
|
||||||
let mut prepub_vec: Vec<JPostInfo> = prepub_map.values().cloned().collect();
|
let mut prepub_vec: Vec<PostInfo> = prepub_map.values().cloned().collect();
|
||||||
result_vec.append(&mut prepub_vec);
|
result_vec.append(&mut prepub_vec);
|
||||||
|
|
||||||
Ok(result_vec)
|
Ok(result_vec)
|
||||||
|
@ -401,7 +252,7 @@ impl Fetcher for JFetcherOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async fn get_latest_prepub(volume_slug: &str) -> Result<Option<lemmy::PostInfoInner>, ()> {
|
async fn get_latest_prepub(volume_slug: &str) -> Option<PostInfoInner> {
|
||||||
let response = match HTTP_CLIENT
|
let response = match HTTP_CLIENT
|
||||||
.get(api_url!() + "/volumes/" + volume_slug + "/parts?format=json")
|
.get(api_url!() + "/volumes/" + volume_slug + "/parts?format=json")
|
||||||
.send()
|
.send()
|
||||||
|
@ -410,15 +261,15 @@ async fn get_latest_prepub(volume_slug: &str) -> Result<Option<lemmy::PostInfoIn
|
||||||
Ok(data) => match data.text().await {
|
Ok(data) => match data.text().await {
|
||||||
Ok(data) => data,
|
Ok(data) => data,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let err_msg = format!("{e}");
|
let err_msg = format!("While getting latest PrePub: {e}");
|
||||||
error!(err_msg);
|
error!(err_msg);
|
||||||
return Err(());
|
return None;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let err_msg = format!("{e}");
|
let err_msg = format!("{e}");
|
||||||
error!(err_msg);
|
error!(err_msg);
|
||||||
return Err(());
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -427,12 +278,12 @@ async fn get_latest_prepub(volume_slug: &str) -> Result<Option<lemmy::PostInfoIn
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let err_msg = format!("{e}");
|
let err_msg = format!("{e}");
|
||||||
error!(err_msg);
|
error!(err_msg);
|
||||||
return Err(());
|
return None;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
volume_prepub_parts_data.parts.reverse(); // Makes breaking out of the parts loop easier
|
volume_prepub_parts_data.parts.reverse(); // Makes breaking out of the parts loop easier
|
||||||
|
|
||||||
let mut post_details: Option<lemmy::PostInfoInner> = None;
|
let mut post_details: Option<PostInfoInner> = None;
|
||||||
|
|
||||||
for prepub_part in volume_prepub_parts_data.parts.iter() {
|
for prepub_part in volume_prepub_parts_data.parts.iter() {
|
||||||
let publishing_date = DateTime::parse_from_rfc3339(&prepub_part.launch).unwrap();
|
let publishing_date = DateTime::parse_from_rfc3339(&prepub_part.launch).unwrap();
|
||||||
|
@ -442,12 +293,15 @@ async fn get_latest_prepub(volume_slug: &str) -> Result<Option<lemmy::PostInfoIn
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let thumbnail = prepub_part.cover.as_ref().map(|cover| cover.thumbnail.clone());
|
||||||
|
|
||||||
let post_url = format!("{}/read/{}", jnc_base_url!(), prepub_part.slug);
|
let post_url = format!("{}/read/{}", jnc_base_url!(), prepub_part.slug);
|
||||||
post_details = Some(lemmy::PostInfoInner {
|
post_details = Some(PostInfoInner {
|
||||||
title: prepub_part.title.clone(),
|
title: prepub_part.title.clone(),
|
||||||
url: Url::parse(&post_url).unwrap(),
|
url: post_url.clone(),
|
||||||
|
thumbnail
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(post_details)
|
post_details
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,33 @@
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use serde_derive::{Deserialize, Serialize};
|
||||||
|
use strum_macros::Display;
|
||||||
|
use crate::fetchers::Fetcher::Jnc;
|
||||||
|
use crate::fetchers::jnovel::JNovelFetcher;
|
||||||
|
use crate::lemmy::{PostInfo};
|
||||||
|
|
||||||
pub mod jnovel;
|
pub mod jnovel;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub(crate) trait Fetcher {
|
pub(crate) trait FetcherTrait {
|
||||||
type Return;
|
fn new() -> Self where Self: Sized;
|
||||||
async fn check_feed(&self) -> Result<Vec<Self::Return>, ()>;
|
async fn check_feed(&self) -> Result<Vec<PostInfo>, ()>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Fetcher {
|
||||||
|
pub(crate) async fn check_feed(&self) -> Result<Vec<PostInfo>, ()> {
|
||||||
|
match self {
|
||||||
|
Jnc(fetcher) => fetcher.check_feed().await,
|
||||||
|
/*default => {
|
||||||
|
let err_msg = format!("Fetcher {default} is not implemented");
|
||||||
|
error!(err_msg);
|
||||||
|
Err(())
|
||||||
|
}*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize, Debug, Clone, Display)]
|
||||||
|
pub(crate) enum Fetcher {
|
||||||
|
#[serde(rename = "jnc")]
|
||||||
|
Jnc(#[serde(skip)] JNovelFetcher)
|
||||||
}
|
}
|
||||||
|
|
396
src/lemmy.rs
396
src/lemmy.rs
|
@ -1,18 +1,28 @@
|
||||||
use crate::config::Config;
|
use std::cmp::Ordering;
|
||||||
|
use crate::config::{Config, PostBody, PostConfig, SeriesConfig};
|
||||||
use crate::{HTTP_CLIENT};
|
use crate::{HTTP_CLIENT};
|
||||||
use lemmy_api_common::community::{ListCommunities, ListCommunitiesResponse};
|
use lemmy_api_common::community::{ListCommunities, ListCommunitiesResponse};
|
||||||
use lemmy_api_common::lemmy_db_views::structs::PostView;
|
use lemmy_api_common::lemmy_db_views::structs::PostView;
|
||||||
use lemmy_api_common::person::{Login, LoginResponse};
|
use lemmy_api_common::person::{Login, LoginResponse};
|
||||||
use lemmy_api_common::post::{CreatePost, FeaturePost, GetPosts, GetPostsResponse};
|
use lemmy_api_common::post::{CreatePost, FeaturePost, GetPosts, GetPostsResponse};
|
||||||
use lemmy_api_common::sensitive::Sensitive;
|
use lemmy_db_schema::newtypes::{CommunityId, LanguageId, PostId};
|
||||||
use lemmy_db_schema::newtypes::{CommunityId, PostId};
|
|
||||||
use lemmy_db_schema::{ListingType, PostFeatureType};
|
use lemmy_db_schema::{ListingType, PostFeatureType};
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::{RwLock};
|
||||||
|
use lemmy_db_schema::sensitive::SensitiveString;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use url::Url;
|
|
||||||
use systemd_journal_logger::connected_to_journal;
|
use systemd_journal_logger::connected_to_journal;
|
||||||
|
|
||||||
|
macro_rules! debug {
|
||||||
|
($msg:tt) => {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => log::debug!("[DEBUG] {}", $msg),
|
||||||
|
false => println!("[DEBUG] {}", $msg),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! error {
|
macro_rules! error {
|
||||||
($msg:tt) => {
|
($msg:tt) => {
|
||||||
match connected_to_journal() {
|
match connected_to_journal() {
|
||||||
|
@ -23,86 +33,263 @@ macro_rules! error {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Lemmy {
|
pub(crate) struct Lemmy {
|
||||||
jwt_token: Sensitive<String>,
|
jwt_token: SensitiveString,
|
||||||
instance: String,
|
instance: String,
|
||||||
|
communities: HashMap<String, CommunityId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub(crate) struct PostInfoInner {
|
pub(crate) struct PostInfoInner {
|
||||||
pub(crate) title: String,
|
pub(crate) title: String,
|
||||||
pub(crate) url: Url,
|
pub(crate) url: String,
|
||||||
|
pub(crate) thumbnail: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) trait PostInfo {
|
#[derive(Debug, Copy, Clone)]
|
||||||
fn get_info(&self) -> PostInfoInner;
|
pub(crate) enum PartInfo {
|
||||||
|
NoParts,
|
||||||
fn get_description(&self) -> Option<String>;
|
Part(u8),
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn login(config: &Config) -> Result<Lemmy, ()> {
|
impl PartInfo {
|
||||||
let login_params = Login {
|
pub(crate) fn as_u8(&self) -> u8 {
|
||||||
username_or_email: config.get_username(),
|
match self {
|
||||||
password: config.get_password(),
|
PartInfo::Part(number) => *number,
|
||||||
totp_2fa_token: None,
|
PartInfo::NoParts => 0,
|
||||||
};
|
|
||||||
|
|
||||||
let response = match HTTP_CLIENT
|
|
||||||
.post(config.instance.to_owned() + "/api/v3/user/login")
|
|
||||||
.json(&login_params)
|
|
||||||
.send()
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Ok(data) => data,
|
|
||||||
Err(e) => {
|
|
||||||
let err_msg = format!("{e}");
|
|
||||||
error!(err_msg);
|
|
||||||
return Err(());
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
match response.status() {
|
pub(crate) fn as_string(&self) -> String {
|
||||||
StatusCode::OK => {
|
self.as_u8().to_string()
|
||||||
let data: LoginResponse = response
|
}
|
||||||
.json()
|
}
|
||||||
.await
|
|
||||||
.expect("Successful Login Request should return JSON");
|
impl PartialEq for PartInfo {
|
||||||
match data.jwt {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
Some(token) => Ok(Lemmy {
|
let self_numeric = self.as_u8();
|
||||||
jwt_token: token.clone(),
|
let other_numeric = other.as_u8();
|
||||||
instance: config.instance.to_owned(),
|
self_numeric == other_numeric
|
||||||
}),
|
}
|
||||||
None => {
|
}
|
||||||
let err_msg = "Login did not return JWT token. Are the credentials valid?".to_owned();
|
|
||||||
error!(err_msg);
|
impl PartialOrd for PartInfo {
|
||||||
Err(())
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
if self.gt(other) {
|
||||||
|
Some(Ordering::Greater)
|
||||||
|
} else if self.eq(other) {
|
||||||
|
Some(Ordering::Equal)
|
||||||
|
} else {
|
||||||
|
Some(Ordering::Less)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lt(&self, other: &Self) -> bool {
|
||||||
|
let self_numeric = self.as_u8();
|
||||||
|
let other_numeric = other.as_u8();
|
||||||
|
|
||||||
|
self_numeric < other_numeric
|
||||||
|
}
|
||||||
|
|
||||||
|
fn le(&self, other: &Self) -> bool {
|
||||||
|
!self.gt(other)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gt(&self, other: &Self) -> bool {
|
||||||
|
let self_numeric = self.as_u8();
|
||||||
|
let other_numeric = other.as_u8();
|
||||||
|
|
||||||
|
self_numeric > other_numeric
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ge(&self, other: &Self) -> bool {
|
||||||
|
!self.lt(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub(crate) enum PostType {
|
||||||
|
Chapter,
|
||||||
|
Volume
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub(crate) struct PostInfo {
|
||||||
|
pub(crate) part: Option<PartInfo>,
|
||||||
|
pub(crate) lemmy_info: PostInfoInner,
|
||||||
|
pub(crate) description: Option<String>,
|
||||||
|
pub(crate) post_type: Option<PostType>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PostInfo {
|
||||||
|
pub(crate)fn get_info(&self) -> PostInfoInner {
|
||||||
|
self.lemmy_info.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate)fn get_description(&self) -> Option<String> {
|
||||||
|
self.description.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_part_info(&self) -> Option<PartInfo> {
|
||||||
|
self.part
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn get_post_config(&self, series: &SeriesConfig) -> PostConfig {
|
||||||
|
match self.post_type {
|
||||||
|
Some(post_type) => {
|
||||||
|
match post_type {
|
||||||
|
PostType::Chapter => series.prepub_community.clone(),
|
||||||
|
PostType::Volume => series.volume_community.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
None => series.prepub_community.clone(),
|
||||||
}
|
}
|
||||||
status => {
|
}
|
||||||
let err_msg = format!("Unexpected HTTP Status '{}' during Login", status);
|
|
||||||
error!(err_msg);
|
pub(crate) fn get_post_data(&self, series: &SeriesConfig, lemmy: &Lemmy) -> CreatePost {
|
||||||
Err(())
|
let post_config = self.get_post_config(series);
|
||||||
|
|
||||||
|
let post_body = match &post_config.post_body {
|
||||||
|
PostBody::None => None,
|
||||||
|
PostBody::Description => self.get_description(),
|
||||||
|
PostBody::Custom(text) => Some(text.clone()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let community_id: CommunityId = lemmy.get_community_id(&post_config.name);
|
||||||
|
|
||||||
|
CreatePost {
|
||||||
|
name: self.get_info().title.clone(),
|
||||||
|
community_id,
|
||||||
|
url: Some(self.get_info().url),
|
||||||
|
custom_thumbnail: self.get_info().thumbnail,
|
||||||
|
body: post_body,
|
||||||
|
alt_text: None,
|
||||||
|
honeypot: None,
|
||||||
|
nsfw: None,
|
||||||
|
language_id: Some(LanguageId(37)), // TODO get this id once every few hours per API request, the ordering of IDs suggests that the EN Id might change in the future
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq for PostInfo {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.part.eq(&other.part)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd for PostInfo {
|
||||||
|
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||||
|
if self.gt(other) {
|
||||||
|
Some(Ordering::Greater)
|
||||||
|
} else if self.eq(other) {
|
||||||
|
Some(Ordering::Equal)
|
||||||
|
} else {
|
||||||
|
Some(Ordering::Less)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lt(&self, other: &Self) -> bool {
|
||||||
|
self.part < other.part
|
||||||
|
}
|
||||||
|
|
||||||
|
fn le(&self, other: &Self) -> bool {
|
||||||
|
!self.gt(other)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn gt(&self, other: &Self) -> bool {
|
||||||
|
self.part > other.part
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ge(&self, other: &Self) -> bool {
|
||||||
|
!self.lt(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Lemmy {
|
impl Lemmy {
|
||||||
pub(crate) async fn post(&self, post: CreatePost) -> Result<PostId, ()> {
|
pub(crate) fn get_community_id(&self, name: &str) -> CommunityId {
|
||||||
let response: String = self.post_data_json("/api/v3/post", &post).await?;
|
*self.communities.get(name).expect("Given community is invalid")
|
||||||
let json_data: PostView = self.parse_json(&response).await?;
|
}
|
||||||
|
pub(crate) async fn new(config: &RwLock<Config>) -> Result<Self, ()> {
|
||||||
|
let read_config = config.read().expect("Read Lock Failed").clone();
|
||||||
|
let login_params = Login {
|
||||||
|
username_or_email: read_config.get_username(),
|
||||||
|
password: read_config.get_password(),
|
||||||
|
totp_2fa_token: None,
|
||||||
|
};
|
||||||
|
|
||||||
Ok(json_data.post.id)
|
let response = match HTTP_CLIENT
|
||||||
|
.post(read_config.instance.to_owned() + "/api/v3/user/login")
|
||||||
|
.json(&login_params)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(data) => data,
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = format!("{e}");
|
||||||
|
error!(err_msg);
|
||||||
|
return Err(());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match response.status() {
|
||||||
|
StatusCode::OK => {
|
||||||
|
let data: LoginResponse = response
|
||||||
|
.json()
|
||||||
|
.await
|
||||||
|
.expect("Successful Login Request should return JSON");
|
||||||
|
match data.jwt {
|
||||||
|
Some(token) => Ok(Lemmy {
|
||||||
|
jwt_token: token.clone(),
|
||||||
|
instance: read_config.instance.to_owned(),
|
||||||
|
communities: HashMap::new(),
|
||||||
|
}),
|
||||||
|
None => {
|
||||||
|
let err_msg = "Login did not return JWT token. Are the credentials valid?".to_owned();
|
||||||
|
error!(err_msg);
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
status => {
|
||||||
|
let err_msg = format!("Unexpected HTTP Status '{}' during Login", status);
|
||||||
|
error!(err_msg);
|
||||||
|
Err(())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn feature(&self, params: FeaturePost) -> Result<PostView, ()> {
|
pub(crate) async fn logout(&self) {
|
||||||
let response: String = self.post_data_json("/api/v3/post/feature", ¶ms).await?;
|
let _ = self.post_data_json("/api/v3/user/logout", &"").await;
|
||||||
let json_data: PostView = self.parse_json(&response).await?;
|
|
||||||
|
|
||||||
Ok(json_data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
|
|
||||||
|
pub(crate) async fn post(&self, post: CreatePost) -> Option<PostId> {
|
||||||
|
let response: String = match self.post_data_json("/api/v3/post", &post).await {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
let json_data: PostView = match self.parse_json_map(&response).await {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(json_data.post.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn feature(&self, params: FeaturePost) -> Option<PostView> {
|
||||||
|
let response: String = match self.post_data_json("/api/v3/post/feature", ¶ms).await {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
let json_data: PostView = match self.parse_json_map(&response).await {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
Some(json_data)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) async fn unpin(&self, post_id: PostId, location: PostFeatureType) -> Option<PostView> {
|
||||||
let pin_params = FeaturePost {
|
let pin_params = FeaturePost {
|
||||||
post_id,
|
post_id,
|
||||||
featured: false,
|
featured: false,
|
||||||
|
@ -111,7 +298,7 @@ impl Lemmy {
|
||||||
self.feature(pin_params).await
|
self.feature(pin_params).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Result<PostView, ()> {
|
pub(crate) async fn pin(&self, post_id: PostId, location: PostFeatureType) -> Option<PostView> {
|
||||||
let pin_params = FeaturePost {
|
let pin_params = FeaturePost {
|
||||||
post_id,
|
post_id,
|
||||||
featured: true,
|
featured: true,
|
||||||
|
@ -120,17 +307,23 @@ impl Lemmy {
|
||||||
self.feature(pin_params).await
|
self.feature(pin_params).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Result<Vec<PostView>, ()> {
|
pub(crate) async fn get_community_pinned(&self, community: CommunityId) -> Option<Vec<PostView>> {
|
||||||
let list_params = GetPosts {
|
let list_params = GetPosts {
|
||||||
community_id: Some(community),
|
community_id: Some(community),
|
||||||
type_: Some(ListingType::Local),
|
type_: Some(ListingType::Local),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let response: String = self.get_data_query("/api/v3/post/list", &list_params).await?;
|
let response: String = match self.get_data_query("/api/v3/post/list", &list_params).await {
|
||||||
let json_data: GetPostsResponse = self.parse_json(&response).await?;
|
Some(data) => data,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
let json_data: GetPostsResponse = match self.parse_json(&response).await {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
Ok(json_data
|
Some(json_data
|
||||||
.posts
|
.posts
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|post| post.post.featured_community)
|
.filter(|post| post.post.featured_community)
|
||||||
|
@ -138,16 +331,22 @@ impl Lemmy {
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn get_local_pinned(&self) -> Result<Vec<PostView>, ()> {
|
pub(crate) async fn get_local_pinned(&self) -> Option<Vec<PostView>> {
|
||||||
let list_params = GetPosts {
|
let list_params = GetPosts {
|
||||||
type_: Some(ListingType::Local),
|
type_: Some(ListingType::Local),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let response: String = self.get_data_query("/api/v3/post/list", &list_params).await?;
|
let response: String = match self.get_data_query("/api/v3/post/list", &list_params).await {
|
||||||
let json_data: GetPostsResponse = self.parse_json(&response).await?;
|
Some(data) => data,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
let json_data: GetPostsResponse = match self.parse_json(&response).await {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
Ok(json_data
|
Some(json_data
|
||||||
.posts
|
.posts
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|post| post.post.featured_local)
|
.filter(|post| post.post.featured_local)
|
||||||
|
@ -155,14 +354,20 @@ impl Lemmy {
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) async fn get_communities(&self) -> Result<HashMap<String, CommunityId>, ()> {
|
pub(crate) async fn get_communities(&mut self) {
|
||||||
let list_params = ListCommunities {
|
let list_params = ListCommunities {
|
||||||
type_: Some(ListingType::Local),
|
type_: Some(ListingType::Local),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
let response: String = self.get_data_query("/api/v3/community/list", &list_params).await?;
|
let response: String = match self.get_data_query("/api/v3/community/list", &list_params).await {
|
||||||
let json_data: ListCommunitiesResponse = self.parse_json(&response).await?;
|
Some(data) => data,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
let json_data: ListCommunitiesResponse = match self.parse_json::<ListCommunitiesResponse>(&response).await {
|
||||||
|
Some(data) => data,
|
||||||
|
None => return,
|
||||||
|
};
|
||||||
|
|
||||||
let mut communities: HashMap<String, CommunityId> = HashMap::new();
|
let mut communities: HashMap<String, CommunityId> = HashMap::new();
|
||||||
for community_view in json_data.communities {
|
for community_view in json_data.communities {
|
||||||
|
@ -170,10 +375,10 @@ impl Lemmy {
|
||||||
communities.insert(community.name, community.id);
|
communities.insert(community.name, community.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(communities)
|
self.communities = communities;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn post_data_json<T: Serialize>(&self, route: &str, json: &T ) -> Result<String,()> {
|
async fn post_data_json<T: Serialize>(&self, route: &str, json: &T ) -> Option<String> {
|
||||||
let res = HTTP_CLIENT
|
let res = HTTP_CLIENT
|
||||||
.post(format!("{}{route}", &self.instance))
|
.post(format!("{}{route}", &self.instance))
|
||||||
.bearer_auth(&self.jwt_token.to_string())
|
.bearer_auth(&self.jwt_token.to_string())
|
||||||
|
@ -183,7 +388,7 @@ impl Lemmy {
|
||||||
self.extract_data(res).await
|
self.extract_data(res).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_data_query<T: Serialize>(&self, route: &str, param: &T ) -> Result<String,()> {
|
async fn get_data_query<T: Serialize>(&self, route: &str, param: &T ) -> Option<String> {
|
||||||
let res = HTTP_CLIENT
|
let res = HTTP_CLIENT
|
||||||
.get(format!("{}{route}", &self.instance))
|
.get(format!("{}{route}", &self.instance))
|
||||||
.bearer_auth(&self.jwt_token.to_string())
|
.bearer_auth(&self.jwt_token.to_string())
|
||||||
|
@ -193,31 +398,52 @@ impl Lemmy {
|
||||||
self.extract_data(res).await
|
self.extract_data(res).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn extract_data(&self, response: Result<reqwest::Response, reqwest::Error>) -> Result<String,()> {
|
async fn extract_data(&self, response: Result<reqwest::Response, reqwest::Error>) -> Option<String> {
|
||||||
match response {
|
match response {
|
||||||
Ok(data) => match data.text().await {
|
Ok(data) => {
|
||||||
Ok(data) => Ok(data),
|
if data.status().is_success() {
|
||||||
Err(e) => {
|
match data.text().await {
|
||||||
let err_msg = format!("{e}");
|
Ok(data) => Some(data),
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = format!("{e}");
|
||||||
|
error!(err_msg);
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
let err_msg = format!("HTTP Request failed: {}", data.text().await.unwrap());
|
||||||
error!(err_msg);
|
error!(err_msg);
|
||||||
Err(())
|
None
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let err_msg = format!("{e}");
|
let err_msg = format!("{e}");
|
||||||
error!(err_msg);
|
error!(err_msg);
|
||||||
Err(())
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn parse_json<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Result<T,()> {
|
async fn parse_json<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Option<T> {
|
||||||
match serde_json::from_str::<HashMap<&str, T>>(response) {
|
match serde_json::from_str::<T>(response) {
|
||||||
Ok(mut data) => Ok(data.remove("post_view").expect("Element should be present")),
|
Ok(data) => Some(data),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let err_msg = format!("{e}");
|
let err_msg = format!("while parsing JSON: {e} ");
|
||||||
error!(err_msg);
|
error!(err_msg);
|
||||||
Err(())
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn parse_json_map<'a, T: Deserialize<'a>>(&self, response: &'a str) -> Option<T> {
|
||||||
|
debug!(response);
|
||||||
|
match serde_json::from_str::<HashMap<&str, T>>(response) {
|
||||||
|
Ok(mut data) => Some(data.remove("post_view").expect("Element should be present")),
|
||||||
|
Err(e) => {
|
||||||
|
let err_msg = format!("while parsing JSON HashMap: {e}");
|
||||||
|
error!(err_msg);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -3,6 +3,7 @@ use log::{LevelFilter};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use systemd_journal_logger::{JournalLog};
|
use systemd_journal_logger::{JournalLog};
|
||||||
|
use crate::bot::Bot;
|
||||||
|
|
||||||
mod bot;
|
mod bot;
|
||||||
mod config;
|
mod config;
|
||||||
|
@ -12,8 +13,8 @@ mod fetchers;
|
||||||
|
|
||||||
pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
|
pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
|
||||||
Client::builder()
|
Client::builder()
|
||||||
.timeout(Duration::seconds(30).to_std().unwrap())
|
.timeout(Duration::seconds(10).to_std().unwrap())
|
||||||
.connect_timeout(Duration::seconds(30).to_std().unwrap())
|
.connect_timeout(Duration::seconds(10).to_std().unwrap())
|
||||||
.build()
|
.build()
|
||||||
.expect("build client")
|
.expect("build client")
|
||||||
});
|
});
|
||||||
|
@ -24,6 +25,17 @@ async fn main() {
|
||||||
.expect("Systemd-Logger crate error")
|
.expect("Systemd-Logger crate error")
|
||||||
.install()
|
.install()
|
||||||
.expect("Systemd-Logger crate error");
|
.expect("Systemd-Logger crate error");
|
||||||
log::set_max_level(LevelFilter::Info);
|
match std::env::var("LOG_LEVEL") {
|
||||||
bot::run().await;
|
Ok(level) => {
|
||||||
|
match level.as_str() {
|
||||||
|
"debug" => log::set_max_level(LevelFilter::Debug),
|
||||||
|
"info" => log::set_max_level(LevelFilter::Info),
|
||||||
|
_ => log::set_max_level(LevelFilter::Info),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => log::set_max_level(LevelFilter::Info),
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut bot = Bot::new();
|
||||||
|
bot.run().await;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue