mirror of
https://github.com/lifegpc/pixiv_downloader.git
synced 2026-07-08 01:32:41 +08:00
add support to output description information
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
use crate::gettext;
|
||||
use crate::pixiv_link::ToPixivID;
|
||||
use crate::pixiv_link::PixivID;
|
||||
use json::JsonValue;
|
||||
use std::convert::TryInto;
|
||||
use xml::unescape;
|
||||
|
||||
/// Pixiv's basic data
|
||||
pub struct PixivData {
|
||||
@@ -11,6 +13,7 @@ pub struct PixivData {
|
||||
pub title: Option<String>,
|
||||
/// The author
|
||||
pub author: Option<String>,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
impl PixivData {
|
||||
@@ -23,6 +26,7 @@ impl PixivData {
|
||||
id: i.unwrap(),
|
||||
title: None,
|
||||
author: None,
|
||||
description: None,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -45,5 +49,20 @@ impl PixivData {
|
||||
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();
|
||||
if description.is_none() {
|
||||
description = value["illust"][ids.as_str()]["illustComment"].as_str();
|
||||
}
|
||||
if description.is_some() {
|
||||
let re = unescape(description.unwrap());
|
||||
match re {
|
||||
Ok(s) => { self.description = Some(s); }
|
||||
Err(s) => {
|
||||
println!("{} {}", gettext("Failed to unescape string:"), s.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ use crate::exif::ExifImage;
|
||||
use crate::exif::ExifKey;
|
||||
use crate::exif::ExifTypeID;
|
||||
use crate::exif::ExifValue;
|
||||
use crate::parser::description::parse_description;
|
||||
use std::convert::TryFrom;
|
||||
use std::ffi::OsStr;
|
||||
use utf16string::LittleEndian;
|
||||
@@ -48,12 +49,31 @@ fn add_image_author(data: &mut ExifData, d: &PixivData) -> Result<(), ()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn add_image_comment(data: &mut ExifData, d: &PixivData) -> Result<(), ()> {
|
||||
if d.description.is_none() {
|
||||
return Ok(());
|
||||
}
|
||||
let desc = parse_description(d.description.as_ref().unwrap());
|
||||
let desc = if desc.is_some() {
|
||||
desc.as_ref().unwrap()
|
||||
} else {
|
||||
d.description.as_ref().unwrap()
|
||||
};
|
||||
let key = ExifKey::try_from("Exif.Image.XPComment")?;
|
||||
let mut value = ExifValue::try_from(ExifTypeID::BYTE)?;
|
||||
let s: WString<LittleEndian> = WString::from(desc);
|
||||
value.read(s.as_bytes(), None)?;
|
||||
data.add(&key, &value)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_exifdata_to_image<S: AsRef<OsStr> + ?Sized>(file_name: &S, data: &PixivData) -> Result<(), ()> {
|
||||
let mut f = ExifImage::new(file_name)?;
|
||||
let mut d = ExifData::new()?;
|
||||
add_image_id(&mut d, data)?;
|
||||
add_image_title(&mut d, data)?;
|
||||
add_image_author(&mut d, data)?;
|
||||
add_image_comment(&mut d, data)?;
|
||||
f.set_exif_data(&d)?;
|
||||
f.write_metadata()?;
|
||||
Ok(())
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::data::data::PixivData;
|
||||
use crate::gettext;
|
||||
use crate::parser::description::parse_description;
|
||||
use crate::pixiv_link::PixivID;
|
||||
use crate::pixiv_link::ToPixivID;
|
||||
use json::JsonValue;
|
||||
@@ -91,6 +92,13 @@ impl From<&PixivData> for JSONDataFile {
|
||||
if p.author.is_some() {
|
||||
f.add("author", p.author.as_ref().unwrap()).unwrap();
|
||||
}
|
||||
if p.description.is_some() {
|
||||
f.add("description", p.description.as_ref().unwrap()).unwrap();
|
||||
let pd = parse_description(p.description.as_ref().unwrap());
|
||||
if pd.is_some() {
|
||||
f.add("parsed_description", pd.unwrap()).unwrap();
|
||||
}
|
||||
}
|
||||
f
|
||||
}
|
||||
}
|
||||
@@ -121,3 +129,9 @@ impl ToJson for &String {
|
||||
Some(JsonValue::String((*self).to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
impl ToJson for String {
|
||||
fn to_json(&self) -> Option<JsonValue> {
|
||||
Some(JsonValue::String(self.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user