This commit is contained in:
2022-07-14 13:24:41 +00:00
committed by GitHub
parent 6420be64af
commit 061aa56bde
5 changed files with 112 additions and 4 deletions

22
src/data/fanbox.rs Normal file
View File

@@ -0,0 +1,22 @@
use crate::fanbox::post::FanboxPost;
use crate::pixiv_link::PixivID;
use crate::pixiv_link::ToPixivID;
use json::JsonValue;
pub struct FanboxData {
pub id: PixivID,
/// Raw data
pub raw: JsonValue,
}
impl FanboxData {
pub fn new<T: ToPixivID>(id: T, post: &FanboxPost) -> Option<Self> {
match id.to_pixiv_id() {
Some(id) => Some(Self {
id,
raw: post.get_json().clone(),
}),
None => None,
}
}
}

View File

@@ -1,3 +1,4 @@
use super::fanbox::FanboxData;
use crate::data::data::PixivData;
use crate::ext::json::ToJson;
use crate::gettext;
@@ -108,6 +109,17 @@ impl From<&PixivData> for JSONDataFile {
}
}
impl From<FanboxData> for JSONDataFile {
fn from(d: FanboxData) -> Self {
let mut f = Self {
id: d.id.clone(),
maps: HashMap::new(),
};
f.add("raw", d.raw).unwrap();
f
}
}
impl ToJson for JSONDataFile {
fn to_json(&self) -> Option<JsonValue> {
let mut value = json::object! {};

View File

@@ -1,6 +1,7 @@
pub mod data;
#[cfg(feature = "exif")]
pub mod exif;
pub mod fanbox;
pub mod json;
#[cfg(feature = "avdict")]
pub mod video;

View File

@@ -4,6 +4,7 @@ use crate::concat_pixiv_downloader_error;
use crate::data::data::PixivData;
#[cfg(feature = "exif")]
use crate::data::exif::add_exifdata_to_image;
use crate::data::fanbox::FanboxData;
use crate::data::json::JSONDataFile;
#[cfg(feature = "ugoira")]
use crate::data::video::get_video_metadata;
@@ -12,9 +13,12 @@ use crate::downloader::DownloaderResult;
use crate::downloader::LocalFile;
use crate::error::PixivDownloaderError;
use crate::ext::try_err::TryErr;
use crate::fanbox::check::CheckUnknown;
use crate::fanbox::post::FanboxPost;
use crate::fanbox_api::FanboxClient;
use crate::gettext;
use crate::opthelper::get_helper;
use crate::pixiv_link::FanboxPostID;
use crate::pixiv_link::PixivID;
use crate::pixiv_web::PixivWebClient;
#[cfg(feature = "ugoira")]
@@ -61,7 +65,7 @@ impl Main {
return r;
}
}
PixivID::FanboxPost(_) => {
PixivID::FanboxPost(id) => {
if !fc.is_inited() {
let helper = get_helper();
if !fc.init(helper.cookies()) {
@@ -71,7 +75,20 @@ impl Main {
if !fc.check_login().await {
return 1;
}
println!("Logined: {}", fc.logined());
if !fc.logined() {
println!("{}", gettext("Warning: Fanbox client is not logined."));
}
let r = self.download_fanbox_post(Arc::clone(&fc), id.clone()).await;
let r = match r {
Ok(_) => 0,
Err(e) => {
println!("{} {}", gettext("Failed to download post:"), e);
1
}
};
if r != 0 {
return r;
}
}
}
}
@@ -353,4 +370,54 @@ impl Main {
}
Ok(())
}
pub async fn download_fanbox_post(
&self,
fc: Arc<FanboxClient>,
id: FanboxPostID,
) -> Result<(), PixivDownloaderError> {
let post = fc
.get_post_info(id.post_id)
.await
.try_err(gettext("Failed to get post info."))?;
let helper = get_helper();
if helper.verbose() {
println!("{:#?}", post);
}
match post.check_unknown() {
Ok(_) => {}
Err(e) => {
println!(
"{} {}",
gettext("Warning: Post info contains unknown data:"),
e
);
}
}
if post
.is_restricted()
.try_err(gettext("Failed to check the post is restricted or not."))?
{
println!("{}", gettext("Warning: This article is restricted."));
// #TODO allow to continue
return Ok(());
}
let base = Arc::new(PathBuf::from(format!("./{}", id.post_id)));
let json_file = base.join("body.json");
let data = FanboxData::new(id, &post).try_err("Failed to create data file.")?;
let data_file = JSONDataFile::from(data);
data_file
.save(&json_file)
.try_err(gettext("Failed to save post data to file."))?;
match post {
FanboxPost::Article(article) => {}
FanboxPost::Image(img) => {}
FanboxPost::Unknown(_) => {
return Err(PixivDownloaderError::from(gettext(
"Unrecognized post type.",
)));
}
}
Ok(())
}
}

View File

@@ -17,9 +17,9 @@ lazy_static! {
/// Fanbox post ID
pub struct FanboxPostID {
/// Creator ID
creator_id: String,
pub creator_id: String,
/// Post ID
post_id: u64,
pub post_id: u64,
}
impl FanboxPostID {
@@ -125,6 +125,12 @@ impl ToJson for PixivID {
}
}
impl ToPixivID for FanboxPostID {
fn to_pixiv_id(&self) -> Option<PixivID> {
Some(PixivID::FanboxPost(self.clone()))
}
}
impl ToPixivID for PixivID {
fn to_pixiv_id(&self) -> Option<PixivID> {
Some(self.clone())