Skip to content

Commit d95f903

Browse files
committed
new behavior, cleaner code
1 parent bf54078 commit d95f903

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lrcsync"
3-
version = "0.1.1"
3+
version = "0.2.0"
44
edition = "2021"
55
description = "a simple tool to sync lrc files from lrclib.net"
66
license = "GPL-3.0"

src/main.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ pub struct CliConfig {
1212
pub lrclib_url: String,
1313
#[arg(short = 'a', long = "hidden", default_value_t = false)]
1414
pub hidden: bool,
15+
#[arg(short = 'f', long = "force", default_value_t = false, help = "overwrite existing lrc files")]
16+
pub force: bool,
17+
#[arg(short = 'i', long = "ignore", value_parser, num_args = 1, help = "ignore the follow properties when searching lrclib by not sending them, comma seperated")]
18+
pub ignore: Vec<String>
1519
}
1620

1721
static DEFAULT_USER_AGENT: &str = concat!(
@@ -37,6 +41,7 @@ pub struct LrclibQuery {
3741
}
3842

3943
impl LrclibQuery {
44+
// old method
4045
pub fn to_query_string(&self) -> String {
4146
let mut query = String::new();
4247
query.push_str("track_name=");
@@ -53,6 +58,27 @@ impl LrclibQuery {
5358
}
5459
query
5560
}
61+
62+
pub fn to_query(&self) -> Vec<(String, String)> {
63+
let mut query = Vec::new();
64+
query.push(("track_name".to_string(), self.track_name.clone()));
65+
query.push(("artist_name".to_string(), self.artist_name.clone()));
66+
if let Some(album_name) = &self.album_name {
67+
query.push(("album_name".to_string(), album_name.clone()));
68+
}
69+
if let Some(duration) = &self.duration {
70+
query.push(("duration".to_string(), duration.to_string()));
71+
}
72+
query
73+
}
74+
75+
pub fn remove_duration(&mut self) {
76+
self.duration = None;
77+
}
78+
79+
pub fn remove_album_name(&mut self) {
80+
self.album_name = None;
81+
}
5682
}
5783

5884
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -82,9 +108,9 @@ impl LrcLibClient {
82108
}
83109

84110
pub async fn get(&self, query: &LrclibQuery) -> anyhow::Result<Option<LrclibItem>> {
85-
86-
let url = format!("{}/api/get?{}" ,self.url, query.to_query_string());
87-
let response = self.client.get(url).send().await?;
111+
let url = format!("{}/api/get" ,self.url);
112+
let request_builder = self.client.get(url).query(&query.to_query());
113+
let response = request_builder.send().await?;
88114
if response.status().is_success() {
89115
let body = response.text().await?;
90116
match serde_json::from_str::<LrclibItem>(&body) {
@@ -122,6 +148,11 @@ async fn main() {
122148
if !is_audio {
123149
continue;
124150
}
151+
let has_existing_lrc = entry.path().with_extension("lrc").exists();
152+
if has_existing_lrc && !config.force {
153+
println!("Skipping {}: lrc file already exists", entry.path().display());
154+
continue;
155+
}
125156
// read file
126157
match Tag::new().read_from_path(entry.path()) {
127158
Ok(tag) => {
@@ -141,12 +172,18 @@ async fn main() {
141172
Some(duration) => Some(duration as f32),
142173
None => None,
143174
};
144-
let lrc_query = LrclibQuery {
175+
let mut lrc_query = LrclibQuery {
145176
track_name: track_name.clone(),
146177
artist_name: artist_name.clone(),
147178
album_name: album_name.clone(),
148179
duration: duration,
149180
};
181+
if config.ignore.contains(&"duration".to_string()) {
182+
lrc_query.remove_duration();
183+
}
184+
if config.ignore.contains(&"album_name".to_string()) {
185+
lrc_query.remove_album_name();
186+
}
150187
match client.get(&lrc_query).await {
151188
Ok(Some(lrc_item)) => {
152189
if let Some(synced_lyrics) = &lrc_item.syncedLyrics {

0 commit comments

Comments
 (0)