goosemusic 1.2.0

A music player with YouTube search, local playback, and OS media controls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
use std::{
    io::Write,
    process::{Command, Stdio},
    sync::atomic::{AtomicU64, Ordering},
    time::Duration,
};

use anyhow::{Context, Result};
use serde::Deserialize;

use super::{run_command_with_timeout, ytdlp};
use crate::{
    providers::{
        ArtistAlbumCard, ArtistHeader, CardData, ProviderId, RelatedArtistCard, SearchScope,
        SearchTab,
    },
    types::Track,
};

#[derive(Debug, Clone, serde::Deserialize)]
pub struct YouTubeVideo {
    pub id: String,
    pub title: String,
    pub url: String,
    #[serde(default, deserialize_with = "deserialize_flexible_duration")]
    pub duration: u32,
    #[serde(default)]
    pub channel: String,
    #[serde(default)]
    pub thumbnail: String,
    #[serde(default)]
    pub album: Option<crate::types::TrackAlbum>,
    #[serde(default)]
    pub artist_id: Option<String>,
    /// ytmusicapi's abbreviated view count ("1.2M", "841M plays"), parsed
    /// to an exact number at deserialization; 0 when absent.
    #[serde(default, deserialize_with = "deserialize_view_count")]
    pub views: u64,
}

impl From<YouTubeVideo> for Track {
    fn from(v: YouTubeVideo) -> Self {
        let mut track = Track::from_provider(
            ProviderId::YouTube,
            v.id,
            v.url,
            v.title,
            v.channel,
            v.duration,
            v.thumbnail,
            v.album,
            v.artist_id,
        );
        if let Some(pt) = track.providers.get_mut(&ProviderId::YouTube) {
            pt.play_count = v.views;
        }
        track
    }
}

#[derive(Deserialize)]
struct YTDLPSearchResult {
    id: String,
    title: String,
    #[serde(default, deserialize_with = "deserialize_flexible_duration")]
    duration: u32,
    channel: String,
    #[serde(default)]
    webpage_url: String,
    #[serde(default)]
    view_count: Option<u64>,
}

/// Durations beyond a week are garbage (e.g. "1e30"), not tracks.
const MAX_TRACK_SECS: f64 = 7.0 * 24.0 * 3600.0;

/// Accept either an abbreviated string ("1.2M", "841M plays") or a plain
/// number for view counts.
fn deserialize_view_count<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde_json::Value;
    Ok(match Value::deserialize(deserializer)? {
        Value::Number(n) => n.as_u64().unwrap_or(0),
        Value::String(s) => parse_abbreviated_count(&s),
        _ => 0,
    })
}

/// ytmusicapi emits durations either as seconds (search) or as a raw
/// "M:SS"/"H:MM:SS" string (browse/watch shelves); accept both.
fn deserialize_flexible_duration<'de, D>(deserializer: D) -> Result<u32, D::Error>
where
    D: serde::Deserializer<'de>,
{
    use serde_json::Value;
    let secs = match Value::deserialize(deserializer)? {
        Value::Number(n) => n.as_f64().unwrap_or(0.0),
        Value::String(s) => s
            .split(':')
            .try_fold(0.0_f64, |acc, part| {
                part.trim().parse::<f64>().map(|p| acc * 60.0 + p)
            })
            .unwrap_or(0.0),
        _ => 0.0,
    };
    let secs = if secs.is_finite() && (0.0..MAX_TRACK_SECS).contains(&secs) {
        secs
    } else {
        0.0
    };
    Ok(secs as u32)
}

/// Parse ytmusicapi's view/play counts ("1.2M", "847", "3.4K", "841M plays").
fn parse_abbreviated_count(s: &str) -> u64 {
    let s = s
        .trim()
        .trim_end_matches("plays")
        .trim_end_matches("views")
        .trim()
        .replace(['\u{a0}', ','], "");
    let (num, mult) = match s.chars().last() {
        Some('K' | 'k') => (&s[..s.len() - 1], 1_000u64),
        Some('M' | 'm') => (&s[..s.len() - 1], 1_000_000),
        Some('B' | 'b') => (&s[..s.len() - 1], 1_000_000_000),
        _ => (s.as_str(), 1),
    };
    num.parse::<f64>().map_or(0, |n| (n * mult as f64) as u64)
}

