Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
xiao-e-yun committed Jan 16, 2025
1 parent 37ba818 commit 7c376ef
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 77 deletions.
39 changes: 25 additions & 14 deletions src/api/fanbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use std::path::PathBuf;
use log::error;
use reqwest::header;
use reqwest_middleware::RequestBuilder;
use serde::{ de::DeserializeOwned, Deserialize, Serialize };
use serde::{de::DeserializeOwned, Deserialize, Serialize};

use crate::{ config::Config, fanbox::{Creator, FollowingCreator, Post, PostListItem, SupportingCreator} };
use crate::{
config::Config,
fanbox::{Creator, FollowingCreator, Post, PostListItem, SupportingCreator},
};

use super::ArchiveClient;

Expand All @@ -25,10 +28,7 @@ impl FanboxClient {
pub fn new(config: &Config) -> Self {
let inner = ArchiveClient::new(config);
let session = config.session();
Self {
inner,
session,
}
Self { inner, session }
}

fn wrap_request(&self, builder: RequestBuilder) -> RequestBuilder {
Expand Down Expand Up @@ -72,43 +72,54 @@ impl FanboxClient {
let response = request.send().await.expect("Failed to send request");

let mut file = tokio::fs::File::create(path).await.unwrap();
self.inner.download(response, &mut file).await.expect("Failed to download file");
self.inner
.download(response, &mut file)
.await
.expect("Failed to download file");

Ok(())
}

pub async fn get_supporting_creators(
&self
&self,
) -> Result<APIListSupportingCreator, Box<dyn std::error::Error>> {
let url = "https://api.fanbox.cc/plan.listSupporting";
let list: APIListSupportingCreator = self
.fetch(url).await
.fetch(url)
.await
.expect("Failed to get supporting authors");
Ok(list)
}

pub async fn get_following_creators(
&self
&self,
) -> Result<APIListFollowingCreator, Box<dyn std::error::Error>> {
let url = "https://api.fanbox.cc/creator.listFollowing";
let list: APIListFollowingCreator = self
.fetch(url).await
.fetch(url)
.await
.expect("Failed to get following authors");
Ok(list)
}

pub async fn get_posts(
&self,
creator: &Creator
creator: &Creator,
) -> Result<APIListCreatorPost, Box<dyn std::error::Error>> {
let url = format!("https://api.fanbox.cc/post.paginateCreator?creatorId={}", creator.id());
let url = format!(
"https://api.fanbox.cc/post.paginateCreator?creatorId={}",
creator.id()
);
let urls: APIListCreatorPaginate = self.fetch(&url).await.expect("Failed to get post list");

let mut tasks = Vec::new();
for url in urls {
let client = self.clone();
let future = async move {
client.fetch::<APIListCreatorPost>(&url).await.expect("Failed to get post")
client
.fetch::<APIListCreatorPost>(&url)
.await
.expect("Failed to get post")
};
tasks.push(tokio::spawn(future));
}
Expand Down
13 changes: 8 additions & 5 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
pub mod fanbox;

use std::sync::Arc;
use reqwest::{ Client, Response };
use reqwest_middleware::{ ClientBuilder, ClientWithMiddleware };
use reqwest_retry::{ policies::ExponentialBackoff, RetryTransientMiddleware };
use tokio::{ fs::File, sync::{ Semaphore, SemaphorePermit } };
use futures::StreamExt;
use reqwest::{Client, Response};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware};
use std::sync::Arc;
use tokio::{
fs::File,
sync::{Semaphore, SemaphorePermit},
};

use crate::{
config::Config,
Expand Down
7 changes: 3 additions & 4 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod save_type;

use clap::{ arg, Parser };
use clap_verbosity_flag::{ InfoLevel, Verbosity };
use clap::{arg, Parser};
use clap_verbosity_flag::{InfoLevel, Verbosity};
use dotenv::dotenv;
use save_type::SaveType;
use std::path::PathBuf;
Expand Down Expand Up @@ -46,8 +46,7 @@ impl Config {
}
/// Create a logger with the configured verbosity level
pub fn init_logger(&self) -> () {
env_logger::Builder
::new()
env_logger::Builder::new()
.filter_level(self.verbose.log_level_filter())
.format_target(false)
.init();
Expand Down
50 changes: 29 additions & 21 deletions src/creator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::{ collections::HashSet, error::Error, ops::Deref };
use std::{collections::HashSet, error::Error, ops::Deref};

use chrono::{DateTime, Utc};
use log::info;
use post_archiver::{ Author, AuthorId, FileMetaId, Link };
use rusqlite::{ params, Connection, OptionalExtension };
use post_archiver::{Author, AuthorId, FileMetaId, Link};
use rusqlite::{params, Connection, OptionalExtension};

use crate::{ api::fanbox::FanboxClient, config::Config, fanbox::Creator };
use crate::{api::fanbox::FanboxClient, config::Config, fanbox::Creator};

pub async fn get_creators(config: &Config) -> Result<Vec<Creator>, Box<dyn Error>> {
let accepts = config.accepts();
Expand Down Expand Up @@ -52,7 +52,10 @@ pub fn display_creators(creators: &Vec<Creator>) {
fee_width = creator.fee().to_string().len().max(fee_width);
}

info!("+-{:-<id_width$}-+-{:-<fee_width$}--+-{}------- - -", " CreatorId ", " Fee ", " Name ");
info!(
"+-{:-<id_width$}-+-{:-<fee_width$}--+-{}------- - -",
" CreatorId ", " Fee ", " Name "
);
for creator in creators.iter() {
info!(
"| {:id_width$} | {:fee_width$}$ | {}",
Expand All @@ -72,27 +75,26 @@ pub fn display_creators(creators: &Vec<Creator>) {

pub fn sync_creators(
conn: &mut Connection,
creators: Vec<Creator>
creators: Vec<Creator>,
) -> Result<Vec<SyncedCreator>, Box<dyn Error>> {
let mut list = vec![];
let tx = conn.transaction().unwrap();
{
let mut get_alias_stmt = tx.prepare("SELECT target FROM author_alias WHERE source = ?")?;
let mut get_author_stmt = tx.prepare("SELECT * FROM authors WHERE id = ?")?;
let mut update_author_stmt = tx.prepare("UPDATE authors SET links = ? WHERE id = ?")?;
let mut insert_author_stmt = tx.prepare(
"INSERT INTO authors (name,links) VALUES (?,?) RETURNING *"
)?;
let mut insert_alias_stmt = tx.prepare(
"INSERT INTO author_alias (source,target) VALUES (?,?)"
)?;
let mut insert_author_stmt =
tx.prepare("INSERT INTO authors (name,links) VALUES (?,?) RETURNING *")?;
let mut insert_alias_stmt =
tx.prepare("INSERT INTO author_alias (source,target) VALUES (?,?)")?;

for creator in creators {
let alias = format!("fanbox:{}", creator.id());
let link = || Link::new("fanbox", &format!("https://{}.fanbox.cc/", creator.id()));

let author = match
get_alias_stmt.query_row([&alias], |row| row.get::<_, u32>(0)).optional()?
let author = match get_alias_stmt
.query_row([&alias], |row| row.get::<_, u32>(0))
.optional()?
{
Some(id) => {
// it should be safe to unwrap here
Expand All @@ -113,12 +115,19 @@ pub fn sync_creators(
author
}
None => {
info!(" + Add new creator {} -> `{}`", creator.id(), creator.name());
info!(
" + Add new creator {} -> `{}`",
creator.id(),
creator.name()
);
let name = creator.name();
let link = link();
let links = serde_json::to_string(&[link])?;
let author = insert_author_stmt.query_row(params![name, links], row_to_author)?;
insert_alias_stmt.execute(params![alias, author.id.raw()]).unwrap();
let author =
insert_author_stmt.query_row(params![name, links], row_to_author)?;
insert_alias_stmt
.execute(params![alias, author.id.raw()])
.unwrap();
author
}
};
Expand All @@ -129,9 +138,8 @@ pub fn sync_creators(
let name: String = row.get("name")?;

let links: String = row.get("links")?;
let links: Vec<Link> = serde_json
::from_str(&links)
.expect("Author links is not valid JSON");
let links: Vec<Link> =
serde_json::from_str(&links).expect("Author links is not valid JSON");

let thumb: Option<u32> = row.get("id")?;
let thumb: Option<FileMetaId> = thumb.map(FileMetaId::new);
Expand Down Expand Up @@ -174,4 +182,4 @@ impl Deref for SyncedCreator {
fn deref(&self) -> &Self::Target {
&self.creator
}
}
}
3 changes: 1 addition & 2 deletions src/fanbox/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::hash::Hash;

use serde::{Deserialize, Serialize};


#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
#[serde(rename_all = "camelCase")]
pub struct User {
Expand All @@ -29,4 +28,4 @@ pub enum PostType {
Article,
Video,
Entry,
}
}
3 changes: 1 addition & 2 deletions src/fanbox/creator/following.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::fanbox::common::{PostType, User};

use super::Creator;


#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
#[serde(rename_all = "camelCase")]
pub struct FollowingCreator {
Expand Down Expand Up @@ -40,4 +39,4 @@ pub struct ProfileItem {
ty: PostType,
image_url: String,
thumbnail_url: String,
}
}
3 changes: 1 addition & 2 deletions src/fanbox/creator/supporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::fanbox::common::User;

use super::Creator;


#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
#[serde(rename_all = "camelCase")]
pub struct SupportingCreator {
Expand All @@ -27,4 +26,4 @@ impl From<SupportingCreator> for Creator {
fee: creator.fee,
}
}
}
}
2 changes: 1 addition & 1 deletion src/fanbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub mod post;

pub use common::*;
pub use creator::*;
pub use post::*;
pub use post::*;
29 changes: 7 additions & 22 deletions src/fanbox/post/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use serde::{Deserialize, Serialize};

use super::PostListItem;


#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
#[serde(rename_all = "camelCase", tag = "type")]
pub struct PostBody {
Expand All @@ -20,7 +19,6 @@ pub struct PostBody {
pub url_embed_map: Option<BTreeMap<String, PostTextEmbed>>,
}


#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum PostBlock {
Expand All @@ -33,25 +31,15 @@ pub enum PostBlock {
styles: Option<Vec<PostBlockStyle>>,
},
#[serde(rename_all = "camelCase")]
Image {
image_id: String,
},
Image { image_id: String },
#[serde(rename_all = "camelCase")]
File {
file_id: String,
},
File { file_id: String },
#[serde(rename_all = "camelCase")]
Embed {
embed_id: String,
},
Embed { embed_id: String },
#[serde(rename_all = "camelCase")]
UrlEmbed {
url_embed_id: String,
},
UrlEmbed { url_embed_id: String },
#[serde(rename_all = "camelCase")]
Video {
video_id: String,
},
Video { video_id: String },
}

#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
Expand Down Expand Up @@ -146,13 +134,10 @@ pub enum PostTextEmbed {
#[serde(rename = "html.card")]
HtmlCard { id: String, html: String },
#[serde(rename = "fanbox.post", rename_all = "camelCase")]
FanboxPost {
id: String,
post_info: PostListItem,
},
FanboxPost { id: String, post_info: PostListItem },
Default {
id: String,
url: String,
host: String,
},
}
}
3 changes: 1 addition & 2 deletions src/fanbox/post/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use serde::{Deserialize, Serialize};

use crate::fanbox::User;


#[derive(Deserialize, Serialize, Debug, Clone, Hash)]
#[serde(rename_all = "camelCase")]
pub struct PostListItem {
Expand Down Expand Up @@ -32,4 +31,4 @@ pub struct PostListItem {
pub enum Cover {
CoverImage { url: String },
PostImage { url: String },
}
}
4 changes: 2 additions & 2 deletions src/fanbox/post/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub mod item;
pub use super::{PostType, User};
use chrono::{DateTime, Utc};

pub use item::*;
pub use body::*;
pub use item::*;

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -64,4 +64,4 @@ pub struct PostShort {
id: String,
title: String,
published_datetime: DateTime<Utc>,
}
}

0 comments on commit 7c376ef

Please sign in to comment.