Fanbox api: add more list apis

This commit is contained in:
2022-06-25 08:19:11 +00:00
committed by GitHub
parent 4e6109d505
commit 40dda3fdb2

View File

@@ -27,6 +27,39 @@ pub struct FanboxClient {
data: RwLock<Option<JsonValue>>,
}
macro_rules! handle_data {
($get_data:expr, $err:expr, $info:expr) => {
match $get_data.await {
Some(r) => {
let status = r.status();
if status.as_u16() >= 400 {
println!("{} {}", $err, status);
return None;
}
match r.text_with_charset("UTF-8").await {
Ok(data) => match json::parse(data.as_str()) {
Ok(obj) => {
if get_helper().verbose() {
println!("{}\n{}", $info, obj.pretty(2),);
}
Some(obj)
}
Err(e) => {
println!("{} {}", $err, e);
None
}
},
Err(e) => {
println!("{} {}", $err, e);
None
}
}
}
None => None,
}
};
}
impl FanboxClient {
/// Create an new instance
pub fn new() -> Self {
@@ -51,6 +84,8 @@ impl FanboxClient {
None => {}
}
self.client.set_header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36");
self.client.set_header("referer", "https://www.fanbox.cc/");
self.client.set_header("origin", "https://www.fanbox.cc");
self.inited.qstore(true);
true
}
@@ -112,39 +147,43 @@ impl FanboxClient {
/// List home page's post list. All supported and followed creators' posts are included.
/// * `limit` - The max count. 10 is used on Fanbox website.
pub async fn list_home(&self, limit: u64) -> Option<JsonValue> {
pub async fn list_home_post(&self, limit: u64) -> Option<JsonValue> {
self.auto_init();
match self
.client
.get_with_param(
handle_data!(
self.client.get_with_param(
"https://api.fanbox.cc/post.listHome",
json::object! {"limit": limit},
json::object! {"referer": "https://www.fanbox.cc/", "origin": "https://www.fanbox.cc"},
)
.await
{
Some(r) => {
let status = r.status();
if status.as_u16() >= 400 {
println!("{} {}", gettext("Failed to list home page's posts:"), status);
return None;
}
match r.text_with_charset("UTF-8").await {
Ok(data) => match json::parse(data.as_str()) {
Ok(obj) => Some(obj),
Err(e) => {
println!("{} {}", gettext("Failed to list home page's posts:"), e);
None
}
},
Err(e) => {
println!("{} {}", gettext("Failed to list home page's posts:"), e);
None
}
}
}
None => None,
}
None,
),
gettext("Failed to list home page's posts:"),
gettext("Home page's posts:")
)
}
/// List all supporting plans.
pub async fn list_supporting_plan(&self) -> Option<JsonValue> {
self.auto_init();
handle_data!(
self.client
.get("https://api.fanbox.cc/plan.listSupporting", None),
gettext("Failed to list all supporting plans."),
gettext("All supporting plans:")
)
}
/// List supported creators' posts.
/// * `limit` - The max count. 10 is used on Fanbox website.
pub async fn list_supporting_post(&self, limit: u64) -> Option<JsonValue> {
self.auto_init();
handle_data!(
self.client.get_with_param(
"https://api.fanbox.cc/post.listSupporting",
json::object! {"limit": limit},
None,
),
gettext("Failed to list supported creators' posts:"),
gettext("Supported creators' posts:")
)
}
/// Returns true if is logged in.
@@ -193,30 +232,52 @@ async fn check_login() -> bool {
TEST_CLIENT.logined()
}
#[proc_macros::async_timeout_test(120s)]
#[tokio::test(flavor = "multi_thread")]
async fn test_list_home() {
match std::env::var("FANBOX_COOKIES_FILE") {
Ok(path) => {
let re = init_test_client(path).await;
if !re {
panic!("Failed to initiailze the client.");
}
if !check_login().await {
println!("The client is not logined. Skip test.");
return;
}
match TEST_CLIENT.list_home(10).await {
Some(data) => {
println!("{}", data.pretty(2));
#[cfg(test)]
macro_rules! quick_test {
($name:ident, $exp:expr, $err:literal) => {
#[proc_macros::async_timeout_test(120s)]
#[tokio::test(flavor = "multi_thread")]
async fn $name() {
match std::env::var("FANBOX_COOKIES_FILE") {
Ok(path) => {
let re = init_test_client(path).await;
if !re {
panic!("Failed to initiailze the client.");
}
if !check_login().await {
println!("The client is not logined. Skip test.");
return;
}
match $exp.await {
Some(_) => {}
None => {
panic!("{}", $err);
}
}
}
None => {
panic!("Failed to list home.");
Err(_) => {
println!("No cookies files specified, skip test.")
}
}
}
Err(_) => {
println!("No cookies files specified, skip test.")
}
}
};
}
#[cfg(test)]
quick_test!(
test_list_home_post,
TEST_CLIENT.list_home_post(10),
"Failed to list home page's posts."
);
#[cfg(test)]
quick_test!(
test_list_supporting_post,
TEST_CLIENT.list_supporting_post(10),
"Failed to list supported creators' posts."
);
#[cfg(test)]
quick_test!(
test_list_supporting_plan,
TEST_CLIENT.list_supporting_plan(),
"Failed to list all supporting plans."
);