|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Create a fake local music library for --mock-files-dir testing. |
| 3 | +
|
| 4 | +The generated files are not valid FLAC audio, but they have .flac extensions and |
| 5 | +real file sizes. Use with --mock-files-no-read-tags. |
| 6 | +""" |
| 7 | + |
| 8 | +from __future__ import annotations |
| 9 | + |
| 10 | +import argparse |
| 11 | +import csv |
| 12 | +import random |
| 13 | +import re |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | + |
| 17 | +ALBUMS: list[tuple[str, str]] = [ |
| 18 | + ("Radiohead", "In Rainbows"), |
| 19 | + ("Daft Punk", "Discovery"), |
| 20 | + ("Kendrick Lamar", "To Pimp a Butterfly"), |
| 21 | + ("Fleetwood Mac", "Rumours"), |
| 22 | + ("Nirvana", "Nevermind"), |
| 23 | + ("The Beatles", "Abbey Road"), |
| 24 | + ("Pink Floyd", "The Dark Side of the Moon"), |
| 25 | + ("Stevie Wonder", "Songs in the Key of Life"), |
| 26 | + ("Joni Mitchell", "Blue"), |
| 27 | + ("Miles Davis", "Kind of Blue"), |
| 28 | + ("The Strokes", "Is This It"), |
| 29 | + ("Arcade Fire", "Funeral"), |
| 30 | + ("Portishead", "Dummy"), |
| 31 | + ("Massive Attack", "Mezzanine"), |
| 32 | + ("Bjork", "Homogenic"), |
| 33 | + ("Aphex Twin", "Selected Ambient Works 85-92"), |
| 34 | + ("The Cure", "Disintegration"), |
| 35 | + ("Prince", "Purple Rain"), |
| 36 | + ("Kate Bush", "Hounds of Love"), |
| 37 | + ("David Bowie", "Low"), |
| 38 | + ("Lauryn Hill", "The Miseducation of Lauryn Hill"), |
| 39 | + ("OutKast", "Aquemini"), |
| 40 | + ("A Tribe Called Quest", "The Low End Theory"), |
| 41 | + ("Public Enemy", "It Takes a Nation of Millions"), |
| 42 | + ("Nas", "Illmatic"), |
| 43 | + ("Wu-Tang Clan", "Enter the Wu-Tang"), |
| 44 | + ("The Clash", "London Calling"), |
| 45 | + ("Television", "Marquee Moon"), |
| 46 | + ("Joy Division", "Unknown Pleasures"), |
| 47 | + ("Talking Heads", "Remain in Light"), |
| 48 | + ("Sonic Youth", "Daydream Nation"), |
| 49 | + ("My Bloody Valentine", "Loveless"), |
| 50 | + ("Neutral Milk Hotel", "In the Aeroplane Over the Sea"), |
| 51 | + ("Elliott Smith", "Either Or"), |
| 52 | + ("Sufjan Stevens", "Illinois"), |
| 53 | + ("Bon Iver", "For Emma Forever Ago"), |
| 54 | + ("Frank Ocean", "Blonde"), |
| 55 | + ("FKA twigs", "LP1"), |
| 56 | + ("LCD Soundsystem", "Sound of Silver"), |
| 57 | + ("The Avalanches", "Since I Left You"), |
| 58 | + ("Burial", "Untrue"), |
| 59 | + ("Boards of Canada", "Music Has the Right to Children"), |
| 60 | + ("Tame Impala", "Currents"), |
| 61 | + ("Gorillaz", "Demon Days"), |
| 62 | + ("The National", "Boxer"), |
| 63 | + ("PJ Harvey", "Stories from the City Stories from the Sea"), |
| 64 | + ("Mitski", "Be the Cowboy"), |
| 65 | + ("Phoebe Bridgers", "Punisher"), |
| 66 | + ("The War on Drugs", "Lost in the Dream"), |
| 67 | + ("Wilco", "Yankee Hotel Foxtrot"), |
| 68 | +] |
| 69 | + |
| 70 | +TITLE_WORDS = [ |
| 71 | + "Midnight", |
| 72 | + "Signal", |
| 73 | + "Golden", |
| 74 | + "Static", |
| 75 | + "River", |
| 76 | + "Mirror", |
| 77 | + "City", |
| 78 | + "Garden", |
| 79 | + "Neon", |
| 80 | + "Summer", |
| 81 | + "Winter", |
| 82 | + "Velvet", |
| 83 | + "Satellite", |
| 84 | + "Memory", |
| 85 | + "Dream", |
| 86 | + "Ocean", |
| 87 | + "Street", |
| 88 | + "Horizon", |
| 89 | + "Fever", |
| 90 | + "Echo", |
| 91 | + "Light", |
| 92 | + "Shadow", |
| 93 | + "Palace", |
| 94 | + "Weather", |
| 95 | + "Morning", |
| 96 | + "Night", |
| 97 | + "Glass", |
| 98 | + "Fire", |
| 99 | + "Paper", |
| 100 | + "Silver", |
| 101 | +] |
| 102 | + |
| 103 | + |
| 104 | +def safe_path_part(value: str) -> str: |
| 105 | + value = re.sub(r'[<>:"/\\|?*]', "", value) |
| 106 | + value = re.sub(r"\s+", " ", value).strip() |
| 107 | + return value.rstrip(". ") |
| 108 | + |
| 109 | + |
| 110 | +def make_track_title(rng: random.Random, used: set[str]) -> str: |
| 111 | + while True: |
| 112 | + words = rng.sample(TITLE_WORDS, rng.randint(2, 4)) |
| 113 | + title = " ".join(words) |
| 114 | + if title not in used: |
| 115 | + used.add(title) |
| 116 | + return title |
| 117 | + |
| 118 | + |
| 119 | +def create_fake_file(path: Path, size_bytes: int) -> None: |
| 120 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 121 | + with path.open("wb") as handle: |
| 122 | + handle.truncate(size_bytes) |
| 123 | + |
| 124 | + |
| 125 | +def generate(root: Path, seed: int) -> None: |
| 126 | + rng = random.Random(seed) |
| 127 | + library_dir = root / "mock-library" |
| 128 | + csv_dir = root / "csv" |
| 129 | + csv_dir.mkdir(parents=True, exist_ok=True) |
| 130 | + |
| 131 | + track_rows: list[dict[str, str]] = [] |
| 132 | + album_rows: list[dict[str, str]] = [] |
| 133 | + |
| 134 | + for artist, album in ALBUMS: |
| 135 | + track_count = rng.randint(8, 12) |
| 136 | + album_rows.append({"artist": artist, "title": "", "album": album}) |
| 137 | + used_titles: set[str] = set() |
| 138 | + |
| 139 | + for track_number in range(1, track_count + 1): |
| 140 | + title = make_track_title(rng, used_titles) |
| 141 | + size_bytes = rng.randint(1, 5) * 1024 * 1024 + rng.randint(0, 1023) |
| 142 | + filename = f"{track_number:02d}. {artist} - {title}.flac" |
| 143 | + path = ( |
| 144 | + library_dir |
| 145 | + / safe_path_part(artist) |
| 146 | + / safe_path_part(album) |
| 147 | + / safe_path_part(filename) |
| 148 | + ) |
| 149 | + create_fake_file(path, size_bytes) |
| 150 | + |
| 151 | + track_rows.append({ |
| 152 | + "artist": artist, |
| 153 | + "title": title, |
| 154 | + "album": album, |
| 155 | + }) |
| 156 | + |
| 157 | + selected_tracks = rng.sample(track_rows, 100) |
| 158 | + selected_albums = rng.sample(album_rows, 30) |
| 159 | + |
| 160 | + list_lines: list[str] =[] |
| 161 | + sample_albums = rng.sample(album_rows, 25) |
| 162 | + sample_tracks = rng.sample(track_rows, 25) |
| 163 | + |
| 164 | + for album in sample_albums: |
| 165 | + list_lines.append(f'a:"{album["artist"]} - {album["album"]}"') |
| 166 | + for track in sample_tracks: |
| 167 | + list_lines.append(f'"{track["artist"]} - {track["title"]}"') |
| 168 | + |
| 169 | + rng.shuffle(list_lines) |
| 170 | + |
| 171 | + tracks_csv = csv_dir / "tracks_to_download.csv" |
| 172 | + albums_csv = csv_dir / "albums_to_download.csv" |
| 173 | + list_txt = csv_dir / "list.txt" |
| 174 | + |
| 175 | + with tracks_csv.open("w", newline="", encoding="utf-8") as handle: |
| 176 | + writer = csv.DictWriter(handle, fieldnames=["artist", "title", "album"]) |
| 177 | + writer.writeheader() |
| 178 | + writer.writerows(selected_tracks) |
| 179 | + |
| 180 | + with albums_csv.open("w", newline="", encoding="utf-8") as handle: |
| 181 | + writer = csv.DictWriter(handle, fieldnames=["artist", "title", "album"]) |
| 182 | + writer.writeheader() |
| 183 | + writer.writerows(selected_albums) |
| 184 | + |
| 185 | + with list_txt.open("w", encoding="utf-8") as handle: |
| 186 | + for line in list_lines: |
| 187 | + handle.write(line + "\n") |
| 188 | + |
| 189 | + print(f"Created library: {library_dir}") |
| 190 | + print(f"Created tracks CSV: {tracks_csv}") |
| 191 | + print(f"Created albums CSV: {albums_csv}") |
| 192 | + print(f"Created list.txt: {list_txt}") |
| 193 | + print() |
| 194 | + print("Example commands:") |
| 195 | + print(f' sldl "{tracks_csv}" --mock-files-dir "{library_dir}" --mock-files-no-read-tags --mock-files-slow') |
| 196 | + print(f' sldl "{albums_csv}" --mock-files-dir "{library_dir}" --mock-files-no-read-tags --mock-files-slow') |
| 197 | + print(f' sldl "{list_txt}" --input-type list --mock-files-dir "{library_dir}" --mock-files-no-read-tags --mock-files-slow') |
| 198 | + |
| 199 | + |
| 200 | +def main() -> None: |
| 201 | + parser = argparse.ArgumentParser(description=__doc__) |
| 202 | + parser.add_argument( |
| 203 | + "-o", |
| 204 | + "--output", |
| 205 | + type=Path, |
| 206 | + required=True, |
| 207 | + help="Output directory for the generated library and CSV files.", |
| 208 | + ) |
| 209 | + parser.add_argument( |
| 210 | + "--seed", |
| 211 | + type=int, |
| 212 | + default=55, |
| 213 | + help="Random seed for deterministic fixture generation.", |
| 214 | + ) |
| 215 | + args = parser.parse_args() |
| 216 | + |
| 217 | + generate(args.output, args.seed) |
| 218 | + |
| 219 | + |
| 220 | +if __name__ == "__main__": |
| 221 | + main() |
0 commit comments