diff --git a/src/download.rs b/src/download.rs index 01457f4..2075af1 100644 --- a/src/download.rs +++ b/src/download.rs @@ -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, id: u64) -> Result<(), PixivDownloaderError> { + pub async fn download_artwork(&self, pw: Arc, 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 = 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!( "{} {} -> {}", diff --git a/src/downloader/tasks.rs b/src/downloader/tasks.rs index fc3c2f6..c621666 100644 --- a/src/downloader/tasks.rs +++ b/src/downloader/tasks.rs @@ -44,10 +44,10 @@ pub async fn create_download_tasks_simple 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); } diff --git a/src/pixiv_web.rs b/src/pixiv_web.rs index f38a791..144e1c9 100644 --- a/src/pixiv_web.rs +++ b/src/pixiv_web.rs @@ -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(&self, url: U) -> Option { + pub async fn download_image(&self, url: U) -> Option { 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(&self, url: U, pdf: &Option) -> Option { 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 { + pub async fn get_artwork_ajax(&self, id: u64) -> Option { 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 { + pub async fn get_artwork(&self, id: u64) -> Option { 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 { + pub async fn get_illust_pages(&self, id: u64) -> Option { 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 { + pub async fn get_ugoira(&self, id: u64) -> Option { 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; } diff --git a/src/webclient.rs b/src/webclient.rs index e65cdfc..c67d11c 100644 --- a/src/webclient.rs +++ b/src/webclient.rs @@ -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(&self, url: U, param: J, headers: H) -> Option { + pub async fn get_with_param(&self, url: U, param: J, headers: H) -> Option { 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(&self, url: U, headers: H) -> Option { - 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 seconds.").replace("", format!("{}", t.as_secs_f64()).as_str()).as_str()); - spin_on(tokio::time::sleep(t)); - } - } - println!("{}", gettext("Retry times now.").replace("", format!("{}", count).as_str()).as_str()); - } - } - None - } - - /// Send Get Requests - pub async fn aget(&self, url: U, headers: H) -> Option { + pub async fn get(&self, url: U, headers: H) -> Option { 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(&self, url: U, headers: H) -> Option { - 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(&self, url: U, headers: H) -> Option { let r = self._aget(url, headers);