const SEARCH_TIMEOUT: Duration = Duration::from_mins(1);
const PYTHON_TIMEOUT: Duration = Duration::from_secs(30);

/// Write the embedded ytmusicapi script to a unique temp file, run it with
/// `python3` in the given `mode`, and return its stdout. Unique per pid +
/// call counter (concurrent searches used to race on one fixed filename) and
/// removed after the run.
fn run_python(mode: &str, args: &[&str]) -> Result<String> {
    static COUNTER: AtomicU64 = AtomicU64::new(0);
    let script_path = std::env::temp_dir().join(format!(
        "goosemusic_search_{}_{}.py",
        std::process::id(),
        COUNTER.fetch_add(1, Ordering::Relaxed)
    ));
    std::fs::write(&script_path, include_str!("../youtube_search.py"))
        .context("Failed to write ytmusicapi script")?;

    let result = (|| {
        let py = crate::deps::python_exe().ok_or_else(|| {
            anyhow::anyhow!("Python 3 not found; install it to search YouTube Music.")
        })?;
        let output = run_python_with(&py, &script_path, mode, args)?;
        if output.status.success() {
            return Ok(String::from_utf8_lossy(&output.stdout).into_owned());
        }
        let stderr = String::from_utf8_lossy(&output.stderr);
        if is_import_error(&stderr) {
            if let Some(sys_py) = crate::deps::system_python_exe() {
                if sys_py != py {
                    let sys_output = run_python_with(&sys_py, &script_path, mode, args)?;
                    if sys_output.status.success() {
                        return Ok(String::from_utf8_lossy(&sys_output.stdout).into_owned());
                    }
                    let sys_stderr = String::from_utf8_lossy(&sys_output.stderr);
                    anyhow::bail!("ytmusicapi {mode} failed: {sys_stderr}");
                }
            }
        }
        anyhow::bail!("ytmusicapi {mode} failed: {stderr}");
    })();

    let _ = std::fs::remove_file(&script_path);
    result
}

fn run_python_with(
    py: &std::path::Path,
    script: &std::path::Path,
    mode: &str,
    args: &[&str],
) -> Result<std::process::Output> {
    let mut cmd = Command::new(py);
    cmd.arg(script).arg(mode).args(args);
    run_command_with_timeout(&mut cmd, PYTHON_TIMEOUT)
        .context("Failed to run Python. Is it installed?")
}

fn is_import_error(stderr: &str) -> bool {
    stderr.contains("ModuleNotFoundError")
        || stderr.contains("ImportError")
        || stderr.contains("No module named")
}

/// Run a search and split the result into the playable `Track` list (for
/// Songs/Videos) and the `SearchTab` describing which tab is active (carrying
/// the concrete card lists for Artists/Albums/Playlists). yt-dlp is the
/// fallback for pagination (`search_more`) and when ytmusicapi is unavailable;
/// it only yields playable tracks, so the scoped card tabs rely on ytmusicapi.
pub fn search(query: &str, scope: SearchScope, offset: usize) -> Result<(Vec<Track>, SearchTab)> {
    if offset == 0 {
        if let Ok(parts) = search_ytmusic(query, scope) {
            return Ok(parts);
        }
    }
    let videos = search_ytdlp(query, offset, crate::theme::SEARCH_PAGE_SIZE)?;
    let tracks: Vec<Track> = videos.into_iter().map(Track::from).collect();
    Ok((tracks, SearchTab::Songs))
}

/// Browse the contents of an artist/album/playlist, returning its tracks.
/// Album browse payload: the python helper returns `{meta, tracks}` for
/// albums; other kinds (and older helpers) return a bare track list.
#[derive(Deserialize)]
#[serde(untagged)]
enum YtBrowseOutput {
    Album {
        #[serde(default)]
        meta: crate::providers::AlbumMeta,
        #[serde(default)]
        tracks: Vec<YouTubeVideo>,
    },
    Tracks(Vec<YouTubeVideo>),
}

pub fn browse(id: &str, kind: &str) -> Result<(Vec<Track>, Option<crate::providers::AlbumMeta>)> {
    let stdout = run_python("browse", &[id, "50", kind])?;
    match serde_json::from_str::<YtBrowseOutput>(&stdout)
        .context("Failed to parse ytmusicapi browse output")?
    {
        YtBrowseOutput::Album { meta, tracks } => {
            let has_meta = !meta.badge.is_empty() || !meta.date.is_empty();
            let out = if has_meta { Some(meta) } else { None };
            Ok((tracks.into_iter().map(Track::from).collect(), out))
        }
        YtBrowseOutput::Tracks(items) => Ok((items.into_iter().map(Track::from).collect(), None)),
    }
}

