Browse Source

Extended the configuration to accept Joplin configs

master
Julio Biason 5 years ago
parent
commit
6cbe296135
  1. 101
      src/main.rs

101
src/main.rs

@ -17,30 +17,22 @@ use toml;
use serde_derive::Serialize; use serde_derive::Serialize;
use serde_derive::Deserialize; use serde_derive::Deserialize;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize, Debug)]
struct JoplinConfig {
port: u32,
folder: String,
}
#[derive(Serialize, Deserialize, Debug)]
struct Config { struct Config {
last_favorite: String last_favorite: String,
joplin: Option<JoplinConfig>,
} }
fn main() { fn main() {
let client = if let Ok(data) = elefren_toml::from_file("mastodon.toml") { let config = dbg!(get_config());
Mastodon::from(data) let client = get_mastodon_connection();
} else { let top = config.last_favorite.to_string();
println!("Your server URL: ");
let mut server = String::new();
io::stdin().read_line(&mut server)
.expect("You need to enter yoru server URL");
let registration = Registration::new(server.trim())
.client_name("downfav")
.build()
.unwrap();
let mastodon = cli::authenticate(registration).unwrap();
elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap();
mastodon
};
let top = get_top_favourite();
let most_recent_favourite = client let most_recent_favourite = client
.favourites() .favourites()
@ -56,8 +48,21 @@ fn main() {
} }
}}); }});
save_config(&config, most_recent_favourite)
}
fn save_config(config: &Config, most_recent_favourite: Option<String>) -> () {
if let Some(id) = most_recent_favourite { if let Some(id) = most_recent_favourite {
let new_configuration = Config { last_favorite: id }; let new_configuration = Config {
last_favorite: id,
joplin: match &config.joplin {
None => None,
Some(x) => Some(JoplinConfig {
folder: x.folder.to_string(),
port: x.port,
})
}
};
let content = toml::to_string(&new_configuration).unwrap(); let content = toml::to_string(&new_configuration).unwrap();
if let Ok(mut fp) = File::create("downfav.toml") { if let Ok(mut fp) = File::create("downfav.toml") {
@ -66,16 +71,35 @@ fn main() {
} }
} }
fn get_top_favourite() -> String { fn get_mastodon_connection() -> Mastodon {
if let Ok(data) = elefren_toml::from_file("mastodon.toml") {
Mastodon::from(data)
} else {
print!("Your server URL: ");
let mut server = String::new();
io::stdin().read_line(&mut server)
.expect("You need to enter yoru server URL");
let registration = Registration::new(server.trim())
.client_name("downfav")
.build()
.unwrap();
let mastodon = cli::authenticate(registration).unwrap();
elefren_toml::to_file(&*mastodon, "mastodon.toml").unwrap();
mastodon
}
}
fn get_config() -> Config {
if let Ok(mut fp) = File::open("downfav.toml") { if let Ok(mut fp) = File::open("downfav.toml") {
let mut contents = String::new(); let mut contents = String::new();
fp.read_to_string(&mut contents).unwrap(); fp.read_to_string(&mut contents).unwrap();
let config: Config = toml::from_str(&contents) let config: Config = toml::from_str(&contents)
.unwrap_or( Config { last_favorite: "".to_string() } ); .unwrap_or( Config { last_favorite: "".to_string(), joplin: None } );
config.last_favorite config
} else { } else {
"".to_string() Config { last_favorite: "".to_string(), joplin: None }
} }
} }
@ -83,8 +107,7 @@ fn dump_record(record: &Status) {
println!("Downloading {}/{}", println!("Downloading {}/{}",
&record.account.acct, &record.account.acct,
&record.id); &record.id);
dbg!("Content:", record); create_structure(dbg!(&record));
create_structure(&record);
save_content(&record); save_content(&record);
save_attachments(&record); save_attachments(&record);
} }
@ -96,32 +119,29 @@ fn toot_dir(record: &Status) -> PathBuf {
} }
fn create_structure(record: &Status) { fn create_structure(record: &Status) {
println!("Current ID: {}", record.id);
std::fs::create_dir_all(toot_dir(record)).expect("Failed to create the storage path"); std::fs::create_dir_all(toot_dir(record)).expect("Failed to create the storage path");
} }
fn save_content(record: &Status) { fn save_content(record: &Status) {
if let Ok(mut fp) = File::create(toot_dir(&record).join("toot.md")) { if let Ok(mut fp) = File::create(toot_dir(&record).join("toot.md")) {
dbg!("Saving content of", &record.id);
fp.write_all(html2md::parse_html(&record.content).as_bytes()) fp.write_all(html2md::parse_html(&record.content).as_bytes())
.expect("Failed to save content"); .expect("Failed to save content");
} }
} }
fn save_attachments(record: &Status) { fn save_attachments(record: &Status) {
dbg!("Saving attachments of", &record.id);
let base_path = toot_dir(&record); let base_path = toot_dir(&record);
record record.media_attachments
.media_attachments
.iter() .iter()
.for_each(move |x| save_attachment(&x, &base_path)); .for_each(move |x| save_attachment(dbg!(&x), &base_path));
} }
fn save_attachment(attachment: &Attachment, base_path: &PathBuf) { fn save_attachment(attachment: &Attachment, base_path: &PathBuf) {
dbg!("Saving attachment", &attachment.url); let filename = get_attachment_filename(dbg!(&attachment.url));
let filename = base_path.join(get_attachment_filename(&attachment.url));
dbg!("Saving attachment to", &filename);
println!("\tAttachment: {:?}", &filename); println!("\tAttachment: {:?}", &filename);
if let Ok(mut fp) = File::create(filename) { let saving_target = dbg!(base_path.join(filename));
if let Ok(mut fp) = File::create(saving_target) {
let client = reqwest::Client::builder() let client = reqwest::Client::builder()
.timeout(Duration::from_secs(600)) .timeout(Duration::from_secs(600))
.build() .build()
@ -136,17 +156,14 @@ fn save_attachment(attachment: &Attachment, base_path: &PathBuf) {
fn get_attachment_filename(url: &str) -> String { fn get_attachment_filename(url: &str) -> String {
let mut frags = url.rsplitn(2, '/'); let mut frags = url.rsplitn(2, '/');
dbg!("URL fragments:", &frags);
if let Some(path_part) = frags.next() { if let Some(path_part) = frags.next() {
dbg!("Found path in the attachment URL:", &path_part); dbg!(path_part
path_part
.split('?') .split('?')
.next() .next()
.unwrap_or(url) .unwrap_or(url)
.to_string() .to_string())
} else { } else {
// this is, most of the time, bad (due special characters -- like '?' -- and path) // this is, most of the time, bad (due special characters -- like '?' -- and path)
dbg!("No path in attachment, using full URL"); dbg!(url.to_string())
url.to_string()
} }
} }

Loading…
Cancel
Save