Skip to content

Commit

Permalink
fix: img extract (#178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy authored Aug 16, 2023
1 parent 5a3c0e9 commit 33d7836
Show file tree
Hide file tree
Showing 143 changed files with 1,539 additions and 1,311 deletions.
112 changes: 56 additions & 56 deletions package.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions rs/src/base64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::infra::result::{homo_result_string, HomoResult, IntoResult};
use crate::infra::result::{HomoResult, IntoResult, ResultExt};
use crate::panic_hook;
use alloc::string::String;
use anyhow::Result;
Expand All @@ -20,7 +20,7 @@ impl RsBase64 {
panic_hook!();
let text = decode(base64);

homo_result_string(text)
text.homo_string()
}

#[wasm_bindgen(js_name = encodeUrl)]
Expand All @@ -33,7 +33,7 @@ impl RsBase64 {
panic_hook!();
let text = decode_url(base64url);

homo_result_string(text)
text.homo_string()
}
}

Expand Down
32 changes: 32 additions & 0 deletions rs/src/cnb/api_base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
pub const BLOG_BACKEND: &str = "https://i.cnblogs.com/api";
#[macro_export]
macro_rules! blog_backend {
($($arg:tt)*) => {{
use $crate::cnb::api_base::BLOG_BACKEND;
use alloc::format;
let rest = format!($($arg)*);
format!("{}{}", BLOG_BACKEND, rest)
}};
}

pub const OPENAPI: &str = "https://api.cnblogs.com/api";
#[macro_export]
macro_rules! openapi {
($($arg:tt)*) => {{
use $crate::cnb::api_base::OPENAPI;
use alloc::format;
let rest = format!($($arg)*);
format!("{}{}", OPENAPI, rest)
}};
}

pub const OAUTH: &str = "https://oauth.cnblogs.com";
#[macro_export]
macro_rules! oauth {
($($arg:tt)*) => {{
use $crate::cnb::api_base::OAUTH;
use alloc::format;
let rest = format!($($arg)*);
format!("{}{}", OAUTH, rest)
}};
}
24 changes: 9 additions & 15 deletions rs/src/cnb/ing/comment.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::cnb::ing::{IngReq, ING_API_BASE_URL};
use crate::cnb::ing::IngReq;
use crate::http::unit_or_err;
use crate::infra::http::{setup_auth, APPLICATION_JSON};
use crate::infra::result::IntoResult;
use crate::panic_hook;
use crate::infra::result::ResultExt;
use crate::{openapi, panic_hook};
use alloc::format;
use alloc::string::{String, ToString};
use anyhow::{anyhow, Result};
use core::ops::Not;
use alloc::string::String;
use anyhow::Result;
use reqwest::header::CONTENT_TYPE;
use serde::{Deserialize, Serialize};
use wasm_bindgen::prelude::*;
Expand All @@ -32,7 +32,7 @@ impl IngReq {
parent_comment_id: Option<usize>,
) -> Result<(), String> {
panic_hook!();
let url = format!("{ING_API_BASE_URL}/{ing_id}/comments");
let url = openapi!("/statuses/{}/comments", ing_id);

let client = reqwest::Client::new().post(url);

Expand All @@ -47,16 +47,10 @@ impl IngReq {
let result: Result<()> = try {
let body = serde_json::to_string_pretty(&body)?;
let req = req.body(body);

let resp = req.send().await?;
let code = resp.status();

if code.is_success().not() {
let text = resp.text().await?;
anyhow!("{}: {}", code, text).into_err()?
}
unit_or_err(resp).await?
};

result.map_err(|e| e.to_string())
result.err_to_string()
}
}
22 changes: 8 additions & 14 deletions rs/src/cnb/ing/get_comment.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,29 @@
use crate::cnb::ing::{IngReq, ING_API_BASE_URL};
use crate::cnb::ing::IngReq;
use crate::http::body_or_err;
use crate::infra::http::setup_auth;
use crate::infra::result::{homo_result_string, HomoResult, IntoResult};
use crate::panic_hook;
use crate::infra::result::{HomoResult, ResultExt};
use crate::{openapi, panic_hook};
use alloc::format;
use alloc::string::String;
use anyhow::{anyhow, Result};
use anyhow::Result;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_class = IngReq)]
impl IngReq {
#[wasm_bindgen(js_name = getComment)]
pub async fn export_get_comment(&self, ing_id: usize) -> HomoResult<String> {
panic_hook!();
let url = format!("{ING_API_BASE_URL}/{ing_id}/comments");
let url = openapi!("/statuses/{}/comments", ing_id);

let client = reqwest::Client::new().get(url);

let req = setup_auth(client, &self.token, self.is_pat_token);

let result: Result<String> = try {
let resp = req.send().await?;
let code = resp.status();

if code.is_success() {
resp.text().await?
} else {
let text = resp.text().await?;
anyhow!("{}: {}", code, text).into_err()?
}
body_or_err(resp).await?
};

homo_result_string(result)
result.homo_string()
}
}
22 changes: 8 additions & 14 deletions rs/src/cnb/ing/get_list.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::cnb::ing::{IngReq, ING_API_BASE_URL};
use crate::cnb::ing::IngReq;
use crate::http::body_or_err;
use crate::infra::http::setup_auth;
use crate::infra::result::{homo_result_string, HomoResult, IntoResult};
use crate::panic_hook;
use crate::infra::result::{HomoResult, ResultExt};
use crate::{openapi, panic_hook};
use alloc::string::String;
use alloc::{format, vec};
use anyhow::{anyhow, Result};
use anyhow::Result;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_class = IngReq)]
Expand All @@ -17,7 +18,7 @@ impl IngReq {
ing_type: usize,
) -> HomoResult<String> {
panic_hook!();
let url = format!("{ING_API_BASE_URL}/@{ing_type}");
let url = openapi!("/statuses/@{}", ing_type);

let client = reqwest::Client::new().get(url);

Expand All @@ -26,16 +27,9 @@ impl IngReq {

let result: Result<String> = try {
let resp = req.send().await?;
let code = resp.status();

if code.is_success() {
resp.text().await?
} else {
let text = resp.text().await?;
anyhow!("{}: {}", code, text).into_err()?
}
body_or_err(resp).await?
};

homo_result_string(result)
result.homo_string()
}
}
2 changes: 0 additions & 2 deletions rs/src/cnb/ing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use crate::panic_hook;
use alloc::string::{String, ToString};
use wasm_bindgen::prelude::*;

