From ff3aaebdd450f93b56e65d03b116f8886d4c9499 Mon Sep 17 00:00:00 2001 From: Andre Julius Date: Mon, 11 Jul 2022 22:48:14 +0200 Subject: [PATCH] Add get_album function --- src/api/album_type.rs | 60 +++++++++++++++++++++++++++++++++++ src/api/mod.rs | 17 ++++++++++ src/api/requests/get_album.rs | 24 ++++++++++++++ src/api/requests/mod.rs | 2 ++ src/cli/album_info.rs | 19 +++++++++++ src/cli/info_image.rs | 2 +- src/cli/mod.rs | 39 +++++++++++------------ src/cli/parse.rs | 11 +++++-- src/cli/upload_image.rs | 2 +- 9 files changed, 150 insertions(+), 26 deletions(-) create mode 100644 src/api/album_type.rs create mode 100644 src/api/requests/get_album.rs create mode 100644 src/cli/album_info.rs diff --git a/src/api/album_type.rs b/src/api/album_type.rs new file mode 100644 index 0000000..c858e1c --- /dev/null +++ b/src/api/album_type.rs @@ -0,0 +1,60 @@ +use serde::{Deserialize, Serialize}; + +use crate::ImageInfoData; + +/// Album Info Response +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct AlbumInfo { + /// Image Data + pub data: AlbumInfoData, + /// Request processed success or not. + pub success: bool, + /// HTTP status code from API request. + pub status: i32, +} + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct AlbumInfoData { + pub id: String, + pub title: String, + pub description: Option, + pub datetime: i64, + pub cover: String, + pub cover_edited: Option, + pub cover_width: i64, + pub cover_height: i64, + pub account_url: Option, + pub account_id: Option, + pub privacy: String, + pub layout: String, + pub views: i64, + pub link: String, + pub favorite: bool, + pub nsfw: bool, + pub section: Option, + pub images_count: i64, + pub in_gallery: bool, + pub is_ad: bool, + pub include_album_ads: bool, + pub is_album: bool, + pub images: Vec, + pub ad_config: AdConfig, +} + + + +#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] +pub struct AdConfig { + #[serde(rename = "safeFlags")] + pub safe_flags: Vec, + #[serde(rename = "highRiskFlags")] + pub high_risk_flags: Vec, + #[serde(rename = "unsafeFlags")] + pub unsafe_flags: Vec, + #[serde(rename = "wallUnsafeFlags")] + pub wall_unsafe_flags: Vec, + #[serde(rename = "showsAds")] + pub shows_ads: bool, + #[serde(rename = "showAdLevel")] + pub show_ad_level: i64, +} \ No newline at end of file diff --git a/src/api/mod.rs b/src/api/mod.rs index 584248b..415f246 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,9 +1,11 @@ +mod album_type; mod client; mod error; mod image_type; mod requests; mod send_api_request; +pub use album_type::*; pub(crate) use client::api_url; pub use client::ImgurClient; pub use error::*; @@ -97,4 +99,19 @@ impl ImgurClient { pub async fn image_info(&self, id: &str) -> Result { requests::get_image(self, id).await } + + /// Get album info from a Imgur + /// ``` + /// use imgurs::ImgurClient; + /// + /// #[tokio::main] + /// async fn main() { + /// let client = ImgurClient::new("3e3ce0d7ac14d56"); + /// + /// client.album_info("lFaGr1x").await.expect("delete album"); + /// } + /// ``` + pub async fn album_info(&self, id: &str) -> Result { + requests::get_album(self, id).await + } } diff --git a/src/api/requests/get_album.rs b/src/api/requests/get_album.rs new file mode 100644 index 0000000..070a42b --- /dev/null +++ b/src/api/requests/get_album.rs @@ -0,0 +1,24 @@ +use reqwest::Method; + +use crate::{api_url, send_api_request, AlbumInfo, Error, ImgurClient, Result}; + +pub async fn get_album(client: &ImgurClient, album: &str) -> Result { + // get imgur api url + let uri = api_url!(format!("album/{album}")); + + // send request to imgur api + let res = send_api_request(client, Method::GET, uri, None).await?; + + // get response http code + let status = res.status(); + + // check if an error has occurred + if status.is_client_error() || status.is_server_error() { + let body = res.text().await?; + + return Err(Error::ApiError(status.as_u16(), body)); + } + + // return `ImageInfo` + Ok(res.json().await?) +} diff --git a/src/api/requests/mod.rs b/src/api/requests/mod.rs index 2430981..c254d74 100644 --- a/src/api/requests/mod.rs +++ b/src/api/requests/mod.rs @@ -1,9 +1,11 @@ mod delete_image; +mod get_album; mod get_image; mod rate_limit; mod upload_image; pub use delete_image::*; +pub use get_album::*; pub use get_image::*; pub use rate_limit::*; pub use upload_image::*; diff --git a/src/cli/album_info.rs b/src/cli/album_info.rs new file mode 100644 index 0000000..8a91da4 --- /dev/null +++ b/src/cli/album_info.rs @@ -0,0 +1,19 @@ +use imgurs::ImgurClient; + +use super::print_image_info; + +pub async fn album_info(client: ImgurClient, id: String) { + // get a image info from imgur + let info = client + .album_info(&id) + .await + .expect("send request to imgur api"); + + print!("got album: {:?}", info); + + // print image information from imgur + for image in info.data.images { + print_image_info(&image); + println!("") + } +} diff --git a/src/cli/info_image.rs b/src/cli/info_image.rs index 261c58a..4f280e2 100644 --- a/src/cli/info_image.rs +++ b/src/cli/info_image.rs @@ -10,5 +10,5 @@ pub async fn image_info(client: ImgurClient, id: String) { .expect("send request to imfur api"); // print image information from imgur - print_image_info(&info); + print_image_info(&info.data); } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index b1876a1..e9ebcbe 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,5 +1,6 @@ mod parse; +pub mod album_info; pub mod clipboard; pub mod credits; pub mod delete_image; @@ -10,19 +11,19 @@ pub use parse::*; use chrono::{prelude::DateTime, Utc}; use colored::Colorize; -use imgurs::ImageInfo; +use imgurs::ImageInfoData; use std::time::{Duration, UNIX_EPOCH}; // print image information from imgur -pub fn print_image_info(i: &ImageInfo) { +pub fn print_image_info(i: &ImageInfoData) { // format image upload date - let d = UNIX_EPOCH + Duration::from_secs(i.data.datetime.try_into().unwrap()); + let d = UNIX_EPOCH + Duration::from_secs(i.datetime.try_into().unwrap()); let datetime = DateTime::::from(d); let timestamp_str = datetime.format("%Y-%m-%d %H:%M:%S").to_string(); // image title - if i.data.title != None { - let title = i.data.title.clone(); + if i.title != None { + let title = i.title.clone(); println!( "{} {}", @@ -32,8 +33,8 @@ pub fn print_image_info(i: &ImageInfo) { } // image description - if i.data.description != None { - let desc = i.data.description.clone(); + if i.description != None { + let desc = i.description.clone(); println!( "{} {}", @@ -43,8 +44,8 @@ pub fn print_image_info(i: &ImageInfo) { } // image deletehas - if i.data.deletehash != None { - let delhash = i.data.deletehash.clone(); + if i.deletehash != None { + let delhash = i.deletehash.clone(); println!( "{} {}", @@ -53,31 +54,27 @@ pub fn print_image_info(i: &ImageInfo) { ); } - println!("{} {}", "id".green(), i.data.id.magenta()); + println!("{} {}", "id".green(), i.id.magenta()); println!( "{} {} {}", "upload date".green(), timestamp_str.magenta(), "(UTC)".blue() ); - println!("{} {}", "type".green(), i.data.img_type.magenta()); - println!("{} {}", "width".green(), i.data.width.to_string().magenta()); - println!( - "{} {}", - "height".green(), - i.data.height.to_string().magenta() - ); + println!("{} {}", "type".green(), i.img_type.magenta()); + println!("{} {}", "width".green(), i.width.to_string().magenta()); + println!("{} {}", "height".green(), i.height.to_string().magenta()); println!( "{} {} {}", "size".green(), - (i.data.size / 1000).to_string().magenta(), + (i.size / 1000).to_string().magenta(), "KB".blue() ); - println!("{} {}", "views".green(), i.data.views.to_string().magenta()); + println!("{} {}", "views".green(), i.views.to_string().magenta()); println!( "{} {}", "bandwidth".green(), - i.data.bandwidth.to_string().magenta() + i.bandwidth.to_string().magenta() ); - println!("{} {}", "link".green(), i.data.link.magenta()); + println!("{} {}", "link".green(), i.link.magenta()); } diff --git a/src/cli/parse.rs b/src/cli/parse.rs index 45695c7..c488aa4 100644 --- a/src/cli/parse.rs +++ b/src/cli/parse.rs @@ -3,7 +3,7 @@ use clap_complete::{generate, Generator, Shell}; use imgurs::ImgurClient; use std::io::{self, stdout}; -use crate::cli::{credits::*, delete_image::*, info_image::*, upload_image::*}; +use crate::cli::{album_info::*, credits::*, delete_image::*, info_image::*, upload_image::*}; // get version from Cargo.toml const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION"); @@ -33,13 +33,16 @@ enum Commands { #[clap(about = "Print image info", display_order = 4)] Info { id: String }, + #[clap(about = "Print album info", display_order = 5)] + AlbumInfo { id: String }, + #[clap( about = "Generate completion file for a shell [bash, elvish, fish, powershell, zsh]", - display_order = 5 + display_order = 6 )] Completions { shell: Shell }, - #[clap(about = "Generate man page", display_order = 6)] + #[clap(about = "Generate man page", display_order = 7)] Manpage, } @@ -60,6 +63,8 @@ pub async fn parse(client: ImgurClient) { Commands::Info { id } => image_info(client, id.to_string()).await, + Commands::AlbumInfo { id } => album_info(client, id.to_string()).await, + Commands::Completions { shell } => { let mut app = Cli::command(); diff --git a/src/cli/upload_image.rs b/src/cli/upload_image.rs index e8ed99b..22fc07c 100644 --- a/src/cli/upload_image.rs +++ b/src/cli/upload_image.rs @@ -35,7 +35,7 @@ pub async fn upload_image(client: ImgurClient, path: String) { } // print image information from imgur - print_image_info(&i); + print_image_info(&i.data); // send notification that the image has been uploaded notify!(Notification::new()