/// Raw JSON shape returned by the python helper's `artist_page` mode. The
/// card lists deserialize straight into the shared provider types.
#[derive(Deserialize, Default)]
#[serde(default)]
struct YtArtistPageRaw {
    header: ArtistHeader,
    popular: Vec<YouTubeVideo>,
    albums: Vec<ArtistAlbumCard>,
    playlists: Vec<CardData>,
    related: Vec<RelatedArtistCard>,
}

/// Fetch the full artist page (header, popular songs, album/single/playlist
/// shelves, related artists) via the python helper.
pub fn fetch_artist_page(
    id: &str,
    kinds: &[crate::providers::ArtistDataKind],
) -> Result<crate::providers::ArtistPage> {
    use crate::providers::{ArtistDataKind as K, ArtistPage};

    // YouTube serves the whole page in a single request regardless of which
    // kinds are asked; unrequested kinds are filtered out of the result.
    let stdout = run_python("artist_page", &[id])?;
    let mut raw: YtArtistPageRaw =
        serde_json::from_str(&stdout).context("Failed to parse ytmusicapi artist_page output")?;
    // The songs shelf carries no durations or view counts; those arrive
    // later via [`enrich_track_metadata`] (a batched yt-dlp pass) so the
    // page can render without waiting for it.
    if !K::Popular.wanted(kinds) {
        raw.popular.clear();
    }
    if !K::Header.wanted(kinds) {
        raw.header.stats.clear();
        raw.header.description.clear();
        raw.header.image.clear();
    }
    if !K::Albums.wanted(kinds) {
        raw.albums.clear();
    }
    if !K::Playlists.wanted(kinds) {
        raw.playlists.clear();
    }
    if !K::Related.wanted(kinds) {
        raw.related.clear();
    }
    Ok(ArtistPage {
        header: Some(raw.header),
        popular: raw.popular.into_iter().map(Track::from).collect(),
        albums: raw.albums,
        playlists: raw.playlists,
        related: raw.related,
    })
}

/// Resolve an artist name to a `YouTube` channel browseId via the Artists
/// search scope. Returns `Ok(None)` when nothing matched.
pub fn resolve_artist_id(name: &str) -> Result<Option<String>> {
    let (_, tab) = search_ytmusic(name, SearchScope::Artists)?;
    Ok(match tab {
        SearchTab::Artists(cards) => cards.into_iter().next().map(|c| c.id),
        _ => None,
    })
}

fn search_ytmusic(query: &str, scope: SearchScope) -> Result<(Vec<Track>, SearchTab)> {
    let limit = 20;
    let limit_str = limit.to_string();
    let scope_arg = scope.youtube_filter();
    let stdout = run_python("search", &[query, scope_arg, &limit_str])?;

    let raw: Vec<serde_json::Value> =
        serde_json::from_str(&stdout).context("Failed to parse ytmusicapi output")?;

    let mut tracks: Vec<Track> = Vec::new();
    let mut cards: Vec<CardData> = Vec::new();
    let mut albums: Vec<CardData> = Vec::new();
    let mut playlists: Vec<CardData> = Vec::new();
    for v in raw {
        let kind = v.get("kind").and_then(|k| k.as_str()).unwrap_or("track");
        let id = v["id"].as_str().unwrap_or_default().to_string();
        let title = v["title"].as_str().unwrap_or_default().to_string();
        let subtitle = v["subtitle"].as_str().unwrap_or_default().to_string();
        let thumbnail = v["thumbnail"].as_str().unwrap_or_default().to_string();
        match kind {
            "artist" => cards.push(CardData {
                id,
                title,
                subtitle: String::new(),
                thumbnail,
            }),
            "album" => albums.push(CardData {
                id,
                title,
                subtitle,
                thumbnail,
            }),
            "playlist" => playlists.push(CardData {
                id,
                title,
                subtitle,
                thumbnail,
            }),
            _ => {
                // song / video -> YouTubeVideo
                if let Ok(video) = serde_json::from_value::<YouTubeVideo>(v) {
                    tracks.push(video.into());
                }
            }
        }
    }

    let tab = match scope {
        SearchScope::Songs => SearchTab::Songs,
        SearchScope::Videos => SearchTab::Videos,
        SearchScope::Artists => SearchTab::Artists(cards),
        SearchScope::Albums => SearchTab::Albums(albums),
        SearchScope::Playlists => SearchTab::Playlists(playlists),
    };
    Ok((tracks, tab))
}

