Skip to content

Commit 450b724

Browse files
committed
update
1 parent e13df6c commit 450b724

File tree

15 files changed

+244
-71
lines changed

15 files changed

+244
-71
lines changed

soda_resource_tools_cli/src/main.rs

+46-31
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use magic_crypt::{new_magic_crypt, MagicCryptTrait};
99
use serde_json::Value;
1010
use soda_resource_tools_lib::soda::{
1111
self,
12-
entity::{EmbyRenameStyle, RenameStyle, ResourceType, ScrapeConfig, SodaError, TransferType},
12+
entity::{RenameStyle, ResourceType, ScrapeConfig, SodaError, TransferType},
1313
};
1414
use tracing_appender::non_blocking::NonBlocking;
1515
use tracing_subscriber::{filter, fmt::time::ChronoLocal, layer::SubscriberExt, util::SubscriberInitExt, Layer};
@@ -72,6 +72,12 @@ enum Commands {
7272
/// 刮削图片
7373
#[arg(long)]
7474
scrape_image: Option<bool>,
75+
76+
/// 重命名格式
77+
#[arg( long, value_parser = clap::builder::PossibleValuesParser::new(["emby"])
78+
.map(|s| s.parse::<RenameStyle>().unwrap()),
79+
)]
80+
rename_style: Option<RenameStyle>,
7581
},
7682
}
7783

