From 061aa56bde68d6d874dcb0f3a37ecc4969508a6d Mon Sep 17 00:00:00 2001 From: lifegpc Date: Thu, 14 Jul 2022 13:24:41 +0000 Subject: [PATCH] Update --- src/data/fanbox.rs | 22 ++++++++++++++ src/data/json.rs | 12 ++++++++ src/data/mod.rs | 1 + src/download.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++-- src/pixiv_link.rs | 10 +++++-- 5 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 src/data/fanbox.rs diff --git a/src/data/fanbox.rs b/src/data/fanbox.rs new file mode 100644 index 0000000..9107639 --- /dev/null +++ b/src/data/fanbox.rs @@ -0,0 +1,22 @@ +use crate::fanbox::post::FanboxPost; +use crate::pixiv_link::PixivID; +use crate::pixiv_link::ToPixivID; +use json::JsonValue; + +pub struct FanboxData { + pub id: PixivID, + /// Raw data + pub raw: JsonValue, +} + +impl FanboxData { + pub fn new(id: T, post: &FanboxPost) -> Option { + match id.to_pixiv_id() { + Some(id) => Some(Self { + id, + raw: post.get_json().clone(), + }), + None => None, + } + } +} diff --git a/src/data/json.rs b/src/data/json.rs index 38312e3..ab05407 100644 --- a/src/data/json.rs +++ b/src/data/json.rs @@ -1,3 +1,4 @@ +use super::fanbox::FanboxData; use crate::data::data::PixivData; use crate::ext::json::ToJson; use crate::gettext; @@ -108,6 +109,17 @@ impl From<&PixivData> for JSONDataFile { } } +impl From for JSONDataFile { + fn from(d: FanboxData) -> Self { + let mut f = Self { + id: d.id.clone(), + maps: HashMap::new(), + }; + f.add("raw", d.raw).unwrap(); + f + } +} + impl ToJson for JSONDataFile { fn to_json(&self) -> Option { let mut value = json::object! {}; diff --git a/src/data/mod.rs b/src/data/mod.rs index aa85c4e..76eacbd 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -1,6 +1,7 @@ pub mod data; #[cfg(feature = "exif")] pub mod exif; +pub mod fanbox; pub mod json; #[cfg(feature = "avdict")] pub mod video; diff --git a/src/download.rs b/src/download.rs index 4a09e5b..bec3e7e 100644 --- a/src/download.rs +++ b/src/download.rs @@ -4,6 +4,7 @@ use crate::concat_pixiv_downloader_error; use crate::data::data::PixivData; #[cfg(feature = "exif")] use crate::data::exif::add_exifdata_to_image; +use crate::data::fanbox::FanboxData; use crate::data::json::JSONDataFile; #[cfg(feature = "ugoira")] use crate::data::video::get_video_metadata; @@ -12,9 +13,12 @@ use crate::downloader::DownloaderResult; use crate::downloader::LocalFile; use crate::error::PixivDownloaderError; use crate::ext::try_err::TryErr; +use crate::fanbox::check::CheckUnknown; +use crate::fanbox::post::FanboxPost; use crate::fanbox_api::FanboxClient; use crate::gettext; use crate::opthelper::get_helper; +use crate::pixiv_link::FanboxPostID; use crate::pixiv_link::PixivID; use crate::pixiv_web::PixivWebClient; #[cfg(feature = "ugoira")] @@ -61,7 +65,7 @@ impl Main { return r; } } - PixivID::FanboxPost(_) => { + PixivID::FanboxPost(id) => { if !fc.is_inited() { let helper = get_helper(); if !fc.init(helper.cookies()) { @@ -71,7 +75,20 @@ impl Main { if !fc.check_login().await { return 1; } - println!("Logined: {}", fc.logined()); + if !fc.logined() { + println!("{}", gettext("Warning: Fanbox client is not logined.")); + } + let r = self.download_fanbox_post(Arc::clone(&fc), id.clone()).await; + let r = match r { + Ok(_) => 0, + Err(e) => { + println!("{} {}", gettext("Failed to download post:"), e); + 1 + } + }; + if r != 0 { + return r; + } } } } @@ -353,4 +370,54 @@ impl Main { } Ok(()) } + + pub async fn download_fanbox_post( + &self, + fc: Arc, + id: FanboxPostID, + ) -> Result<(), PixivDownloaderError> { + let post = fc + .get_post_info(id.post_id) + .await + .try_err(gettext("Failed to get post info."))?; + let helper = get_helper(); + if helper.verbose() { + println!("{:#?}", post); + } + match post.check_unknown() { + Ok(_) => {} + Err(e) => { + println!( + "{} {}", + gettext("Warning: Post info contains unknown data:"), + e + ); + } + } + if post + .is_restricted() + .try_err(gettext("Failed to check the post is restricted or not."))? + { + println!("{}", gettext("Warning: This article is restricted.")); + // #TODO allow to continue + return Ok(()); + } + let base = Arc::new(PathBuf::from(format!("./{}", id.post_id))); + let json_file = base.join("body.json"); + let data = FanboxData::new(id, &post).try_err("Failed to create data file.")?; + let data_file = JSONDataFile::from(data); + data_file + .save(&json_file) + .try_err(gettext("Failed to save post data to file."))?; + match post { + FanboxPost::Article(article) => {} + FanboxPost::Image(img) => {} + FanboxPost::Unknown(_) => { + return Err(PixivDownloaderError::from(gettext( + "Unrecognized post type.", + ))); + } + } + Ok(()) + } } diff --git a/src/pixiv_link.rs b/src/pixiv_link.rs index 5f8f592..1415f0f 100644 --- a/src/pixiv_link.rs +++ b/src/pixiv_link.rs @@ -17,9 +17,9 @@ lazy_static! { /// Fanbox post ID pub struct FanboxPostID { /// Creator ID - creator_id: String, + pub creator_id: String, /// Post ID - post_id: u64, + pub post_id: u64, } impl FanboxPostID { @@ -125,6 +125,12 @@ impl ToJson for PixivID { } } +impl ToPixivID for FanboxPostID { + fn to_pixiv_id(&self) -> Option { + Some(PixivID::FanboxPost(self.clone())) + } +} + impl ToPixivID for PixivID { fn to_pixiv_id(&self) -> Option { Some(self.clone())