fn search_ytdlp(query: &str, offset: usize, page_size: usize) -> Result<Vec<YouTubeVideo>> {
    // yt-dlp cannot parse an empty/blank query ("ytsearchN:" alone is an
    // unsupported url scheme); blank queries are served by the ytmusicapi
    // charts path, so degrade to an empty page instead of erroring.
    if query.trim().is_empty() {
        return Ok(Vec::new());
    }
    // flat_search returns full metadata in a single yt-dlp pass.
    let (videos, _) = flat_search(query, offset + 1, offset + page_size)?;
    Ok(videos)
}

pub fn search_more(query: &str, offset: usize) -> Result<Vec<Track>> {
    // Pagination only works through yt-dlp, which yields playable tracks.
    // (Scoped non-All searches get their first page from ytmusicapi; further
    // pages fall back to general track results — acceptable for "Load More".)
    let videos = search_ytdlp(query, offset, crate::theme::SEARCH_PAGE_SIZE)?;
    Ok(videos.into_iter().map(Track::from).collect())
}

// yt-dlp search pass. The `ytsearchN:` prefix requests up to N results;
// `--playlist-start/--playlist-end` then slice the requested window. (The old
// `--default-search <music-url> --flat-playlist` combo now resolves to channel
// browse ids — `UC...` — which is_video_id() rejects, so every search returned
// zero results.) Each dumped entry already carries duration/channel/views, so
// no second metadata pass is required. Playlist offsets are 1-based per
// yt-dlp's --playlist-start/--playlist-end convention; callers must convert
// 0-based offsets to 1-based before calling.
fn flat_search(query: &str, start: usize, end: usize) -> Result<(Vec<YouTubeVideo>, Vec<String>)> {
    let start_str = start.to_string();
    let end_str = end.to_string();
    // `ytsearchN:` caps how many results yt-dlp fetches; use `end` (the upper
    // bound of the requested window) as N so the slice below is always within
    // what was actually fetched.
    let search_spec = format!("ytsearch{end}:{query}");
    let args: Vec<&str> = vec![
        "--dump-json",
        "--no-warnings",
        "--skip-download",
        "--playlist-start",
        &start_str,
        "--playlist-end",
        &end_str,
        &search_spec,
    ];

    let mut cmd =
        crate::deps::yt_dlp_command().context("Failed to run yt-dlp. Is it installed?")?;
    cmd.args(&args);
    let flat_output = run_command_with_timeout(&mut cmd, SEARCH_TIMEOUT)
        .context("Failed to run yt-dlp. Is it installed?")?;

    if !flat_output.status.success() {
        let stderr = String::from_utf8_lossy(&flat_output.stderr);
        anyhow::bail!("yt-dlp search failed: {stderr}");
    }

    let mut videos: Vec<YouTubeVideo> = Vec::new();
    let mut valid_ids: Vec<String> = Vec::new();

    for line in String::from_utf8_lossy(&flat_output.stdout).lines() {
        if line.trim().is_empty() {
            continue;
        }
        if let Ok(item) = serde_json::from_str::<YTDLPSearchResult>(line) {
            let id = item.id;
            // Skip non-video playlist entries (mixes, channels, etc.).
            if !is_video_id(&id) {
                continue;
            }
            valid_ids.push(id.clone());
            videos.push(YouTubeVideo {
                id: id.clone(),
                title: item.title,
                url: if item.webpage_url.is_empty() {
                    format!("https://youtube.com/watch?v={id}")
                } else {
                    item.webpage_url
                },
                duration: item.duration,
                channel: item.channel,
                thumbnail: format!("https://i.ytimg.com/vi/{id}/mqdefault.jpg"),
                album: None,
                views: item.view_count.unwrap_or(0),
                artist_id: None,
            });
        }
    }

    Ok((videos, valid_ids))
}