@@ -118,34 +124,43 @@ fn main() {
118124
tracing::info!(target:"soda::info", "缓存文件目录: {}", cache_dir.to_str().unwrap());
119125
tracing::info!(target:"soda::info", "日志文件目录: {}", log_dir.to_str().unwrap());
120126

121-
// 开发者配置
122-
if dev {
123-
let lib_dir = current_dir().unwrap().parent().unwrap().join("soda_resource_tools_lib");
124-
init_lib_config_dev(&lib_dir);
125-
}
126-
// Release配置
127-
else {
128-
if let Ok(()) = init_config(&config_dir) {
129-
tracing::info!(target:"soda::info", "初始化配置文件成功");
130-
} else {
131-
tracing::error!(target:"soda::info", "初始化配置文件失败");
132-
return;
133-
}
127+
match args.command {
128+
Commands::Scrape {
129+
resource_type,
130+
transfer_type,
131+
src_dir,
132+
target_dir,
133+
scrape_image,
134+
rename_style: rename_format,
135+
} => {
136+
let rename_style = rename_format.unwrap_or(RenameStyle::Emby);
137+
138+
// 开发者配置
139+
if dev {
140+
let lib_dir = current_dir().unwrap().parent().unwrap().join("soda_resource_tools_lib");
141+
init_lib_config_dev(&lib_dir, &rename_style);
142+
}
143+
// Release配置
144+
else {
145+
if let Ok(()) = init_config(&config_dir) {
146+
tracing::info!(target:"soda::info", "初始化配置文件成功");
147+
} else {
148+
tracing::error!(target:"soda::info", "初始化配置文件失败");
149+
return;
150+
}
134151

135-
let local_soda_config_path = config_dir.join("soda_config.json");
136-
let local_soda_config: Value = serde_json::from_str(&fs::read_to_string(&local_soda_config_path).unwrap()).unwrap();
137-
tracing::info!(target:"soda::info", "配置文件: {}", local_soda_config);
152+
let local_soda_config_path = config_dir.join("soda_config.json");
153+
let local_soda_config: Value = serde_json::from_str(&fs::read_to_string(&local_soda_config_path).unwrap()).unwrap();
154+
tracing::info!(target:"soda::info", "配置文件: {}", local_soda_config);
138155

139-
if !local_soda_config.get("enable_cli").unwrap().as_bool().unwrap() {
140-
tracing::error!(target:"soda::info", "配置文件中enable_cli为false,不允许使用soda_cli");
141-
return;
142-
}
156+
if !local_soda_config.get("enable_cli").unwrap().as_bool().unwrap() {
157+
tracing::error!(target:"soda::info", "配置文件中enable_cli为false,不允许使用soda_cli");
158+
return;
159+
}
143160

144-
init_lib_config(&local_soda_config, &config_dir, &cache_dir);
145-
}
161+
init_lib_config(&local_soda_config, &config_dir, &cache_dir, &rename_style);
162+
}
146163

147-
match args.command {
148-
Commands::Scrape { resource_type, transfer_type, src_dir, target_dir, scrape_image } => {
149164
if src_dir.is_some() {
150165
let src_dir = src_dir.clone().unwrap();
151166
let target_dir = if target_dir.is_some() { target_dir.unwrap() } else { src_dir.clone() };
@@ -165,7 +180,7 @@ fn main() {
165180
}
166181
}
167182

168-
fn init_lib_config(local_soda_config: &Value, config_dir: &Path, cache_dir: &Path) {
183+
fn init_lib_config(local_soda_config: &Value, config_dir: &Path, cache_dir: &Path, rename_style: &RenameStyle) {
169184
let mc = new_magic_crypt!("biezhihua_soda", 256);
170185

171186
let bin_path_name = local_soda_config.get("bin").unwrap().as_str().unwrap();
@@ -190,21 +205,21 @@ fn init_lib_config(local_soda_config: &Value, config_dir: &Path, cache_dir: &Pat
190205
config.strong_match_regex_rules_path = "".to_string();
191206
config.strong_match_name_map_path = "".to_string();
192207
config.metadata_skip_special = true;
193-
config.movie_rename_format = Some(RenameStyle::Emby(EmbyRenameStyle::EmbyMovie));
194-
config.tv_rename_format = Some(RenameStyle::Emby(EmbyRenameStyle::EmbyTV));
208+
config.movie_rename_format = Some(rename_style.clone());
209+
config.tv_rename_format = Some(rename_style.clone());
195210
soda::update_lib_config(config);
196211
tracing::info!(target:"soda::info", "配置更新成功");
197212
}
198213

199-
fn init_lib_config_dev(lib_dir: &Path) {
214+
fn init_lib_config_dev(lib_dir: &Path, rename_style: &RenameStyle) {
200215
let mut config = soda::get_lib_config();
201216
config.cache_path = lib_dir.join("cache").to_str().unwrap().to_string();
202217
config.strong_match_rules_tv_path = lib_dir.join("config").join("mt_strong_match_rules_tv.json").to_str().unwrap().to_string();
203218
config.strong_match_rules_movie_path = lib_dir.join("config").join("mt_strong_match_rules_movie.json").to_str().unwrap().to_string();
204219
config.strong_match_regex_rules_path = lib_dir.join("config").join("mt_strong_match_regex_rules.json").to_str().unwrap().to_string();
205220
config.strong_match_name_map_path = lib_dir.join("config").join("mt_strong_match_name_map.json").to_str().unwrap().to_string();
206-
config.movie_rename_format = Some(RenameStyle::Emby(EmbyRenameStyle::EmbyMovie));
207-
config.tv_rename_format = Some(RenameStyle::Emby(EmbyRenameStyle::EmbyTV));
221+
config.movie_rename_format = Some(rename_style.clone());
222+
config.tv_rename_format = Some(rename_style.clone());
208223
config.metadata_skip_special = true;
209224
soda::update_lib_config(config);
210225
tracing::info!(target:"soda::info", "配置更新成功");

soda_resource_tools_lib/src/soda.rs

+40-7
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,20 @@ pub fn update_lib_config(new_config: LibConfig) {
6464
/// https://emby.media/support/articles/Movie-Naming.html
6565
/// https://support.emby.media/support/solutions/articles/44001159110-tv-naming
6666
///
67-
pub fn scrape(resource_type: ResourceType, transfer_type: TransferType, scrape_config: ScrapeConfig, src_directory: String, target_directory: String) {
68-
tracing::debug!("scrape_src_to_target resource_type {:?}, transfer_mode {:?}, src_directory {:?}, target_directory {:?}", resource_type, transfer_type, src_directory, target_directory);
67+
pub fn scrape(
68+
resource_type: ResourceType,
69+
transfer_type: TransferType,
70+
scrape_config: ScrapeConfig,
71+
src_directory: String,
72+
target_directory: String,
73+
) {
74+
tracing::debug!(
75+
"scrape_src_to_target resource_type {:?}, transfer_mode {:?}, src_directory {:?}, target_directory {:?}",
76+
resource_type,
77+
transfer_type,
78+
src_directory,
79+
target_directory
80+
);
6981

7082
let mut paths: Vec<String> = Vec::new();
7183

@@ -100,7 +112,14 @@ pub fn scrape(resource_type: ResourceType, transfer_type: TransferType, scrape_c
100112
// 初始化上下文
101113
if meta_context.init(&src_path) {
102114
// 刮削
103-
match scrape_mt(&mut meta_context, &mut mt_infos, src_path, &scrape_config, &target_directory, &transfer_type) {
115+
match scrape_mt(
116+
&mut meta_context,
117+
&mut mt_infos,
118+
src_path,
119+
&scrape_config,
120+
&target_directory,
121+
&transfer_type,
122+
) {
104123
Ok(_) => {}
105124
Err(e) => {
106125
tracing::error!(target: "soda::info","刮削失败 e = {}", e);
@@ -114,7 +133,14 @@ pub fn scrape(resource_type: ResourceType, transfer_type: TransferType, scrape_c
114133
}
115134
}
116135

117-
fn scrape_mt(meta_context: &mut MetaContext, mt_infos: &mut HashMap<String, MTInfo>, src_path: String, scrape_config: &ScrapeConfig, target_directory: &String, transfer_type: &TransferType) -> Result<(), SodaError> {
136+
fn scrape_mt(
137+
meta_context: &mut MetaContext,
138+
mt_infos: &mut HashMap<String, MTInfo>,
139+
src_path: String,
140+
scrape_config: &ScrapeConfig,
141+
target_directory: &String,
142+
transfer_type: &TransferType,
143+
) -> Result<(), SodaError> {
118144
tracing::info!(target:"soda::info", "开始刮削: {}", src_path);
119145

120146
// 创建meta
@@ -190,7 +216,11 @@ fn scrape_mt(meta_context: &mut MetaContext, mt_infos: &mut HashMap<String, MTIn
190216
}
191217

192218
// 选择重命名格式
193-
let rename_format = if mt_meta.is_movie() { LIB_CONFIG.lock().unwrap().transfer_rename_format_movie.clone() } else { LIB_CONFIG.lock().unwrap().transfer_rename_format_tv.clone() };
219+
let rename_format = if mt_meta.is_movie() {
220+
LIB_CONFIG.lock().unwrap().transfer_rename_format_movie.clone()
221+
} else {
222+
LIB_CONFIG.lock().unwrap().transfer_rename_format_tv.clone()
223+
};
194224

195225
// 生成转移文件路径
196226
let transfer_target_path = transfer::gen_mt_transfer_target_path(&target_directory, &rename_format, &mt_meta);
@@ -207,14 +237,17 @@ fn scrape_mt(meta_context: &mut MetaContext, mt_infos: &mut HashMap<String, MTIn
207237
tracing::error!(target: "soda::info","识别失败 e = {}", error);
208238

209239
// 选择重命名格式
210-
let rename_format = if mt_meta.is_movie() { LIB_CONFIG.lock().unwrap().transfer_rename_format_movie.clone() } else { LIB_CONFIG.lock().unwrap().transfer_rename_format_tv.clone() };
240+
let rename_format = if mt_meta.is_movie() {
241+
LIB_CONFIG.lock().unwrap().transfer_rename_format_movie.clone()
242+
} else {
243+
LIB_CONFIG.lock().unwrap().transfer_rename_format_tv.clone()
244+
};
211245

212246
// 生成转移文件路径
213247
let transfer_target_path = transfer::gen_mt_transfer_target_path(&target_directory, &rename_format, &mt_meta);
214248

215249
// 转移文件
216250
transfer::transfer_mt_file(&mt_meta, &src_path, &transfer_target_path, &transfer_type)?;
217-
218251
}
219252
}
220253

soda_resource_tools_lib/src/soda/entity.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,27 @@ impl From<serde_json::Error> for SodaError {
7474
#[derive(Debug, Clone, Serialize, Deserialize)]
7575
pub enum RenameStyle {
7676
/// Emby 重命名格式
77-
Emby(EmbyRenameStyle),
77+
Emby,
78+
}
79+
80+
impl std::fmt::Display for RenameStyle {
81+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
82+
let s = match self {
83+
RenameStyle::Emby => "emby",
84+
};
85+
s.fmt(f)
86+
}
87+
}
88+
89+
impl std::str::FromStr for RenameStyle {
90+
type Err = String;
91+
92+
fn from_str(s: &str) -> Result<Self, Self::Err> {
93+
match s {
94+
"emby" => Ok(Self::Emby),
95+
_ => Err(format!("Unknown type: {s}")),
96+
}
97+
}
7898
}
7999

80100
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -384,6 +404,7 @@ pub enum MTType {
384404
#[derive(Debug, Clone)]
385405
pub enum ResourceType {
386406
/// 影视 MT
407+
/// Movie or TV
387408
MT,
388409
}
389410

@@ -1005,6 +1026,7 @@ impl std::fmt::Display for TransferType {
10051026
s.fmt(f)
10061027
}
10071028
}
1029+
10081030
impl std::str::FromStr for TransferType {
10091031
type Err = String;
10101032

soda_resource_tools_lib/src/soda/fanart/entity.rs

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct FanartTV {
1414
pub tvposter: Option<Vec<FanartImg>>,
1515
}
1616

17-
1817
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
1918
pub struct FanartMovie {
2019
pub name: Option<String>,

soda_resource_tools_lib/src/soda/filebrowser.rs

+34-5
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,22 @@ impl FileItem {
3333
pub(crate) fn create(path_obj: &Path) -> FileItem {
3434
let is_dir = path_obj.is_dir();
3535
let file_type = if is_dir { "dir".to_string() } else { "file".to_string() };
36-
let path = if system::is_windows_os() { path_obj.to_str().unwrap().to_string().replace("\\", "/") } else { path_obj.to_str().unwrap().to_string() };
36+
let path = if system::is_windows_os() {
37+
path_obj.to_str().unwrap().to_string().replace("\\", "/")
38+
} else {
39+
path_obj.to_str().unwrap().to_string()
40+
};
3741
let name = path_obj.file_name().unwrap().to_str().unwrap().to_string();
3842
let extension = Self::get_extension(path_obj, is_dir);
3943
let size = if is_dir { 0 } else { path_obj.metadata().unwrap().len() };
40-
let modify_time = path_obj.metadata().unwrap().modified().unwrap().duration_since(UNIX_EPOCH).unwrap().as_secs();
44+
let modify_time = path_obj
45+
.metadata()
46+
.unwrap()
47+
.modified()
48+
.unwrap()
49+
.duration_since(UNIX_EPOCH)
50+
.unwrap()
51+
.as_secs();
4152
FileItem {
4253
file_type,
4354
path,
@@ -67,7 +78,11 @@ impl FileItem {
6778
}
6879

6980
fn get_extension(path_obj: &Path, is_dir: bool) -> String {
70-
let extension = if is_dir { "".to_string() } else { path_obj.extension().unwrap_or(OsStr::new("")).to_str().unwrap_or("").to_string() };
81+
let extension = if is_dir {
82+
"".to_string()
83+
} else {
84+
path_obj.extension().unwrap_or(OsStr::new("")).to_str().unwrap_or("").to_string()
85+
};
7186
extension
7287
}
7388

@@ -78,7 +93,14 @@ impl FileItem {
7893
let name = path_obj.to_str().unwrap().to_string();
7994
let extension = Self::get_extension(path_obj, is_dir);
8095
let size = if is_dir { 0 } else { path_obj.metadata().unwrap().len() };
81-
let modify_time = path_obj.metadata().unwrap().modified().unwrap().duration_since(UNIX_EPOCH).unwrap().as_secs();
96+
let modify_time = path_obj
97+
.metadata()
98+
.unwrap()
99+
.modified()
100+
.unwrap()
101+
.duration_since(UNIX_EPOCH)
102+
.unwrap()
103+
.as_secs();
82104
FileItem {
83105
file_type,
84106
path,
@@ -98,7 +120,14 @@ impl FileItem {
98120
let name = path_obj.to_str().unwrap().to_string();
99121
let extension = Self::get_extension(path_obj, is_dir);
100122
let size = if is_dir { 0 } else { path_obj.metadata().unwrap().len() };
101-
let modify_time = path_obj.metadata().unwrap().modified().unwrap().duration_since(UNIX_EPOCH).unwrap().as_secs();
123+
let modify_time = path_obj
124+
.metadata()
125+
.unwrap()
126+
.modified()
127+
.unwrap()
128+
.duration_since(UNIX_EPOCH)
129+
.unwrap()
130+
.as_secs();
102131
FileItem {
103132
file_type,
104133
path,

soda_resource_tools_lib/src/soda/meta.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ pub fn create_metadata_mt(meta_context: &mut MetaContext) -> Result<MTMetadata,
2222
if let Some(meta) = gen_metadata_mt(&cur_file_name, tokens) {
2323
let end_time = SystemTime::now();
2424
tracing::debug!("meta = {:?} ", meta);
25-
tracing::debug!("create metadata success title = {:?} time = {:?}", cur_file_name, (end_time.duration_since(start_time)).unwrap());
25+
tracing::debug!(
26+
"create metadata success title = {:?} time = {:?}",
27+
cur_file_name,
28+
(end_time.duration_since(start_time)).unwrap()
29+
);
2630
return Ok(meta);
2731
}
2832
}

soda_resource_tools_lib/src/soda/request.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ pub(crate) fn blocking_request_value_with_cache(cache_type: CacheType, method: &
5858
}
5959

6060
pub(crate) fn blocking_request(method: &str, url: &str) -> Result<reqwest::blocking::Response, SodaError> {
61-
Ok(if method == "GET" { blocking_request_get(url)? } else { blocking_request_post(url)? })
61+
Ok(if method == "GET" {
62+
blocking_request_get(url)?
63+
} else {
64+
blocking_request_post(url)?
65+
})
6266
}
6367

6468
pub(crate) fn blocking_request_post(url: &str) -> Result<reqwest::blocking::Response, SodaError> {

soda_resource_tools_lib/src/soda/tmdb/movie.rs

-2
Original file line numberDiff line numberDiff line change
@@ -163,5 +163,3 @@ pub(crate) fn movie_details(tmdb_id: &str) -> Result<TmdbMovie, SodaError> {
163163
Err(e) => Err(SodaError::String(format!("movie_details json error {:?}", e))),
164164
}
165165
}
166-
167-

soda_resource_tools_lib/src/soda/tmdb/request.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ pub(crate) fn tmdb_request(action: &str, params: &str, method: &str) -> Result<V
1313
})
1414
.unwrap();
1515

16-
let url = if params.contains("language") { format!("https://{}/3{}?api_key={}&{}", get_api_domain(), action, api_key, params) } else { format!("https://{}/3{}?api_key={}&{}&language={}", get_api_domain(), action, api_key, params, get_api_language()) };
16+
let url = if params.contains("language") {
17+
format!("https://{}/3{}?api_key={}&{}", get_api_domain(), action, api_key, params)
18+
} else {
19+
format!(
20+
"https://{}/3{}?api_key={}&{}&language={}",
21+
get_api_domain(),
22+
action,
23+
api_key,
24+
params,
25+
get_api_language()
26+
)
27+
};
1728

1829
let json = request::blocking_request_value_with_cache(cache::CacheType::TMDB, method, &url)?;
1930

soda_resource_tools_lib/src/soda/tmdb/scraper.rs

-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,6 @@ fn save_movie_images(tmdb: &TmdbMovie, fanart: &FanartMovie, root_path: &Path) {
267267
tracing::debug!("backdrop image exist, skip save backdrop image, image_path = {:?}", image_path);
268268
}
269269
}
270-
271270
}
272271

273272
fn save_tv_show_images(tmdb: &TmdbTV, fanart: &FanartTV, root_path: &Path) {

0 commit comments

Comments
 (0)