PulseCanvas / app · pulsecanvas.ai / scripts / build-radio-stations.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Create genre-labeled stations while retaining All Music and original track metadata.""" |
| 3 | import argparse |
| 4 | import json |
| 5 | from pathlib import Path |
| 6 | import re |
| 7 | |
| 8 | |
| 9 | def build(catalog_path, output, names_path): |
| 10 | catalog_bytes = catalog_path.read_bytes() |
| 11 | catalog = json.loads(catalog_bytes) |
| 12 | names = json.loads(names_path.read_text()) |
| 13 | assert catalog['version'] == 1 |
| 14 | assert len({x['id'] for x in names}) == len(names) |
| 15 | assert len(names) <= 7, 'Keep at most seven genre stations plus All Music' |
| 16 | genres = [genre for station in names for genre in station['genres']] |
| 17 | assert len(set(genres)) == len(genres), 'A genre may belong to only one station' |
| 18 | assert all(station['genres'] and re.fullmatch(r'[a-z0-9-]+', station['id']) for station in names) |
| 19 | assert all(station['id'] != 'all-music' and station['title'].strip() for station in names) |
| 20 | unknown = {track['genre'] for track in catalog['tracks']} - set(genres) |
| 21 | assert not unknown, 'Assign new genres to a station before publishing: ' + ', '.join(sorted(unknown)) |
| 22 | covered = set() |
| 23 | directory = dict(version=1, stations=[dict(id='all-music', title='All Music', |
| 24 | description='Every genre, one station', catalog='catalog.json')]) |
| 25 | output.mkdir(parents=True, exist_ok=True) |
| 26 | for station in names: |
| 27 | tracks = [x for x in catalog['tracks'] if x['genre'] in station['genres']] |
| 28 | if not tracks: |
| 29 | continue |
| 30 | covered.update(x['id'] for x in tracks) |
| 31 | filename = 'station-' + station['id'] + '.json' |
| 32 | # Catalogs share the root so original relative audio URLs remain valid. |
| 33 | (output / filename).write_text(json.dumps(dict(version=1, title=station['title'], tracks=tracks), indent=2, ensure_ascii=False) + '\n') |
| 34 | directory['stations'].append({k: station[k] for k in ['id', 'title', 'description']} | dict(catalog=filename)) |
| 35 | assert covered == {x['id'] for x in catalog['tracks']}, 'Some songs need a genre station in station-names.json' |
| 36 | (output / 'catalog.json').write_bytes(catalog_bytes) |
| 37 | # Publish this index only after all per-station catalogs are uploaded. |
| 38 | (output / 'stations.json').write_text(json.dumps(directory, indent=2, ensure_ascii=False) + '\n') |
| 39 | print(f"Prepared {len(directory['stations'])} stations; {len(covered)} songs; original metadata and audio URLs retained") |
| 40 | |
| 41 | |
| 42 | if __name__ == '__main__': |
| 43 | parser = argparse.ArgumentParser(description=__doc__) |
| 44 | parser.add_argument('--catalog', required=True, type=Path) |
| 45 | parser.add_argument('--output', required=True, type=Path) |
| 46 | parser.add_argument('--names', type=Path, default=Path(__file__).resolve().parents[1] / 'radio/station-names.json') |
| 47 | args = parser.parse_args() |
| 48 | build(args.catalog, args.output, args.names) |