/// Fill missing duration/view-count data on YouTube-sourced tracks with one
/// batched yt-dlp metadata pass. Used as a second phase after the artist
/// page renders, so popular rows don't wait on yt-dlp.
pub fn enrich_track_metadata(tracks: &mut [crate::types::Track]) {
    let ids: Vec<String> = tracks
        .iter()
        .filter_map(|t| t.provider_id(ProviderId::YouTube).map(str::to_string))
        .collect();
    if ids.is_empty() {
        return;
    }
    let metadata = fetch_batch_metadata(&ids);
    for track in tracks.iter_mut() {
        let Some(id) = track.provider_id(ProviderId::YouTube).map(str::to_string) else {
            continue;
        };
        if let (Some(item), Some(pt)) = (
            metadata.get(&id),
            track.providers.get_mut(&ProviderId::YouTube),
        ) {
            if pt.duration == 0 {
                pt.duration = item.duration;
            }
            // The songs shelf never carries counts; the yt-dlp pass's
            // exact view_count is the authoritative fallback.
            if pt.play_count == 0 {
                pt.play_count = item.view_count.unwrap_or(0);
            }
        }
    }
}

// Single batched metadata pass over yt-dlp for a list of video ids. Returns a
// map keyed by video id; a failed yt-dlp invocation yields an empty map so
// callers gracefully fall back to the cheap flat-search stubs.
fn fetch_batch_metadata(
    valid_ids: &[String],
) -> std::collections::HashMap<String, YTDLPSearchResult> {
    use std::collections::HashMap;
    let mut results: HashMap<String, YTDLPSearchResult> = HashMap::new();

    let Some(path) = crate::deps::resolve_yt_dlp() else {
        return results;
    };
    let Ok(mut child) = Command::new(path)
        .args([
            "--batch-file",
            "-",
            "--dump-json",
            "--skip-download",
            "--no-warnings",
        ])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::null())
        .spawn()
    else {
        return results;
    };

    if let Some(ref mut stdin) = child.stdin {
        for id in valid_ids {
            let _ = writeln!(stdin, "https://youtube.com/watch?v={id}");
        }
    }
    drop(child.stdin.take());

    if let Ok(output) = child.wait_with_output() {
        if output.status.success() {
            for line in String::from_utf8_lossy(&output.stdout).lines() {
                if line.trim().is_empty() {
                    continue;
                }
                if let Ok(item) = serde_json::from_str::<YTDLPSearchResult>(line) {
                    results.insert(item.id.clone(), item);
                }
            }
        }
    }
    results
}

/// Fetch a `YouTube` Music radio/mix playlist via ytmusicapi's watch-playlist
/// engine (the same one behind the site's "Start radio" button). Pass a
/// `video_id` for a song-seeded mix or a `playlist_id` (e.g. an artist
/// browseId) for an artist/playlist mix. Durations and thumbnails are derived
/// locally from the watch-playlist response, so no extra yt-dlp pass is
/// needed (which keeps radio generation to a couple of seconds).
pub fn watch_playlist(video_id: Option<&str>, playlist_id: Option<&str>) -> Result<Vec<Track>> {
    let video_arg = video_id.unwrap_or("");
    let playlist_arg = playlist_id.unwrap_or("");
    let stdout = run_python("watch", &[video_arg, playlist_arg, "50"])?;
    let items: Vec<YouTubeVideo> =
        serde_json::from_str(&stdout).context("Failed to parse ytmusicapi watch output")?;
    Ok(items.into_iter().map(Track::from).collect())
}

/// Build a song radio from a real `YouTube` Music mix seeded by the track's
/// `video_id`.
pub fn radio_song(video_id: &str) -> Result<Vec<Track>> {
    watch_playlist(Some(video_id), None)
}

/// Build an artist radio from a real `YouTube` Music mix seeded by the artist's
/// `browse_id` (resolved from the track's provider artist id).
pub fn radio_artist(browse_id: &str) -> Result<Vec<Track>> {
    watch_playlist(None, Some(browse_id))
}

/// Resolve a logical track (title + artist) to a `YouTube` video id by running a
/// yt-dlp search and returning the first result's id. Used by the "play via /
/// download from `YouTube`" flow when a track lacks a `YouTube` id.
pub fn resolve_id(track: &Track) -> Result<Option<Track>> {
    let query = track.search_query();
    let (videos, _) = flat_search(&query, 1, 1)?;
    Ok(videos.into_iter().next().map(|mut v| {
        if v.url.is_empty() {
            v.url = format!("https://www.youtube.com/watch?v={}", v.id);
        }
        Track::from(v)
    }))
}

