mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
Declare the main function as async
This commit is contained in:
@@ -31,13 +31,13 @@ use std::path::PathBuf;
|
||||
use std::sync::Arc;
|
||||
|
||||
impl Main {
|
||||
pub fn download(&mut self) -> i32 {
|
||||
pub async fn download(&mut self) -> i32 {
|
||||
let pw = Arc::new(PixivWebClient::new());
|
||||
if !pw.init() {
|
||||
println!("{}", gettext("Failed to initialize pixiv web api client."));
|
||||
return 1;
|
||||
}
|
||||
if !pw.check_login() {
|
||||
if !pw.check_login().await {
|
||||
return 1;
|
||||
}
|
||||
if !pw.logined() {
|
||||
@@ -49,7 +49,7 @@ impl Main {
|
||||
for id in self.cmd.as_ref().unwrap().ids.iter() {
|
||||
match id {
|
||||
PixivID::Artwork(id) => {
|
||||
let r = self.download_artwork(Arc::clone(&pw), id.clone());
|
||||
let r = self.download_artwork(Arc::clone(&pw), id.clone()).await;
|
||||
let r = if r.is_ok() {
|
||||
0
|
||||
} else {
|
||||
@@ -124,19 +124,19 @@ impl Main {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn download_artwork(&self, pw: Arc<PixivWebClient>, id: u64) -> Result<(), PixivDownloaderError> {
|
||||
pub async fn download_artwork(&self, pw: Arc<PixivWebClient>, id: u64) -> Result<(), PixivDownloaderError> {
|
||||
let mut re = None;
|
||||
let pages;
|
||||
let mut ajax_ver = true;
|
||||
let helper = get_helper();
|
||||
if helper.use_webpage() {
|
||||
re = pw.get_artwork(id);
|
||||
re = pw.get_artwork(id).await;
|
||||
if re.is_some() {
|
||||
ajax_ver = false;
|
||||
}
|
||||
}
|
||||
if re.is_none() {
|
||||
re = pw.get_artwork_ajax(id);
|
||||
re = pw.get_artwork_ajax(id).await;
|
||||
}
|
||||
let re = re.try_err(gettext("Failed to get artwork's data."))?;
|
||||
if ajax_ver {
|
||||
@@ -147,7 +147,7 @@ impl Main {
|
||||
let pages = pages.try_err(gettext("Failed to get page count."))?;
|
||||
let mut pages_data: Option<JsonValue> = None;
|
||||
if pages > 1 {
|
||||
pages_data = pw.get_illust_pages(id);
|
||||
pages_data = pw.get_illust_pages(id).await;
|
||||
}
|
||||
if pages > 1 && pages_data.is_none() {
|
||||
return Err(PixivDownloaderError::from(gettext("Failed to get pages' data.")));
|
||||
@@ -175,7 +175,7 @@ impl Main {
|
||||
match illust_type {
|
||||
0 => { }
|
||||
2 => {
|
||||
let ugoira_data = pw.get_ugoira(id).try_err(gettext("Failed to get ugoira's data."))?;
|
||||
let ugoira_data = pw.get_ugoira(id).await.try_err(gettext("Failed to get ugoira's data."))?;
|
||||
let src = (&ugoira_data["originalSrc"]).as_str().try_err(gettext("Can not find source link for ugoira."))?;
|
||||
let file_name = get_file_name_from_url(src).try_err(format!("{} {}", gettext("Failed to get file name from url:"), src))?;
|
||||
let file_name = base.join(file_name);
|
||||
@@ -188,7 +188,7 @@ impl Main {
|
||||
true
|
||||
};
|
||||
if dw {
|
||||
let r = pw.download_image(src).try_err(format!("{} {}", gettext("Failed to download ugoira:"), src))?;
|
||||
let r = pw.download_image(src).await.try_err(format!("{} {}", gettext("Failed to download ugoira:"), src))?;
|
||||
WebClient::download_stream(&file_name, r).try_err(format!("{} {}", gettext("Failed to download ugoira:"), src))?;
|
||||
println!(
|
||||
"{} {} -> {}",
|
||||
@@ -282,7 +282,7 @@ impl Main {
|
||||
}
|
||||
}
|
||||
}
|
||||
let r = pw.download_image(link).try_err(format!("{} {}", gettext("Failed to download image:"), link))?;
|
||||
let r = pw.download_image(link).await.try_err(format!("{} {}", gettext("Failed to download image:"), link))?;
|
||||
WebClient::download_stream(&file_name, r).try_err(format!("{} {}", gettext("Failed to download image:"), link))?;
|
||||
println!(
|
||||
"{} {} -> {}",
|
||||
@@ -333,7 +333,7 @@ impl Main {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
let r = pw.download_image(link).try_err(format!("{} {}", gettext("Failed to download image:"), link))?;
|
||||
let r = pw.download_image(link).await.try_err(format!("{} {}", gettext("Failed to download image:"), link))?;
|
||||
WebClient::download_stream(&file_name, r).try_err(format!("{} {}", gettext("Failed to download image:"), link))?;
|
||||
println!(
|
||||
"{} {} -> {}",
|
||||
|
||||
@@ -44,10 +44,10 @@ pub async fn create_download_tasks_simple<T: Seek + Write + Send + Sync + ClearF
|
||||
if start != 0 {
|
||||
headers.insert(String::from("Range"), format!("bytes={}-", start));
|
||||
}
|
||||
let mut result = d.client.aget(d.url.deref().clone(), headers).await.try_err(gettext("Failed to get url."))?;
|
||||
let mut result = d.client.get(d.url.deref().clone(), headers).await.try_err(gettext("Failed to get url."))?;
|
||||
let mut status = result.status();
|
||||
if status == 416 {
|
||||
result = d.client.aget(d.url.deref().clone(), d.headers.deref().clone()).await.try_err(gettext("Failed to get url."))?;
|
||||
result = d.client.get(d.url.deref().clone(), d.headers.deref().clone()).await.try_err(gettext("Failed to get url."))?;
|
||||
status = result.status();
|
||||
} else if status == 206 {
|
||||
let headers = result.headers();
|
||||
@@ -80,7 +80,7 @@ pub async fn create_download_tasks_simple<T: Seek + Write + Send + Sync + ClearF
|
||||
if need_reget {
|
||||
d.pd.clear()?;
|
||||
d.clear_file()?;
|
||||
result = d.client.get(d.url.deref().clone(), d.headers.deref().clone()).try_err(gettext("Failed to get url."))?;
|
||||
result = d.client.get(d.url.deref().clone(), d.headers.deref().clone()).await.try_err(gettext("Failed to get url."))?;
|
||||
status = result.status();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ impl Main {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> i32 {
|
||||
pub async fn run(&mut self) -> i32 {
|
||||
self.cmd = opts::parse_cmd();
|
||||
if self.cmd.is_none() {
|
||||
return 1;
|
||||
@@ -145,7 +145,7 @@ impl Main {
|
||||
self.deal_config_cmd();
|
||||
}
|
||||
Command::Download => {
|
||||
return self.download();
|
||||
return self.download().await;
|
||||
}
|
||||
Command::None => {
|
||||
return 0;
|
||||
@@ -158,5 +158,5 @@ impl Main {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let mut m = Main::new();
|
||||
std::process::exit(m.run());
|
||||
std::process::exit(m.run().await);
|
||||
}
|
||||
|
||||
@@ -101,9 +101,9 @@ impl PixivWebClient {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn check_login(&self) -> bool {
|
||||
pub async fn check_login(&self) -> bool {
|
||||
self.auto_init();
|
||||
let r = self.client.get_with_param("https://www.pixiv.net/", self.params.get_ref(), None);
|
||||
let r = self.client.get_with_param("https://www.pixiv.net/", self.params.get_ref(), None).await;
|
||||
if r.is_none() {
|
||||
return false;
|
||||
}
|
||||
@@ -181,9 +181,9 @@ impl PixivWebClient {
|
||||
Some(body.clone())
|
||||
}
|
||||
|
||||
pub fn download_image<U: IntoUrl + Clone>(&self, url: U) -> Option<Response> {
|
||||
pub async fn download_image<U: IntoUrl + Clone>(&self, url: U) -> Option<Response> {
|
||||
self.auto_init();
|
||||
let r = self.client.get(url, json::object!{"referer": "https://www.pixiv.net/"});
|
||||
let r = self.client.get(url, json::object!{"referer": "https://www.pixiv.net/"}).await;
|
||||
if r.is_none() {
|
||||
return None;
|
||||
}
|
||||
@@ -199,7 +199,7 @@ impl PixivWebClient {
|
||||
|
||||
pub async fn adownload_image<U: IntoUrl + Clone>(&self, url: U, pdf: &Option<PdFile>) -> Option<Response> {
|
||||
self.auto_init();
|
||||
let r = self.client.aget(url, json::object!{"referer": "https://www.pixiv.net/"}).await;
|
||||
let r = self.client.get(url, json::object!{"referer": "https://www.pixiv.net/"}).await;
|
||||
if r.is_none() {
|
||||
return None;
|
||||
}
|
||||
@@ -213,9 +213,9 @@ impl PixivWebClient {
|
||||
Some(r)
|
||||
}
|
||||
|
||||
pub fn get_artwork_ajax(&self, id: u64) -> Option<JsonValue> {
|
||||
pub async fn get_artwork_ajax(&self, id: u64) -> Option<JsonValue> {
|
||||
self.auto_init();
|
||||
let r = self.client.get_with_param(format!("https://www.pixiv.net/ajax/illust/{}", id), self.params.get_ref(), None);
|
||||
let r = self.client.get_with_param(format!("https://www.pixiv.net/ajax/illust/{}", id), self.params.get_ref(), None).await;
|
||||
if r.is_none() {
|
||||
return None;
|
||||
}
|
||||
@@ -227,9 +227,9 @@ impl PixivWebClient {
|
||||
v
|
||||
}
|
||||
|
||||
pub fn get_artwork(&self, id: u64) -> Option<JsonValue> {
|
||||
pub async fn get_artwork(&self, id: u64) -> Option<JsonValue> {
|
||||
self.auto_init();
|
||||
let r = self.client.get_with_param(format!("https://www.pixiv.net/artworks/{}", id), self.params.get_ref(), None);
|
||||
let r = self.client.get_with_param(format!("https://www.pixiv.net/artworks/{}", id), self.params.get_ref(), None).await;
|
||||
if r.is_none() {
|
||||
return None;
|
||||
}
|
||||
@@ -257,9 +257,9 @@ impl PixivWebClient {
|
||||
Some(p.value.unwrap())
|
||||
}
|
||||
|
||||
pub fn get_illust_pages(&self, id: u64) -> Option<JsonValue> {
|
||||
pub async fn get_illust_pages(&self, id: u64) -> Option<JsonValue> {
|
||||
self.auto_init();
|
||||
let r = self.client.get_with_param(format!("https://www.pixiv.net/ajax/illust/{}/pages", id), self.params.get_ref(), None);
|
||||
let r = self.client.get_with_param(format!("https://www.pixiv.net/ajax/illust/{}/pages", id), self.params.get_ref(), None).await;
|
||||
if r.is_none() {
|
||||
return None;
|
||||
}
|
||||
@@ -271,9 +271,9 @@ impl PixivWebClient {
|
||||
v
|
||||
}
|
||||
|
||||
pub fn get_ugoira(&self, id: u64) -> Option<JsonValue> {
|
||||
pub async fn get_ugoira(&self, id: u64) -> Option<JsonValue> {
|
||||
self.auto_init();
|
||||
let r = self.client.get_with_param(format!("https://www.pixiv.net/ajax/illust/{}/ugoira_meta", id), self.params.get_ref(), None);
|
||||
let r = self.client.get_with_param(format!("https://www.pixiv.net/ajax/illust/{}/ugoira_meta", id), self.params.get_ref(), None).await;
|
||||
if r.is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
@@ -293,7 +293,7 @@ impl WebClient {
|
||||
/// client.get_with_param("https://test.com/a", json::array![["daa", "param1"]], None);
|
||||
/// ```
|
||||
/// It will GET `https://test.com/a?data=param1`, `https://test.com/a?daa=%7B%22ad%22%3A%22test%22%7D`, `https://test.com/a?daa=param1`
|
||||
pub fn get_with_param<U: IntoUrl + Clone, J: ToJson, H: ToHeaders + Clone>(&self, url: U, param: J, headers: H) -> Option<Response> {
|
||||
pub async fn get_with_param<U: IntoUrl + Clone, J: ToJson, H: ToHeaders + Clone>(&self, url: U, param: J, headers: H) -> Option<Response> {
|
||||
let u = url.into_url();
|
||||
if u.is_err() {
|
||||
println!("{} \"{}\"", gettext("Can not parse URL:"), u.unwrap_err());
|
||||
@@ -302,7 +302,7 @@ impl WebClient {
|
||||
let mut u = u.unwrap();
|
||||
let obj = param.to_json();
|
||||
if obj.is_none() {
|
||||
return self.get(u, headers);
|
||||
return self.get(u, headers).await;
|
||||
}
|
||||
let obj = obj.unwrap();
|
||||
if !obj.is_object() && !obj.is_array() {
|
||||
@@ -356,36 +356,11 @@ impl WebClient {
|
||||
}
|
||||
}
|
||||
}
|
||||
self.get(u.as_str(), headers)
|
||||
self.get(u.as_str(), headers).await
|
||||
}
|
||||
|
||||
/// Send Get Requests
|
||||
pub fn get<U: IntoUrl + Clone, H: ToHeaders + Clone>(&self, url: U, headers: H) -> Option<Response> {
|
||||
let mut count = 0u64;
|
||||
let retry = self.get_retry();
|
||||
while count <= retry {
|
||||
let r = self._get(url.clone(), headers.clone());
|
||||
if r.is_some() {
|
||||
return r;
|
||||
}
|
||||
count += 1;
|
||||
if count <= retry {
|
||||
let ri = self.get_retry_interval();
|
||||
if ri.is_some() {
|
||||
let t = ri.as_ref().unwrap()[(count - 1).try_into().unwrap()];
|
||||
if !t.is_zero() {
|
||||
println!("{}", gettext("Retry after <num> seconds.").replace("<num>", format!("{}", t.as_secs_f64()).as_str()).as_str());
|
||||
spin_on(tokio::time::sleep(t));
|
||||
}
|
||||
}
|
||||
println!("{}", gettext("Retry <count> times now.").replace("<count>", format!("{}", count).as_str()).as_str());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Send Get Requests
|
||||
pub async fn aget<U: IntoUrl + Clone, H: ToHeaders + Clone>(&self, url: U, headers: H) -> Option<Response> {
|
||||
pub async fn get<U: IntoUrl + Clone, H: ToHeaders + Clone>(&self, url: U, headers: H) -> Option<Response> {
|
||||
let mut count = 0u64;
|
||||
let retry = self.get_retry();
|
||||
while count <= retry {
|
||||
@@ -406,26 +381,6 @@ impl WebClient {
|
||||
None
|
||||
}
|
||||
|
||||
/// Send GET requests without retry
|
||||
pub fn _get<U: IntoUrl, H: ToHeaders>(&self, url: U, headers: H) -> Option<Response> {
|
||||
let r = self._aget(url, headers);
|
||||
let r = r.send();
|
||||
let r = spin_on(r);
|
||||
match r {
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
println!("{} {}", gettext("Error when request:"), e);
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let r = r.unwrap();
|
||||
self.handle_set_cookie(&r);
|
||||
if self.get_verbose() {
|
||||
println!("{}", r.status());
|
||||
}
|
||||
Some(r)
|
||||
}
|
||||
|
||||
/// Send GET requests without retry
|
||||
pub async fn _aget2<U: IntoUrl, H: ToHeaders>(&self, url: U, headers: H) -> Option<Response> {
|
||||
let r = self._aget(url, headers);
|
||||
|
||||
Reference in New Issue
Block a user