Skip to content

Commit

Permalink
Add new settings add-history
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc authored Oct 8, 2023
1 parent 252ea9e commit eaa9d11
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ regex = "1"
reqwest = { version = "0.11", features = ["brotli", "deflate", "gzip", "socks", "stream"] }
rusqlite = { version = "0.29", features = ["bundled", "chrono"], optional = true }
RustyXML = "0.3"
serde = "1"
tokio = { version = "1.27", features = ["rt", "macros", "rt-multi-thread", "time"] }
url = "2.3"
urlparse = "0.7"
Expand Down
11 changes: 11 additions & 0 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,10 @@ pub async fn download_artwork_app(
let json_file = base.join(format!("{}.json", id));
let mut datas = PixivData::new(id).unwrap();
datas.from_app_illust(&data);
let mut web_used = false;
if data.caption_is_empty() && helper.use_web_description() {
if let Some(data) = pw.get_artwork_ajax(id).await {
web_used = true;
if let Some(desc) = data["description"]
.as_str()
.or_else(|| data["illustComment"].as_str())
Expand All @@ -396,6 +398,15 @@ pub async fn download_artwork_app(
}
}
}
if helper.add_history() && !web_used {
if let Err(e) = ac.add_illust_to_browsing_history(vec![id]).await {
println!(
"{} {}",
gettext("Warning: Failed to add artwork to history:"),
e
);
}
}
let datas = Arc::new(datas);
let json_data = JSONDataFile::from(Arc::clone(&datas));
if !json_data.save(&json_file) {
Expand Down
12 changes: 12 additions & 0 deletions src/opthelper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ pub struct OptHelper {
}

impl OptHelper {
/// Whether to add artworks to pixiv's history. Only works for APP API.
pub fn add_history(&self) -> bool {
if self.opt.get_ref().add_history.is_some() {
return self.opt.get_ref().add_history.unwrap();
}
if self.settings.get_ref().have_bool("add-history") {
return self.settings.get_ref().get_bool("add-history").unwrap();
}
false
}

/// return author name filters
pub fn author_name_filters<'a>(&'a self) -> Option<RwLockReadGuard<'a, Vec<AuthorNameFilter>>> {
if self.settings.get_ref().have("author-name-filters") {
return Some(self._author_name_filters.get_ref());
Expand Down
30 changes: 30 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ pub struct CommandOpts {
pub use_app_api: Option<bool>,
/// Whether to use description from Web API when description from APP API is empty.
pub use_web_description: Option<bool>,
/// Whether to add artworks to pixiv's history. Only works for APP API.
pub add_history: Option<bool>,
}

impl CommandOpts {
Expand Down Expand Up @@ -174,6 +176,7 @@ impl CommandOpts {
refresh_token: None,
use_app_api: None,
use_web_description: None,
add_history: None,
}
}

Expand Down Expand Up @@ -645,6 +648,20 @@ pub fn parse_cmd() -> Option<CommandOpts> {
HasArg::Maybe,
getopts::Occur::Optional,
);
opts.opt(
"",
"add-history",
format!(
"{} ({} {})",
gettext("Whether to add artworks to pixiv's history. Only works for APP API."),
gettext("Default:"),
"yes"
)
.as_str(),
"yes/no",
HasArg::Maybe,
getopts::Occur::Optional,
);
let result = match opts.parse(&argv[1..]) {
Ok(m) => m,
Err(err) => {
Expand Down Expand Up @@ -1033,6 +1050,19 @@ pub fn parse_cmd() -> Option<CommandOpts> {
return None;
}
}
match parse_optional_opt(&result, "add-history", true, parse_bool) {
Ok(b) => re.as_mut().unwrap().add_history = b,
Err(e) => {
println!(
"{} {}",
gettext("Failed to parse <opt>:")
.replace("<opt>", "add-history")
.as_str(),
e
);
return None;
}
}
re
}

Expand Down
19 changes: 19 additions & 0 deletions src/pixiv_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,25 @@ impl PixivAppClientInternal {
}
Ok(obj)
}

pub async fn add_illust_to_browsing_history(
&self,
ids: Vec<u64>,
) -> Result<(), PixivDownloaderError> {
self.auto_handle().await?;
let params: Vec<_> = ids.iter().map(|id| ("illust_ids[]", id)).collect();
let re = self
.client
.post(
"https://app-api.pixiv.net/v2/user/browsing-history/illust/add",
None,
Some(params),
)
.await
.ok_or("Failed to add illust to browsing history.")?;
handle_error(re).await?;
Ok(())
}
}

#[derive(Clone)]
Expand Down
46 changes: 28 additions & 18 deletions src/push/every_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ pub enum EveryPushTextType {
Markdown,
}

impl std::fmt::Display for EveryPushTextType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
impl AsRef<str> for EveryPushTextType {
fn as_ref(&self) -> &str {
match self {
EveryPushTextType::Text => write!(f, "text"),
EveryPushTextType::Image => write!(f, "image"),
EveryPushTextType::Markdown => write!(f, "markdown"),
EveryPushTextType::Text => "text",
EveryPushTextType::Image => "image",
EveryPushTextType::Markdown => "markdown",
}
}
}

impl std::fmt::Display for EveryPushTextType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", AsRef::<str>::as_ref(self))
}
}

