parses actor.json for author account info

main
Ashelyn Dawn 1 year ago
parent 34c462fead
commit f5ff64b88d

@ -13,9 +13,6 @@ pub struct GeneratorOptions {
#[arg(short = 'o', long, default_value = "out")]
pub output_dir: String,
#[arg(long, default_value = "temp")]
pub temp_dir: String,
#[arg(short = 'O', long)]
pub overwrite: bool,
}

@ -7,6 +7,19 @@ pub struct Outbox {
pub ordered_items: Vec<Item>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Person {
pub id: String,
pub preferred_username: String,
pub name: String,
pub summary: String,
pub url: String,
#[serde(rename = "attachment")]
pub attachments: Option<Vec<Attachment>>,
}
#[derive(Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Item {
@ -44,21 +57,30 @@ pub struct Post {
url: String,
sensitive: bool,
content: String,
// attachment: Vec<Attachment>,
// tag: Vec<Tag>,
// replies: Collection,
#[serde(rename = "attachment")]
attachments: Option<Vec<Attachment>>,
tag: Option<Vec<Tag>>,
replies: Option<Collection>,
quote_uri: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Attachment {
media_type: String,
url: String,
name: String,
focal_point: [f32; 2],
width: u32,
height: u32,
#[serde(rename_all = "camelCase", tag = "type")]
pub enum Attachment {
#[serde(rename = "Document", rename_all = "camelCase")]
File {
media_type: Option<String>,
url: String,
name: Option<String>,
focal_point: Option<[f32; 2]>,
width: Option<u32>,
height: Option<u32>,
},
#[serde(rename = "PropertyValue")]
Property { name: String, value: String },
}
#[derive(Deserialize, Debug)]

@ -10,8 +10,10 @@ use std::{
};
use tar::Archive;
use crate::cli::GeneratorOptions;
use crate::data::Outbox;
use crate::{
cli::GeneratorOptions,
data::{Outbox, Person},
};
mod cli;
mod data;
@ -20,7 +22,7 @@ fn main() {
let args = GeneratorOptions::parse();
if let Err(err) = setup_dirs(&args) {
eprintln!("Could not create output or temp directory");
eprintln!("Could not create output directory");
eprintln!("Error: {}", err);
std::process::exit(1);
}
@ -32,34 +34,18 @@ fn main() {
std::process::exit(1);
}
let outbox_text = archive_result.unwrap();
let data: Result<Outbox, serde_json::Error> = serde_json::from_str(outbox_text.as_str());
if let Err(err) = data {
eprintln!("Could not parse outbox file");
eprintln!("Error: {}", err);
std::process::exit(1);
}
let (_outbox_text, actor_text) = archive_result.unwrap();
let outbox_text = fs::read_to_string("./temp/outbox-formatted.json").unwrap();
let (outbox, author) = parse_json(outbox_text, actor_text);
let data = data.unwrap();
println!("{:?}\n", outbox.ordered_items.first().unwrap());
println!("{:?}", data.ordered_items.first().unwrap());
println!("{:?}", author)
}
fn setup_dirs(args: &GeneratorOptions) -> Result<(), std::io::Error> {
let temp_dir = Path::new(&args.temp_dir);
let output_dir = Path::new(&args.output_dir);
// Clear temp dir
if !temp_dir.exists() {
fs::create_dir(temp_dir)?
} else {
for entry in fs::read_dir(temp_dir)? {
fs::remove_file(entry?.path())?;
}
}
// Create output dir
if !output_dir.exists() {
fs::create_dir(output_dir)?;
@ -77,7 +63,7 @@ fn setup_dirs(args: &GeneratorOptions) -> Result<(), std::io::Error> {
Ok(())
}
fn read_archive(args: &GeneratorOptions) -> Result<String, std::io::Error> {
fn read_archive(args: &GeneratorOptions) -> Result<(String, String), std::io::Error> {
let path = &args.archive_file;
let tar_gz = File::open(path)?;
@ -85,6 +71,7 @@ fn read_archive(args: &GeneratorOptions) -> Result<String, std::io::Error> {
let mut archive = Archive::new(tar);
let mut outbox_text = String::new();
let mut actor_text = String::new();
for (_, file) in archive.entries()?.enumerate() {
let mut file = file?;
@ -94,13 +81,30 @@ fn read_archive(args: &GeneratorOptions) -> Result<String, std::io::Error> {
let path = path_result?.clone();
path_str = String::from_str(&path.to_string_lossy()).unwrap();
}
if path_str.starts_with("media_attachments") {
file.unpack_in(&args.output_dir)?;
} else if path_str.eq("outbox.json") {
file.read_to_string(&mut outbox_text)?;
} else {
match path_str.as_str() {
"outbox.json" => {
file.read_to_string(&mut outbox_text)?;
}
"actor.json" => {
file.read_to_string(&mut actor_text)?;
}
"avatar.png" => {
file.unpack_in(&args.output_dir)?;
}
"header.png" => {
file.unpack_in(&args.output_dir)?;
}
_ => (),
};
}
}
let test = if 1 > 0 { 'a' } else { 'b' };
if outbox_text.len() < 1 {
return Err(std::io::Error::new(
ErrorKind::Other,
@ -108,5 +112,35 @@ fn read_archive(args: &GeneratorOptions) -> Result<String, std::io::Error> {
));
}
Ok(outbox_text)
if actor_text.len() < 1 {
return Err(std::io::Error::new(
ErrorKind::Other,
"No actor.json in archive",
));
}
Ok((outbox_text, actor_text))
}
fn parse_json(outbox_text: String, actor_text: String) -> (Outbox, Person) {
let outbox_parse: Result<Outbox, serde_json::Error> =
serde_json::from_str(outbox_text.as_str());
let actor_parse: Result<Person, serde_json::Error> = serde_json::from_str(actor_text.as_str());
if let Err(err) = outbox_parse {
eprintln!("Could not parse outbox file");
eprintln!("Error: {}", err);
std::process::exit(1);
}
if let Err(err) = actor_parse {
eprintln!("Could not parse actor file");
eprintln!("Error: {}", err);
std::process::exit(1);
}
let outbox = outbox_parse.unwrap();
let author = actor_parse.unwrap();
(outbox, author)
}

Loading…
Cancel
Save