Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Feb 5, 2024
1 parent 4dbc7a4 commit f867c94
Showing 1 changed file with 113 additions and 44 deletions.
157 changes: 113 additions & 44 deletions soda_resource_tools_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::{
env::current_dir,
fs::{self},
fs,
path::{Path, PathBuf},
};

use directories::ProjectDirs;
// use magic_crypt::{new_magic_crypt, MagicCryptTrait};
use serde_json::Value;
use soda_resource_tools_lib::soda::{
self,
Expand All @@ -31,7 +30,7 @@ struct Cli {
log_path: Option<std::path::PathBuf>,

/// 日志级别
#[arg(long, default_value = "info", value_parser = clap::builder::PossibleValuesParser::new(["trace", "debug", "info", "warn", "error"]))]
#[arg(long, default_value = "debug", value_parser = clap::builder::PossibleValuesParser::new(["trace", "debug", "info", "warn", "error"]))]
log_level: Option<String>,

/// 缓存路径
Expand All @@ -48,6 +47,7 @@ enum Commands {
#[command(arg_required_else_help = true)]
Scrape {
/// 媒体类型
/// mt: 电影和电视剧
#[arg(
long,
default_value_t = ResourceType::MT,
Expand All @@ -57,6 +57,10 @@ enum Commands {
resource_type: ResourceType,

/// 媒体从源目录转移到输出目录的方式
/// hard_link: 硬链接
/// symbol_link: 符号链接
/// copy: 复制
/// move: 移动
#[arg(
long,
default_value_t = TransferType::HardLink,
Expand All @@ -65,53 +69,61 @@ enum Commands {
)]
transfer_type: TransferType,

/// 媒体源目录 - 存在音视频文件的目录
#[arg(long,value_hint = clap::ValueHint::DirPath)]
src_dir: Option<std::path::PathBuf>,

/// 媒体刮削输出目录 - 刮削后的文件输出目录,如果不指定则默认为src_dir
#[arg(long,value_hint = clap::ValueHint::DirPath)]
target_dir: Option<std::path::PathBuf>,

/// 刮削图片
#[arg(long)]
scrape_image: Option<bool>,
/// true: 刮削图片
/// false: 不刮削图片
#[arg(long, default_value_t = true)]
scrape_image: bool,

/// 重命名格式
#[arg( long, value_parser = clap::builder::PossibleValuesParser::new(["emby"])
/// emby: Emby格式
#[arg( long, default_value_t = RenameStyle::Emby, value_parser = clap::builder::PossibleValuesParser::new(["emby"])
.map(|s| s.parse::<RenameStyle>().unwrap()),
)]
rename_style: Option<RenameStyle>,
rename_style: RenameStyle,

/// 媒体源目录
#[arg(long,value_hint = clap::ValueHint::DirPath)]
src_dir: Option<std::path::PathBuf>,

/// 媒体刮削输出目录
/// 刮削后的文件输出目录,如果不指定则默认为src_dir
#[arg(long,value_hint = clap::ValueHint::DirPath)]
target_dir: Option<std::path::PathBuf>,
},
}

