A tool to read animebox backup files and export the data in alternate formats.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

94 lines
2.6 KiB

use clap::{App, Arg};
use lazy_static::lazy_static;
use serde_json::from_reader;
use std::error::Error;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use std::process::exit;
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
lazy_static! {
static ref COMMANDS: Vec<&'static str> = vec!["print", "searches", "favorites"];
}
mod model;
fn read_animeboxes_backup<P: AsRef<Path>>(
path: P,
) -> Result<model::anime_boxes::Backup, Box<dyn Error>> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let result: model::anime_boxes::Backup = from_reader(reader)?;
Ok(result)
}
fn searches_command(backup: model::anime_boxes::Backup) {
let mut searches: Vec<String> = backup
.search_history
.iter()
.map(|s| String::from(&s.search_text))
.collect();
searches.sort();
for search in searches {
println!("{}", search);
}
}
fn favorites_command(backup: model::anime_boxes::Backup) {
let mut favorites: Vec<String> = backup
.favorites
.iter()
.map(|f| String::from(&f.file.url))
.collect();
for favorite in favorites {
println!("{}", favorite);
}
}
fn main() {
let matches = App::new("AnimeBoxes Sync")
.version(VERSION.unwrap_or("UNKNOWN"))
.author("Drew Short <warrick@sothr.com>")
.about("Parses AnimeBoxes backup files")
.arg(
Arg::with_name("config")
.short("c")
.value_name("FILE")
.help("Set a custom config file")
.takes_value(true),
)
.arg(
Arg::with_name("INPUT")
.help("The AnimeBoxes file to process")
.required(true)
.index(1),
)
.arg(
Arg::with_name("COMMAND")
.help("The command to run on the backup")
.required(true)
.index(2)
.possible_values(&COMMANDS),
)
.get_matches();
let config = matches.value_of("config").unwrap_or("abs-default.conf");
let path = matches.value_of("INPUT").unwrap();
let result = read_animeboxes_backup(path).unwrap();
let command = matches.value_of("COMMAND").unwrap();
match command {
"favorites" => {
favorites_command(result);
}
"print" => println!("{:#?}", result),
"searches" => {
searches_command(result);
}
_ => {
println!("{} is unrecognized", command);
exit(1)
}
}
}