diff --git a/Cargo.lock b/Cargo.lock index 05d3fb7..1c1bf00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -448,6 +448,7 @@ dependencies = [ "mockito", "serde", "serde_json", + "tabled", "tokio", "tokio-test", "toml", @@ -581,6 +582,15 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "papergrid" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fe93a33fc67c8a9d21f3fefa54c4a991e08d2eba5f2fd6f327ca520862eddfb" +dependencies = [ + "unicode-width", +] + [[package]] name = "parking_lot" version = "0.11.2" @@ -969,6 +979,27 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tabled" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "465c45048a4b177eee43527a3741866dd66839f15c3223b5047dbc43d8a5076e" +dependencies = [ + "papergrid", + "tabled_derive", +] + +[[package]] +name = "tabled_derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b366a04a152b687a209b2459f29da3650b74bc859390308949305211e28af4b9" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tempfile" version = "3.2.0" diff --git a/Cargo.toml b/Cargo.toml index 6f9ae3a..d0e92e6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ cookie = "0.15.1" toml = "0.5.8" serde = { version = "1", features = ["derive"] } serde_json = "1" +tabled = "0.3.0" [dev-dependencies] mockito = "0.30.0" diff --git a/src/albums.rs b/src/albums.rs index 0618585..af55fc0 100644 --- a/src/albums.rs +++ b/src/albums.rs @@ -1,9 +1,54 @@ use hyper::{Body, Method, Request, header::{AUTHORIZATION, COOKIE}}; -use serde_json::{Value, json}; +use serde_json::{Map, Value, from_str}; +use serde::Deserialize; +use tabled::Tabled; use crate::{LycheeClient, body_to_str}; -pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> Value { +fn display_option_string(o: &Option) -> String { + match o { + Some(s) => format!("{}", s), + None => format!("N/A") + } +} + +#[derive(Deserialize, Debug)] +pub struct AllAlbums { + pub smartalbums: Map, + pub albums: Vec, + pub shared_albums: Vec +} + +#[derive(Deserialize, Debug, Tabled)] +pub struct Album { + pub id: String, + pub title: String, + pub public: String, + pub full_photo: String, + pub visible: String, + pub nsfw: String, + pub parent_id: String, + pub cover_id: String, + pub description: String, + pub downloadable: String, + pub share_button_visible: String, + pub created_at: String, + pub updated_at: String, + #[field(display_with="display_option_string")] + pub min_taken_at: Option, + #[field(display_with="display_option_string")] + pub max_taken_at: Option, + pub password: String, + pub license: String, + pub sorting_col: String, + pub sorting_order: String, + #[field(display_with="display_option_string")] + pub thumb: Option, + pub has_albums: String, + pub owner: String +} + +pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> AllAlbums { let req = Request::builder() .method(Method::POST) .uri(c.endpoint.to_string() + "/api/Albums::get") @@ -12,5 +57,6 @@ pub async fn get_albums(c: &LycheeClient, lychee_session: &str) -> Value { .body(Body::empty()) .expect("Cannot request /api/Albums::get."); let res = c.client.request(req).await.unwrap(); - return json!(body_to_str(res.into_body()).await); + let v: AllAlbums = from_str(body_to_str(res.into_body()).await.as_str()).unwrap(); + return v; } diff --git a/src/main.rs b/src/main.rs index 86623b6..495f734 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ use hyper::{Client, client::HttpConnector}; use clap::{App, Arg, SubCommand, crate_version}; use session::logout; use cookie::Cookie; +use tabled::Table; mod albums; mod session; @@ -76,7 +77,8 @@ async fn main() -> Result<(), Box> { logout(&client, &lychee_session_cookie).await; } else if let Some(matches) = matches.subcommand_matches("albums") { if matches.is_present("get") { - println!("{}", get_albums(&client, &lychee_session_cookie).await); + let a = get_albums(&client, &lychee_session_cookie).await.albums; + println!("{}", Table::new(a).to_string()); } else { App::print_long_help(&mut app).unwrap(); }