This commit is contained in:
2022-07-02 14:51:01 +00:00
committed by GitHub
parent cebae862b0
commit 879c6c0738
3 changed files with 123 additions and 0 deletions

View File

@@ -4,3 +4,4 @@ pub mod item;
pub mod item_list;
pub mod paginated_creator_posts;
pub mod plan;
pub mod post;

90
src/fanbox/post.rs Normal file
View File

@@ -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<FanboxClientInternal>,
}
impl FanboxPostArticle {
#[inline]
pub fn cover_image_url(&self) -> Option<&str> {
self.data["coverImageUrl"].as_str()
}
#[inline]
pub fn fee_required(&self) -> Option<u64> {
self.data["feeRequired"].as_u64()
}
#[inline]
pub fn id(&self) -> Option<u64> {
parse_u64(&self.data["id"])
}
#[inline]
/// Create a new instance
pub fn new(data: &JsonValue, client: Arc<FanboxClientInternal>) -> 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<FanboxClientInternal>) -> 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()),
}
}
}

View File

@@ -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<JsonValue> {
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<FanboxPost> {
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),