mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
add option use_webpage
This commit is contained in:
@@ -37,22 +37,30 @@ impl PixivData {
|
||||
pub fn from_web_page_data(&mut self, value: &JsonValue, allow_overwrite: bool) {
|
||||
let id: u64 = (&self.id).try_into().unwrap();
|
||||
let ids = format!("{}", id);
|
||||
self.from_web_page_ajax_data(&value["illust"][ids.as_str()], allow_overwrite)
|
||||
}
|
||||
|
||||
/// Read data from JSON object.
|
||||
/// The object is from `https://www.pixiv.net/illust/ajax/<id>`
|
||||
/// * `value` - The JSON object
|
||||
/// * `allow_overwrite` - Allow overwrite the data existing.
|
||||
pub fn from_web_page_ajax_data(&mut self, value: &JsonValue, allow_overwrite: bool) {
|
||||
if self.title.is_none() || allow_overwrite {
|
||||
let title = value["illust"][ids.as_str()]["illustTitle"].as_str();
|
||||
let title = value["illustTitle"].as_str();
|
||||
if title.is_some() {
|
||||
self.title = Some(String::from(title.unwrap()));
|
||||
}
|
||||
}
|
||||
if self.author.is_none() || allow_overwrite {
|
||||
let author = value["illust"][ids.as_str()]["userName"].as_str();
|
||||
let author = value["userName"].as_str();
|
||||
if author.is_some() {
|
||||
self.author = Some(String::from(author.unwrap()));
|
||||
}
|
||||
}
|
||||
if self.description.is_none() || allow_overwrite {
|
||||
let mut description = value["illust"][ids.as_str()]["description"].as_str();
|
||||
let mut description = value["description"].as_str();
|
||||
if description.is_none() {
|
||||
description = value["illust"][ids.as_str()]["illustComment"].as_str();
|
||||
description = value["illustComment"].as_str();
|
||||
}
|
||||
if description.is_some() {
|
||||
let re = unescape(description.unwrap());
|
||||
|
||||
@@ -41,12 +41,24 @@ impl Main {
|
||||
}
|
||||
|
||||
pub fn download_artwork(&self, pw: &mut PixivWebClient, id: u64) -> i32 {
|
||||
let re = pw.get_artwork(id);
|
||||
let mut re = None;
|
||||
let pages;
|
||||
let mut ajax_ver = true;
|
||||
if pw.helper.use_webpage() {
|
||||
re = pw.get_artwork(id);
|
||||
if re.is_some() {
|
||||
ajax_ver = false;
|
||||
}
|
||||
}
|
||||
if re.is_none() {
|
||||
return 1;
|
||||
re = pw.get_artwork_ajax(id);
|
||||
}
|
||||
let re = re.unwrap();
|
||||
let pages = (&re["illust"][format!("{}", id).as_str()]["pageCount"]).as_u64();
|
||||
if ajax_ver {
|
||||
pages = (&re["pageCount"]).as_u64();
|
||||
} else {
|
||||
pages = (&re["illust"][format!("{}", id).as_str()]["pageCount"]).as_u64();
|
||||
}
|
||||
if pages.is_none() {
|
||||
println!("{}", gettext("Failed to get page count."));
|
||||
return 1;
|
||||
@@ -63,7 +75,11 @@ impl Main {
|
||||
let base = PathBuf::from(".");
|
||||
let json_file = base.join(format!("{}.json", id));
|
||||
let mut datas = PixivData::new(id).unwrap();
|
||||
datas.from_web_page_data(&re, true);
|
||||
if ajax_ver {
|
||||
datas.from_web_page_ajax_data(&re, true);
|
||||
} else {
|
||||
datas.from_web_page_data(&re, true);
|
||||
}
|
||||
let json_data = JSONDataFile::from(&datas);
|
||||
if !json_data.save(&json_file) {
|
||||
println!("{}", gettext("Failed to save metadata to JSON file."));
|
||||
@@ -117,7 +133,11 @@ impl Main {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let link = (&re["illust"][format!("{}", id)]["urls"]["original"]).as_str();
|
||||
let link = if ajax_ver {
|
||||
(&re["urls"]["original"]).as_str()
|
||||
} else {
|
||||
(&re["illust"][format!("{}", id)]["urls"]["original"]).as_str()
|
||||
};
|
||||
if link.is_none() {
|
||||
println!("{}", gettext("Failed to get original picture's link."));
|
||||
return 1;
|
||||
|
||||
@@ -78,4 +78,15 @@ impl<'a> OptHelper<'a> {
|
||||
}
|
||||
self.default_retry_interval.clone()
|
||||
}
|
||||
|
||||
/// Return whether to use data from webpage first.
|
||||
pub fn use_webpage(&self) -> bool {
|
||||
if self.opt.use_webpage {
|
||||
return true;
|
||||
}
|
||||
if self.settings.have_bool("use-webpage") {
|
||||
return self.settings.get_bool("use-webpage").unwrap();
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,8 @@ pub struct CommandOpts {
|
||||
pub retry: Option<u64>,
|
||||
/// Retry interval
|
||||
pub retry_interval: Option<NonTailList<Duration>>,
|
||||
/// Use data from webpage first
|
||||
pub use_webpage: bool,
|
||||
}
|
||||
|
||||
impl CommandOpts {
|
||||
@@ -72,6 +74,7 @@ impl CommandOpts {
|
||||
overwrite: None,
|
||||
retry: None,
|
||||
retry_interval: None,
|
||||
use_webpage: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,6 +157,7 @@ pub fn parse_cmd() -> Option<CommandOpts> {
|
||||
gettext("The interval (in seconds) between two retries."),
|
||||
"LIST",
|
||||
);
|
||||
opts.optflag("", "use-webpage", gettext("Use data from webpage first."));
|
||||
let result = match opts.parse(&argv[1..]) {
|
||||
Ok(m) => m,
|
||||
Err(err) => {
|
||||
@@ -260,5 +264,6 @@ pub fn parse_cmd() -> Option<CommandOpts> {
|
||||
}
|
||||
re.as_mut().unwrap().retry_interval = Some(r.unwrap());
|
||||
}
|
||||
re.as_mut().unwrap().use_webpage = result.opt_present("use-webpage");
|
||||
re
|
||||
}
|
||||
|
||||
@@ -156,6 +156,20 @@ impl<'a> PixivWebClient<'a> {
|
||||
Some(r)
|
||||
}
|
||||
|
||||
pub fn get_artwork_ajax(&mut self, id: u64) -> Option<JsonValue> {
|
||||
self.auto_init();
|
||||
let r = self.client.get(format!("https://www.pixiv.net/ajax/illust/{}", id), None);
|
||||
if r.is_none() {
|
||||
return None;
|
||||
}
|
||||
let r = r.unwrap();
|
||||
let v = self.deal_json(r);
|
||||
if self.helper.verbose() && v.is_some() {
|
||||
println!("{} {}", gettext("Artwork's data:"), v.as_ref().unwrap().pretty(2));
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
pub fn get_artwork(&mut self, id: u64) -> Option<JsonValue> {
|
||||
self.auto_init();
|
||||
let r = self.client.get(format!("https://www.pixiv.net/artworks/{}", id), None);
|
||||
@@ -194,7 +208,7 @@ impl<'a> PixivWebClient<'a> {
|
||||
}
|
||||
let r = r.unwrap();
|
||||
let v = self.deal_json(r);
|
||||
if self.helper.verbose() {
|
||||
if self.helper.verbose() && v.is_some() {
|
||||
println!("{} {}", gettext("Artwork's page data:"), v.as_ref().unwrap().pretty(2));
|
||||
}
|
||||
v
|
||||
|
||||
@@ -292,6 +292,13 @@ impl SettingStore {
|
||||
self.data.get(key)
|
||||
}
|
||||
|
||||
pub fn get_bool(&self, key: &str) -> Option<bool> {
|
||||
match self.data.get(key) {
|
||||
Some(obj) => { obj.as_bool() }
|
||||
None => { None }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_str(&self, key: &str) -> Option<String> {
|
||||
let obj = self.data.get(key);
|
||||
if obj.is_none() {
|
||||
@@ -309,6 +316,13 @@ impl SettingStore {
|
||||
self.data.have(key)
|
||||
}
|
||||
|
||||
pub fn have_bool(&self, key: &str) -> bool {
|
||||
match self.data.get(key) {
|
||||
Some(obj) => { obj.is_boolean() }
|
||||
None => { false }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn have_str(&self, key: &str) -> bool {
|
||||
let obj = self.data.get(key);
|
||||
if obj.is_none() {
|
||||
|
||||
@@ -11,6 +11,7 @@ pub fn get_settings_list() -> Vec<SettingDes> {
|
||||
SettingDes::new("language", gettext("The language of translated tags."), JsonValueType::Str, None).unwrap(),
|
||||
SettingDes::new("retry", gettext("Max retry count if request failed."), JsonValueType::Number, Some(check_u64)).unwrap(),
|
||||
SettingDes::new("retry-interval", gettext("The interval (in seconds) between two retries."), JsonValueType::Multiple, Some(check_retry_interval)).unwrap(),
|
||||
SettingDes::new("use-webpage", gettext("Use data from webpage first."), JsonValueType::Boolean, None).unwrap(),
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user