pub fn download(video_url: &str, download_dir: &str) -> Result<String> {
    let id = video_url
        .split("v=")
        .nth(1)
        .and_then(|s| s.split('&').next())
        .unwrap_or("download");
    let dir = std::path::Path::new(download_dir);
    let _ = std::fs::create_dir_all(dir);
    let output_path = dir.join(format!("{id}.mp3"));
    download_audio(video_url, output_path.to_string_lossy().as_ref())
}

pub fn download_audio(video_url: &str, output_path: &str) -> Result<String> {
    #[cfg(target_os = "linux")]
    let extra_args = &["--extractor-args", "youtube:player_client=web_embedded"];
    #[cfg(not(target_os = "linux"))]
    let extra_args: &[&str] = &[];
    ytdlp::download_audio(video_url, output_path, extra_args)
}

/// Whether `id` looks like a real `YouTube` video `id` (11 chars, not a
/// mix/playlist/channel entry). yt-dlp's flat playlist also returns mixes
/// (`MPRE` prefix), channel uploads (`UC` prefix), and other non-video
/// entries whose ids are longer than the 11-character video id; those are
/// filtered out so the search results stay playable.
fn is_video_id(id: &str) -> bool {
    id.len() == 11 && !id.starts_with("MPRE") && !id.starts_with("UC")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn deserializes_view_count_shapes() {
        let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
            "id": "a", "title": "t", "url": "u", "views": "841M plays"
        }))
        .unwrap();
        assert_eq!(v.views, 841_000_000);
        let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
            "id": "a", "title": "t", "url": "u", "views": null
        }))
        .unwrap();
        assert_eq!(v.views, 0);
        let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
            "id": "a", "title": "t", "url": "u", "views": 841_234
        }))
        .unwrap();
        assert_eq!(v.views, 841_234);
        for fractional in [-12.5f64, 841_234.7, u64::MAX as f64 * 2.0] {
            let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
                "id": "a", "title": "t", "url": "u", "views": fractional
            }))
            .unwrap();
            assert_eq!(v.views, 0, "non-u64 number {fractional} degrades to 0");
        }
    }

    #[test]
    fn deserializes_mixed_duration_shapes() {
        let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
            "id": "a", "title": "t", "url": "u",
            "duration": "4:36"
        }))
        .unwrap();
        assert_eq!(v.duration, 276);
        let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
            "id": "a", "title": "t", "url": "u", "duration": 321.0
        }))
        .unwrap();
        assert_eq!(v.duration, 321);
        let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
            "id": "a", "title": "t", "url": "u", "duration": null
        }))
        .unwrap();
        assert_eq!(v.duration, 0);
        for malformed in ["-1:30", "1e30", "4:xx"] {
            let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
                "id": "a", "title": "t", "url": "u", "duration": malformed
            }))
            .unwrap();
            assert_eq!(v.duration, 0, "malformed duration {malformed} -> 0");
        }
        let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
            "id": "a", "title": "t", "url": "u", "duration": 1e30
        }))
        .unwrap();
        assert_eq!(v.duration, 0);
        let v: YouTubeVideo = serde_json::from_value(serde_json::json!({
            "id": "a", "title": "t", "url": "u", "duration": "2:00:05"
        }))
        .unwrap();
        assert_eq!(v.duration, 7205);
    }
    use super::parse_abbreviated_count as p;

    #[test]
    fn parses_abbreviated_counts() {
        assert_eq!(p("847"), 847);
        assert_eq!(p("1.2K"), 1_200);
        assert_eq!(p("966K"), 966_000);
        assert_eq!(p("4.9M"), 4_900_000);
        assert_eq!(p("841M plays"), 841_000_000);
        assert_eq!(p("614M\u{a0}plays"), 614_000_000);
        assert_eq!(p("3.4B views"), 3_400_000_000);
        assert_eq!(p(""), 0);
        assert_eq!(p("garbage"), 0);
        assert_eq!(p(" \u{a0}1.2K "), 1_200);
        assert_eq!(p("-3K"), 0);
    }
}