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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. use clap::{App, Arg};
  2. use lazy_static::lazy_static;
  3. use serde_json::from_reader;
  4. use std::error::Error;
  5. use std::fs::File;
  6. use std::io::BufReader;
  7. use std::path::Path;
  8. use std::process::exit;
  9. const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
  10. lazy_static! {
  11. static ref COMMANDS: Vec<&'static str> = vec!["print", "searches", "favorites"];
  12. }
  13. mod model;
  14. fn read_animeboxes_backup<P: AsRef<Path>>(
  15. path: P,
  16. ) -> Result<model::anime_boxes::Backup, Box<dyn Error>> {
  17. let file = File::open(path)?;
  18. let reader = BufReader::new(file);
  19. let result: model::anime_boxes::Backup = from_reader(reader)?;
  20. Ok(result)
  21. }
  22. fn searches_command(backup: model::anime_boxes::Backup) {
  23. let mut searches: Vec<String> = backup
  24. .search_history
  25. .iter()
  26. .map(|s| String::from(&s.search_text))
  27. .collect();
  28. searches.sort();
  29. for search in searches {
  30. println!("{}", search);
  31. }
  32. }
  33. fn favorites_command(backup: model::anime_boxes::Backup) {
  34. let mut favorites: Vec<String> = backup
  35. .favorites
  36. .iter()
  37. .map(|f| String::from(&f.file.url))
  38. .collect();
  39. for favorite in favorites {
  40. println!("{}", favorite);
  41. }
  42. }
  43. fn main() {
  44. let matches = App::new("AnimeBoxes Sync")
  45. .version(VERSION.unwrap_or("UNKNOWN"))
  46. .author("Drew Short <warrick@sothr.com>")
  47. .about("Parses AnimeBoxes backup files")
  48. .arg(
  49. Arg::with_name("config")
  50. .short("c")
  51. .value_name("FILE")
  52. .help("Set a custom config file")
  53. .takes_value(true),
  54. )
  55. .arg(
  56. Arg::with_name("INPUT")
  57. .help("The AnimeBoxes file to process")
  58. .required(true)
  59. .index(1),
  60. )
  61. .arg(
  62. Arg::with_name("COMMAND")
  63. .help("The command to run on the backup")
  64. .required(true)
  65. .index(2)
  66. .possible_values(&COMMANDS),
  67. )
  68. .get_matches();
  69. let config = matches.value_of("config").unwrap_or("abs-default.conf");
  70. let path = matches.value_of("INPUT").unwrap();
  71. let result = read_animeboxes_backup(path).unwrap();
  72. let command = matches.value_of("COMMAND").unwrap();
  73. match command {
  74. "favorites" => {
  75. favorites_command(result);
  76. }
  77. "print" => println!("{:#?}", result),
  78. "searches" => {
  79. searches_command(result);
  80. }
  81. _ => {
  82. println!("{} is unrecognized", command);
  83. exit(1)
  84. }
  85. }
  86. }