const ING_API_BASE_URL: &str = "https://api.cnblogs.com/api/statuses";

#[wasm_bindgen(js_name = IngReq)]
pub struct IngReq {
token: String,
Expand Down
21 changes: 8 additions & 13 deletions rs/src/cnb/ing/pub.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::cnb::ing::{IngReq, ING_API_BASE_URL};
use crate::cnb::ing::IngReq;
use crate::http::unit_or_err;
use crate::infra::http::{setup_auth, APPLICATION_JSON};
use crate::infra::result::IntoResult;
use crate::panic_hook;
use crate::infra::result::ResultExt;
use crate::{openapi, panic_hook};
use alloc::string::{String, ToString};
use anyhow::{anyhow, Result};
use core::ops::Not;
use anyhow::Result;
use reqwest::header::CONTENT_TYPE;
use serde_json::json;
use wasm_bindgen::prelude::*;
Expand All @@ -14,7 +14,7 @@ impl IngReq {
#[wasm_bindgen(js_name = pub)]
pub async fn export_pub(&self, content: &str, is_private: bool) -> Result<(), String> {
panic_hook!();
let url = ING_API_BASE_URL;
let url = openapi!("/statuses");

let body = json!({
"content": content,
Expand All @@ -30,14 +30,9 @@ impl IngReq {

let result: Result<()> = try {
let resp = req.send().await?;
let code = resp.status();

if code.is_success().not() {
let text = resp.text().await?;
anyhow!("{}: {}", code, text).into_err()?
}
unit_or_err(resp).await?
};

result.map_err(|e| e.to_string())
result.err_to_string()
}
}
3 changes: 3 additions & 0 deletions rs/src/cnb/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod api_base;
pub mod ing;
pub mod oauth;
pub mod post;
pub mod post_category;
pub mod user;
21 changes: 7 additions & 14 deletions rs/src/cnb/oauth/get_token.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::cnb::oauth::OauthReq;
use crate::cnb::oauth::OAUTH_API_BASE_URL;
use crate::http::body_or_err;
use crate::infra::http::{cons_query_string, APPLICATION_X3WFU};
use crate::infra::result::{homo_result_string, HomoResult, IntoResult};
use crate::panic_hook;
use crate::infra::result::{HomoResult, ResultExt};
use crate::{oauth, panic_hook};
use alloc::string::String;
use alloc::{format, vec};
use anyhow::{anyhow, Result};
use anyhow::Result;
use reqwest::header::CONTENT_TYPE;
use wasm_bindgen::prelude::*;

Expand All @@ -19,7 +19,7 @@ impl OauthReq {
callback_url: &str,
) -> HomoResult<String> {
panic_hook!();
let url = format!("{OAUTH_API_BASE_URL}/connect/token");
let url = oauth!("/connect/token");

let client = reqwest::Client::new().post(url);

Expand All @@ -38,16 +38,9 @@ impl OauthReq {

let result: Result<String> = try {
let resp = req.send().await?;
let code = resp.status();

if code.is_success() {
resp.text().await?
} else {
let text = resp.text().await?;
anyhow!("{}: {}", code, text).into_err()?
}
body_or_err(resp).await?
};

homo_result_string(result)
result.homo_string()
}
}
2 changes: 0 additions & 2 deletions rs/src/cnb/oauth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use crate::panic_hook;
use alloc::string::String;
use wasm_bindgen::prelude::*;

const OAUTH_API_BASE_URL: &str = "https://oauth.cnblogs.com";

#[wasm_bindgen(js_name = OauthReq)]
pub struct OauthReq {
client_id: String,
Expand Down
22 changes: 8 additions & 14 deletions rs/src/cnb/oauth/revoke_token.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::cnb::oauth::OauthReq;
use crate::cnb::oauth::OAUTH_API_BASE_URL;
use crate::http::unit_or_err;
use crate::infra::http::{cons_query_string, APPLICATION_X3WFU};
use crate::infra::result::IntoResult;
use crate::{basic, panic_hook};
use alloc::string::{String, ToString};
use crate::infra::result::ResultExt;
use crate::{basic, oauth, panic_hook};
use alloc::string::String;
use alloc::{format, vec};
use anyhow::{anyhow, Result};
use anyhow::Result;
use base64::engine::general_purpose;
use base64::Engine;
use core::ops::Not;
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
use wasm_bindgen::prelude::*;

Expand All @@ -19,7 +18,7 @@ impl OauthReq {
panic_hook!();
let credentials = format!("{}:{}", self.client_id, self.client_secret);
let credentials = general_purpose::STANDARD.encode(credentials);
let url = format!("{OAUTH_API_BASE_URL}/connect/revocation");
let url = oauth!("/connect/revocation");

let client = reqwest::Client::new().post(url);

Expand All @@ -36,14 +35,9 @@ impl OauthReq {

let result: Result<()> = try {
let resp = req.send().await?;
let code = resp.status();

if code.is_success().not() {
let text = resp.text().await?;
anyhow!("{}: {}", code, text).into_err()?
}
unit_or_err(resp).await?
};

result.map_err(|e| e.to_string())
result.err_to_string()
}
}
29 changes: 29 additions & 0 deletions rs/src/cnb/post/del_one.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::cnb::post::PostReq;
use crate::http::unit_or_err;
use crate::infra::http::setup_auth;
use crate::infra::result::ResultExt;
use crate::{blog_backend, panic_hook};
use alloc::format;
use alloc::string::String;
use anyhow::Result;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(js_class = PostReq)]
impl PostReq {
#[wasm_bindgen(js_name = delOne)]
pub async fn export_del_one(&self, post_id: usize) -> Result<(), String> {
panic_hook!();
let url = blog_backend!("/posts/{}", post_id);

let client = reqwest::Client::new().delete(url);

let req = setup_auth(client, &self.token, self.is_pat_token);

let result: Result<()> = try {
let resp = req.send().await?;
unit_or_err(resp).await?
};

result.err_to_string()
}
}
Loading

0 comments on commit 33d7836

Please sign in to comment.