fn main() {
fn main() -> Result<(), SodaError> {
// 解析命令行参数
let args = Cli::parse();

// 开发模式
let dev = args.dev.unwrap_or(false);

// 获取配置文件目录
let proj_dirs = ProjectDirs::from("com", "biezhihua", "soda").unwrap();

// 创建配置文件目录
let config_dir = proj_dirs.config_dir();
fs::create_dir_all(config_dir).unwrap();
fs::create_dir_all(config_dir)?;

// 创建缓存文件目录
let mut cache_dir = args.cache_path.unwrap_or(proj_dirs.cache_dir().join("cache"));
if dev {
cache_dir = current_dir().unwrap().parent().unwrap().join("soda_resource_tools_lib").join("cache");
cache_dir = current_dir()?.parent().unwrap().join("soda_resource_tools_lib").join("cache");
}
let cache_dir = Path::new(&cache_dir);
fs::create_dir_all(cache_dir).unwrap();
fs::create_dir_all(cache_dir)?;

// 创建日志文件目录
let mut log_dir = args.log_path.unwrap_or(proj_dirs.cache_dir().join("log"));
if dev {
log_dir = cache_dir.join("log");
}
let log_dir = if dev {
cache_dir.join("log")
} else {
args.log_path.unwrap_or(proj_dirs.cache_dir().join("log"))
};
let log_dir = Path::new(&log_dir);
clean_dir(&log_dir.to_path_buf());
fs::create_dir_all(log_dir).unwrap();
fs::create_dir_all(log_dir)?;

// 配置日志
let log_level = args.log_level.unwrap_or("info".to_string());
Expand All @@ -131,38 +143,39 @@ fn main() {
tracing::info!(target:"soda::info", "缓存文件目录: {}", cache_dir.to_str().unwrap());
tracing::info!(target:"soda::info", "日志文件目录: {}", log_dir.to_str().unwrap());

// 检查网络
check_internet()?;

match args.command {
Commands::Scrape {
resource_type,
transfer_type,
src_dir,
target_dir,
scrape_image,
rename_style: rename_format,
rename_style,
} => {
let rename_style = rename_format.unwrap_or(RenameStyle::Emby);

// 开发者配置
if dev {
let lib_dir = current_dir().unwrap().parent().unwrap().join("soda_resource_tools_lib");
let lib_dir = current_dir()?.parent().unwrap().join("soda_resource_tools_lib");
init_lib_config_dev(&lib_dir, &rename_style);
}
// Release配置
else {
if let Ok(()) = init_config(&config_dir) {
tracing::info!(target:"soda::info", "初始化配置文件成功");
} else {
tracing::error!(target:"soda::info", "初始化配置文件失败");
return;
tracing::error!(target:"soda::info", "初始化配置文件失败,请检查网络后重试");
return Err(SodaError::Str("初始化配置文件失败,请检查网络后重试"));
}

let local_soda_config_path = config_dir.join("soda_config.json");
let local_soda_config: Value = serde_json::from_str(&fs::read_to_string(&local_soda_config_path).unwrap()).unwrap();
let local_soda_config: Value = serde_json::from_str(&fs::read_to_string(&local_soda_config_path)?)?;
tracing::info!(target:"soda::info", "配置文件: {}", local_soda_config);

if !local_soda_config.get("enable_cli").unwrap().as_bool().unwrap() {
tracing::error!(target:"soda::info", "配置文件中enable_cli为false,不允许使用soda_cli");
return;
return Ok(());
}

init_lib_config(&local_soda_config, &config_dir, &cache_dir, &rename_style);
Expand All @@ -178,13 +191,32 @@ fn main() {
}
let src_dir = src_dir.to_str().unwrap().to_string();
let target_dir = target_dir.to_str().unwrap().to_string();
scrape_mt(resource_type, transfer_type, src_dir, target_dir, scrape_image.unwrap_or(true));
scrape_mt(resource_type, transfer_type, src_dir, target_dir, scrape_image);
} else {
tracing::error!(target:"soda::info", "媒体源目录不存在")
}
}
}
}

return Ok(());
}

fn check_internet() -> Result<(), SodaError> {
tracing::info!(target:"soda::info", "开始检查网络");

tracing::info!(target:"soda::info", "开始访问: https://raw.githubusercontent.com/biezhihua/soda_cli/main/soda_config.json" );
let _ = reqwest::blocking::get("https://raw.githubusercontent.com/biezhihua/soda_cli/main/soda_config.json")?;

tracing::info!(target:"soda::info", "开始访问: https://api.themoviedb.org" );
// let _ = reqwest::blocking::Client::new().post("https://api.themoviedb.org").send()?;
let _ = reqwest::blocking::get("https://api.themoviedb.org")?;

tracing::info!(target:"soda::info", "开始访问: https://webservice.fanart.tv" );
// let _ = reqwest::blocking::Client::new().post("https://webservice.fanart.tv").send()?;
let _ = reqwest::blocking::get("https://webservice.fanart.tv")?;

return Ok(());
}

fn init_lib_config(local_soda_config: &Value, config_dir: &Path, cache_dir: &Path, rename_style: &RenameStyle) {
Expand Down Expand Up @@ -238,26 +270,63 @@ fn init_lib_config_dev(lib_dir: &Path, rename_style: &RenameStyle) {
}

fn init_config(config_dir: &Path) -> Result<(), SodaError> {
let remote_soda_config: Value = reqwest::blocking::get("https://raw.githubusercontent.com/biezhihua/soda_cli/main/soda_config.json")?.json()?;
tracing::info!(target:"soda::info", "开始从Github获取配置文件");

let soda_config_url = format!("{}/soda_config.json", raw_github());

let remote_soda_config: Value = reqwest::blocking::get(soda_config_url)?.json()?;

tracing::info!(target:"soda::info", "获取配置文件成功: {}", remote_soda_config);

let local_soda_config_path = config_dir.join("soda_config.json");
if !local_soda_config_path.exists() {
update_soda_config(&local_soda_config_path, &remote_soda_config, config_dir);
update_soda_config(&local_soda_config_path, &remote_soda_config, config_dir)?;
} else {
let local_soda_config: Value = serde_json::from_str(&fs::read_to_string(&local_soda_config_path).unwrap()).unwrap();
if local_soda_config.get("version").unwrap().as_i64() < remote_soda_config.get("version").unwrap().as_i64() {
update_soda_config(&local_soda_config_path, &remote_soda_config, config_dir);
let local_soda_config: Value = serde_json::from_str(&fs::read_to_string(&local_soda_config_path)?)?;
let local_version = local_soda_config.get("version").unwrap().as_i64().unwrap();
let remote_version = remote_soda_config.get("version").unwrap().as_i64().unwrap();
if local_version < remote_version {
update_soda_config(&local_soda_config_path, &remote_soda_config, config_dir)?;
}
}
return Ok(());
}

fn update_soda_config(local_soda_config_path: &std::path::PathBuf, remote_soda_config: &Value, config_dir: &std::path::Path) {
fs::write(local_soda_config_path, remote_soda_config.to_string()).unwrap();
reqwest::blocking::get("https://raw.githubusercontent.com/biezhihua/soda_cli/main/soda_config.bin")
.unwrap()
.copy_to(&mut fs::File::create(config_dir.join(remote_soda_config.get("bin").unwrap().as_str().unwrap())).unwrap())
.unwrap();
fn raw_github() -> &'static str {
return "https://raw.githubusercontent.com/biezhihua/soda_cli/main";
}

fn api_themoviedb() -> &'static str {
return "https://api.themoviedb.org";
}

fn api_fanart() -> &'static str {
return "https://webservice.fanart.tv";
}

fn update_soda_config(
local_soda_config_path: &std::path::PathBuf,
remote_soda_config: &Value,
config_dir: &std::path::Path,
) -> Result<(), SodaError> {
// write soda_config.json
fs::write(local_soda_config_path, remote_soda_config.to_string())?;

let bin = remote_soda_config
.get("bin")
.ok_or(SodaError::Str("bin字段不存在"))?
.as_str()
.ok_or(SodaError::Str("bin字段不是字符串"))?;

let bin_url = format!("{}/{}", raw_github(), bin);

tracing::info!(target:"soda::info", "开始从Github获取bin文件: {}", bin_url);

let mut local_bin_file = fs::File::create(config_dir.join(bin))?;

reqwest::blocking::get(bin_url)?.copy_to(&mut local_bin_file)?;

return Ok(());
}

fn init_tracing(log_level: String, all_log: NonBlocking, metadata_log: NonBlocking) {
Expand Down

0 comments on commit f867c94

Please sign in to comment.