Add tags to json data file

This commit is contained in:
2022-10-23 08:34:03 +00:00
committed by GitHub
parent 98c1ce03bd
commit 9d1bb86d97
2 changed files with 33 additions and 0 deletions

View File

@@ -18,6 +18,8 @@ pub struct PixivData {
/// The author
pub author: Option<String>,
pub description: Option<String>,
/// Tags (Original, translated)
pub tags: Option<Vec<(String, Option<String>)>>,
}
impl PixivData {
@@ -31,6 +33,7 @@ impl PixivData {
title: None,
author: None,
description: None,
tags: None,
})
}
@@ -84,6 +87,19 @@ impl PixivData {
}
}
}
let mut tags = Vec::new();
for tag in value["tags"]["tags"].members() {
if let Some(ori) = tag["tag"].as_str() {
tags.push((
ori.to_owned(),
match tag["translation"]["en"].as_str() {
Some(s) => Some(s.to_owned()),
None => None,
},
));
}
}
self.tags.replace(tags);
}
}

View File

@@ -105,6 +105,23 @@ impl From<&PixivData> for JSONDataFile {
f.add("parsed_description", pd.unwrap()).unwrap();
}
}
match p.tags.as_ref() {
Some(tags) => {
let mut t = JsonValue::new_array();
for tag in tags {
t.push(json::array![
tag.0.as_str(),
match &tag.1 {
Some(s) => JsonValue::String(s.to_owned()),
None => JsonValue::Null,
}
])
.unwrap();
}
f.add("tags", t).unwrap();
}
None => {}
}
f
}
}