Browse Source

Store the more recent favourite and retrieve everything TILL this element

master
Julio Biason 6 years ago
parent
commit
9bf6983e28
  1. 1
      .gitignore
  2. 3
      Cargo.lock
  3. 7
      Cargo.toml
  4. 48
      src/main.rs

1
.gitignore vendored

@ -7,5 +7,6 @@
**/*.rs.bk
mastodon.toml
downfav.toml
data

3
Cargo.lock generated

@ -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]]

7
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"

48
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<u>
// }
#[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('?')

Loading…
Cancel
Save