pub struct EveryPushClient {
client: WebClient,
server: String,
Expand All @@ -39,22 +45,26 @@ impl EveryPushClient {
/// * `typ` - text type
///
/// For more information, see [API document](https://github.com/PeanutMelonSeedBigAlmond/EveryPush.Server/blob/main/api.md#推送消息)
pub async fn push_message(
pub async fn push_message<
P: AsRef<str> + ?Sized,
T: AsRef<str> + ?Sized,
I: AsRef<str> + ?Sized,
>(
&self,
push_token: String,
text: String,
title: Option<String>,
push_token: &P,
text: &T,
title: Option<&I>,
typ: Option<EveryPushTextType>,
) -> Result<(), String> {
let mut params: HashMap<String, String> = HashMap::new();
params.insert(String::from("pushToken"), push_token);
params.insert(String::from("text"), text);
let mut params = HashMap::new();
params.insert("pushToken", push_token.as_ref());
params.insert("text", text.as_ref());
match title {
Some(t) => params.insert(String::from("title"), t),
Some(t) => params.insert("title", t.as_ref()),
None => None,
};
match typ {
Some(t) => params.insert(String::from("type"), format!("{}", t)),
match &typ {
Some(t) => params.insert("type", t.as_ref()),
None => None,
};
let re = self
Expand Down Expand Up @@ -92,9 +102,9 @@ async fn test_every_push_push() {
let client = EveryPushClient::new(server);
match client
.push_message(
token,
String::from("Push Test"),
Some(String::from("Push")),
&token,
"Push Test",
Some("Push"),
Some(EveryPushTextType::Text),
)
.await
Expand Down
58 changes: 35 additions & 23 deletions src/push/pushdeer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ impl PushdeerClient {
/// push text message to server
/// * `pushkey` - push key
/// * `text` - text
pub async fn push_text_message(&self, pushkey: String, text: String) -> Result<(), String> {
let mut params: HashMap<String, String> = HashMap::new();
params.insert(String::from("pushkey"), pushkey);
params.insert(String::from("text"), text);
pub async fn push_text_message<P: AsRef<str> + ?Sized, T: AsRef<str> + ?Sized>(
&self,
pushkey: &P,
text: &T,
) -> Result<(), String> {
let mut params = HashMap::new();
params.insert("pushkey", pushkey.as_ref());
params.insert("text", text.as_ref());
let re = self
.client
.post(format!("{}/message/push", self.server), None, Some(params))
Expand All @@ -53,11 +57,15 @@ impl PushdeerClient {
/// push image message to server
/// * `pushkey` - push key
/// * `image` - image URL
pub async fn push_image(&self, pushkey: String, image: String) -> Result<(), String> {
let mut params: HashMap<String, String> = HashMap::new();
params.insert(String::from("pushkey"), pushkey);
params.insert(String::from("text"), image);
params.insert(String::from("type"), String::from("image"));
pub async fn push_image<P: AsRef<str> + ?Sized, I: AsRef<str> + ?Sized>(
&self,
pushkey: &P,
image: &I,
) -> Result<(), String> {
let mut params = HashMap::new();
params.insert("pushkey", pushkey.as_ref());
params.insert("text", image.as_ref());
params.insert("type", "image");
let re = self
.client
.post(format!("{}/message/push", self.server), None, Some(params))
Expand All @@ -70,17 +78,21 @@ impl PushdeerClient {
/// * `pushkey` - push key
/// * `title` - title
/// * `text` - markdown text
pub async fn push_markdown_message(
pub async fn push_markdown_message<
P: AsRef<str> + ?Sized,
T: AsRef<str> + ?Sized,
E: AsRef<str> + ?Sized,
>(
&self,
pushkey: String,
title: String,
text: String,
pushkey: &P,
title: &T,
text: &E,
) -> Result<(), String> {
let mut params: HashMap<String, String> = HashMap::new();
params.insert(String::from("pushkey"), pushkey);
params.insert(String::from("text"), title);
params.insert(String::from("desp"), text);
params.insert(String::from("type"), String::from("markdown"));
let mut params = HashMap::new();
params.insert("pushkey", pushkey.as_ref());
params.insert("text", title.as_ref());
params.insert("desp", text.as_ref());
params.insert("type", "markdown");
let re = self
.client
.post(format!("{}/message/push", self.server), None, Some(params))
Expand All @@ -92,22 +104,22 @@ impl PushdeerClient {

pushdeer_api_quick_test!(
test_push_text_message,
client.push_text_message(token, String::from("Test message")),
client.push_text_message(&token, "Test message"),
"Failed to send text message:"
);

pushdeer_api_quick_test!(
test_push_image,
client.push_image(token, String::from("https://pd.lifegpc.com/proxy/pixiv/112199539_p0.jpg?url=https%3A%2F%2Fi.pximg.net%2Fimg-original%2Fimg%2F2023%2F10%2F02%2F00%2F00%2F38%2F112199539_p0.jpg&sign=4f429e69218669e226e1171d2499f49ac7f41f56c3a8275619641c0a3327a394dae63dcd145367d66a3735279dc59b39219eb4d4c20bfe5afb7e801a2e7c2496")),
client.push_image(&token, "https://pd.lifegpc.com/proxy/pixiv/112199539_p0.jpg?url=https%3A%2F%2Fi.pximg.net%2Fimg-original%2Fimg%2F2023%2F10%2F02%2F00%2F00%2F38%2F112199539_p0.jpg&sign=4f429e69218669e226e1171d2499f49ac7f41f56c3a8275619641c0a3327a394dae63dcd145367d66a3735279dc59b39219eb4d4c20bfe5afb7e801a2e7c2496"),
"Failed to send image message:"
);

pushdeer_api_quick_test!(
test_push_markdown_message,
client.push_markdown_message(
token,
String::from("Test title"),
String::from("# Test Header\n[link](https://github.com/lifegpc/pixiv_downloader)")
&token,
"Test title",
"# Test Header\n[link](https://github.com/lifegpc/pixiv_downloader)"
),
"Failed to send markdown message:"
);
1 change: 1 addition & 0 deletions src/settings_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn get_settings_list() -> Vec<SettingDes> {
SettingDes::new("cors-allow-all", gettext("Whether to allow all domains to send CORS requests."), JsonValueType::Boolean, None).unwrap(),
SettingDes::new("use-app-api", gettext("Whether to use Pixiv APP API first."), JsonValueType::Boolean, None).unwrap(),
SettingDes::new("use-web-description", gettext("Whether to use description from Web API when description from APP API is empty."), JsonValueType::Boolean, None).unwrap(),
SettingDes::new("add-history", gettext("Whether to add artworks to pixiv's history. Only works for APP API."), JsonValueType::Boolean, None).unwrap(),
]
}

Expand Down
13 changes: 7 additions & 6 deletions src/webclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::opthelper::get_helper;
use json::JsonValue;
use proc_macros::print_error;
use reqwest::{Client, ClientBuilder, IntoUrl, Request, Response};
use serde::ser::Serialize;
use std::collections::HashMap;
use std::default::Default;
use std::sync::atomic::AtomicBool;
Expand Down Expand Up @@ -390,11 +391,11 @@ impl WebClient {
self.handle_req_middlewares(r.build()?)
}

pub async fn post<U: IntoUrl + Clone, H: ToHeaders + Clone>(
pub async fn post<U: IntoUrl + Clone, H: ToHeaders + Clone, S: Serialize + Clone>(
&self,
url: U,
headers: H,
form: Option<HashMap<String, String>>,
form: Option<S>,
) -> Option<Response> {
let mut count = 0i64;
let retry = self.get_retry();
Expand Down Expand Up @@ -429,11 +430,11 @@ impl WebClient {
None
}

pub async fn _apost2<U: IntoUrl, H: ToHeaders>(
pub async fn _apost2<U: IntoUrl, H: ToHeaders, S: Serialize>(
&self,
url: U,
headers: H,
form: Option<HashMap<String, String>>,
form: Option<S>,
) -> Option<Response> {
let r = print_error!(
gettext("Failed to generate request:"),
Expand All @@ -448,11 +449,11 @@ impl WebClient {
}

/// Generate a POST request
pub fn _apost<U: IntoUrl, H: ToHeaders>(
pub fn _apost<U: IntoUrl, H: ToHeaders, S: Serialize>(
&self,
url: U,
headers: H,
form: Option<HashMap<String, String>>,
form: Option<S>,
) -> Result<Request, PixivDownloaderError> {
let s = url.as_str();
if self.get_verbose() {
Expand Down

0 comments on commit eaa9d11

Please sign in to comment.