diff --git a/.gitignore b/.gitignore index 83e1106..503771c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,5 +7,6 @@ **/*.rs.bk mastodon.toml +downfav.toml data diff --git a/Cargo.lock b/Cargo.lock index 304123a..3bf0071 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -279,6 +279,9 @@ dependencies = [ "html2md 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "reqwest 0.9.17 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.91 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 567ba24..9faa1c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,10 @@ edition = "2018" [dependencies] elefren = { version = "0.19", features = ["toml"] } +env_logger = "0.6.1" html2md = "0.2.9" -reqwest = "0.9.17" log = "0.4.6" -env_logger = "0.6.1" +reqwest = "0.9.17" +serde = "*" +serde_derive = "*" +toml = "0.5" diff --git a/src/main.rs b/src/main.rs index 9ed1bee..0f0a486 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,22 +6,26 @@ use std::path::PathBuf; use elefren::entities::attachment::Attachment; use elefren::entities::status::Status; use elefren::helpers::cli; -use elefren::helpers::toml; +use elefren::helpers::toml as elefren_toml; use elefren::prelude::*; use reqwest; use log; use env_logger; +use toml; +use serde_derive::Serialize; +use serde_derive::Deserialize; -// struct Config { -// last_favorite: Option -// } +#[derive(Serialize, Deserialize)] +struct Config { + last_favorite: String +} fn main() { env_logger::init(); - let client = if let Ok(data) = toml::from_file("mastodon.toml") { + let client = if let Ok(data) = elefren_toml::from_file("mastodon.toml") { Mastodon::from(data) } else { let registration = Registration::new("https://functional.cafe") @@ -29,20 +33,20 @@ fn main() { .build() .unwrap(); let mastodon = cli::authenticate(registration).unwrap(); - toml::to_file(&*mastodon, "mastodon.toml").unwrap(); + elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap(); mastodon }; - // let last_favorite = if let Ok(fp) + let top = get_top_favourite(); - let top = ""; log::info!("Starting up..."); + log::debug!("Going all the way till {}", top); + let most_recent_favourite = client .favourites() .unwrap() .items_iter() .take_while(|record| record.id != top) - .take(2) .map(|record| { dump_record(&record); record }) .fold(None, {|first, current| { if let Some(_) = first { @@ -52,10 +56,32 @@ fn main() { } }}); log::debug!("First favourite: {:?}", most_recent_favourite); + + if let Some(id) = most_recent_favourite { + let new_configuration = Config { last_favorite: id }; + let content = toml::to_string(&new_configuration).unwrap(); + + if let Ok(mut fp) = File::create("downfav.toml") { + fp.write_all(content.as_bytes()).unwrap(); + } + } +} + +fn get_top_favourite() -> String { + if let Ok(mut fp) = File::open("downfav.toml") { + let mut contents = String::new(); + fp.read_to_string(&mut contents).unwrap(); + + let config: Config = toml::from_str(&contents) + .unwrap_or( Config { last_favorite: "".to_string() } ); + config.last_favorite + } else { + "".to_string() + } } fn dump_record(record: &Status) -> () { - log::debug!("Retriving record {}", record.id); + log::debug!("Retrieving record {}", record.id); log::debug!("Content: {:?}", record); create_structure(&record); save_content(&record); @@ -104,7 +130,7 @@ fn save_attachment(attachment: &Attachment, base_path: &PathBuf) -> () { fn get_attachment_filename(url: &str) -> String { let mut frags = url.rsplitn(2, '/'); log::debug!("URL fragments: {:?}", frags); - if let Some(path_part) = frags.next() { + if let Some(path_part) = frags.next() { log::debug!("Found path in the attachment URL: {:?}", path_part); path_part .split('?')