From 879c6c07384342b5015351c5c58d15861d6d4a32 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sat, 2 Jul 2022 14:51:01 +0000 Subject: [PATCH] Update --- src/fanbox/mod.rs | 1 + src/fanbox/post.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++ src/fanbox_api.rs | 32 +++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 src/fanbox/post.rs diff --git a/src/fanbox/mod.rs b/src/fanbox/mod.rs index bbbdb6f..f990e5c 100644 --- a/src/fanbox/mod.rs +++ b/src/fanbox/mod.rs @@ -4,3 +4,4 @@ pub mod item; pub mod item_list; pub mod paginated_creator_posts; pub mod plan; +pub mod post; diff --git a/src/fanbox/post.rs b/src/fanbox/post.rs new file mode 100644 index 0000000..362f3ea --- /dev/null +++ b/src/fanbox/post.rs @@ -0,0 +1,90 @@ +use crate::fanbox_api::FanboxClientInternal; +use crate::parser::json::parse_u64; +use json::JsonValue; +use std::fmt::Debug; +use std::sync::Arc; + +/// Fanbox post's article +pub struct FanboxPostArticle { + /// Raw data + pub data: JsonValue, + /// Fanbox api client + client: Arc, +} + +impl FanboxPostArticle { + #[inline] + pub fn cover_image_url(&self) -> Option<&str> { + self.data["coverImageUrl"].as_str() + } + + #[inline] + pub fn fee_required(&self) -> Option { + self.data["feeRequired"].as_u64() + } + + #[inline] + pub fn id(&self) -> Option { + parse_u64(&self.data["id"]) + } + + #[inline] + /// Create a new instance + pub fn new(data: &JsonValue, client: Arc) -> Self { + Self { + data: data.clone(), + client, + } + } + + #[inline] + pub fn published_datetime(&self) -> Option<&str> { + self.data["publishedDatetime"].as_str() + } + + #[inline] + pub fn title(&self) -> Option<&str> { + self.data["title"].as_str() + } + + #[inline] + pub fn updated_datetime(&self) -> Option<&str> { + self.data["updatedDatetime"].as_str() + } +} + +impl Debug for FanboxPostArticle { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("FanboxPostArticle") + .field("id", &self.id()) + .field("cover_image_url", &self.cover_image_url()) + .field("fee_required", &self.fee_required()) + .field("published_datetime", &self.published_datetime()) + .field("title", &self.title()) + .field("updated_datetime", &self.updated_datetime()) + .finish_non_exhaustive() + } +} + +#[derive(Debug)] +/// The fanbox's post +pub enum FanboxPost { + /// Article + Article(FanboxPostArticle), + /// Unknown + Unknown(JsonValue), +} + +impl FanboxPost { + #[inline] + /// Create a new instance + pub fn new(data: &JsonValue, client: Arc) -> Self { + match data["type"].as_str() { + Some(s) => match s { + "article" => Self::Article(FanboxPostArticle::new(data, client)), + _ => Self::Unknown(data.clone()), + }, + _ => Self::Unknown(data.clone()), + } + } +} diff --git a/src/fanbox_api.rs b/src/fanbox_api.rs index 5936505..c36a2fd 100644 --- a/src/fanbox_api.rs +++ b/src/fanbox_api.rs @@ -5,6 +5,7 @@ use crate::fanbox::creator::FanboxCreator; use crate::fanbox::item_list::FanboxItemList; use crate::fanbox::paginated_creator_posts::PaginatedCreatorPosts; use crate::fanbox::plan::FanboxPlanList; +use crate::fanbox::post::FanboxPost; use crate::gettext; use crate::opthelper::get_helper; use crate::parser::metadata::MetaDataParser; @@ -161,6 +162,22 @@ impl FanboxClientInternal { ) } + #[allow(dead_code)] + /// Get post info + /// * `post_id` - The id of the post + pub async fn get_post_info(&self, post_id: u64) -> Option { + self.auto_init(); + handle_data!( + self.client.get_with_param( + "https://api.fanbox.cc/post.info", + json::object! { "postId": post_id }, + None, + ), + gettext("Failed to get post info:"), + gettext("Post info:") + ) + } + /// Send requests to speicfied url. /// * `url` - The url /// * `errmsg` - The error message when error occured. @@ -276,6 +293,16 @@ impl FanboxClient { } } + #[allow(dead_code)] + /// Get post info + /// * `post_id` - The id of the post + pub async fn get_post_info(&self, post_id: u64) -> Option { + match self.client.get_post_info(post_id).await { + Some(s) => Some(FanboxPost::new(&s["body"], Arc::clone(&self.client))), + None => None, + } + } + /// Create an new instance pub fn new() -> Self { Self { @@ -357,6 +384,11 @@ impl Deref for FanboxClient { } } +fanbox_api_quick_test!( + test_get_post_info, + client.get_post_info(4070200), + "Failed to get the post info(第253話 【壁紙プレゼント】ひんやりレモンころねちゃん🍋)." +); fanbox_api_quick_test!( test_list_home_post, client.list_home_post(10),