(cleanup): cleanup on src/

This commit is contained in:
socatlolmeow 2026-10-10 18:04:20 +02:00
commit 576801f108
10 changed files with 503 additions and 193 deletions

View file

@ -16,7 +16,9 @@ pub fn search(query: &str) -> Result<Vec<Track>, String> {
Ok(tracks) if !tracks.is_empty() => Ok(tracks),
Ok(_) => ytdlp_search(query).map_err(|e| format!("{e:#}")),
Err(primary) => ytdlp_search(query).map_err(|fallback| {
format!("YouTube Music search failed ({primary:#}); yt-dlp fallback failed ({fallback:#})")
format!(
"YouTube Music search failed ({primary:#}); yt-dlp fallback failed ({fallback:#})"
)
}),
}
}
@ -25,7 +27,7 @@ pub fn import(url: &str) -> Result<Vec<Track>, String> {
if !(url.starts_with("http://") || url.starts_with("https://")) {
return Err("import expects a full http(s) URL".into());
}
ytdlp_entries(url, &[]).map_err(|e| format!("{e:#}"))
ytdlp_entries(url).map_err(|e| format!("{e:#}"))
}
fn client_version() -> String {
@ -73,7 +75,9 @@ fn search_innertube(query: &str) -> Result<Vec<Track>> {
.set("Cookie", "SOCS=CAI")
.send_json(body)
.map_err(|e| anyhow!("request failed: {e}"))?;
let v: Value = resp.into_json().context("invalid JSON from YouTube Music")?;
let v: Value = resp
.into_json()
.context("invalid JSON from YouTube Music")?;
Ok(parse_search_response(&v))
}
@ -115,7 +119,9 @@ fn column_segments(item: &Value, col: usize) -> Vec<String> {
let mut segments = Vec::new();
let mut cur = String::new();
for run in runs.into_iter().flatten() {
let Some(text) = run.get("text").and_then(Value::as_str) else { continue };
let Some(text) = run.get("text").and_then(Value::as_str) else {
continue;
};
if text.trim() == "•" {
segments.push(std::mem::take(&mut cur));
} else {
@ -142,7 +148,10 @@ fn parse_item(item: &Value) -> Option<Track> {
let title = column_segments(item, 0).into_iter().next()?;
let mut meta = column_segments(item, 1);
if matches!(meta.first().map(String::as_str), Some("Song" | "Video" | "Episode")) {
if matches!(
meta.first().map(String::as_str),
Some("Song" | "Video" | "Episode")
) {
meta.remove(0);
}
let duration = match meta.last() {
@ -153,7 +162,10 @@ fn parse_item(item: &Value) -> Option<Track> {
}
_ => None,
};
let artist = meta.first().cloned().unwrap_or_else(|| "Unknown artist".into());
let artist = meta
.first()
.cloned()
.unwrap_or_else(|| "Unknown artist".into());
let album = meta
.get(1)
.filter(|a| !a.contains(" views") && !a.contains(" plays"))
@ -180,10 +192,10 @@ pub fn parse_search_response(v: &Value) -> Vec<Track> {
}
fn ytdlp_search(query: &str) -> Result<Vec<Track>> {
ytdlp_entries(&format!("ytsearch{FALLBACK_COUNT}:{query}"), &[])
ytdlp_entries(&format!("ytsearch{FALLBACK_COUNT}:{query}"))
}
fn ytdlp_entries(target: &str, extra: &[&str]) -> Result<Vec<Track>> {
fn ytdlp_entries(target: &str) -> Result<Vec<Track>> {
let out = Command::new("yt-dlp")
.args([
"--flat-playlist",
@ -193,7 +205,6 @@ fn ytdlp_entries(target: &str, extra: &[&str]) -> Result<Vec<Track>> {
"--socket-timeout",
"15",
])
.args(extra)
.arg(target)
.stdin(Stdio::null())
.output()