-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathplaylist-generator.py
More file actions
51 lines (43 loc) · 1.76 KB
/
Copy pathplaylist-generator.py
File metadata and controls
51 lines (43 loc) · 1.76 KB
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
#!/usr/bin/env python3
import json
import os
import sys
import urllib.error
import urllib.parse
import urllib.request
BASE_URL = os.environ.get("ASTRA_API_URL", "http://127.0.0.1:38401").rstrip("/")
TOKEN = os.environ.get("ASTRA_API_TOKEN")
def request(method, path, body=None):
headers = {"Authorization": f"Bearer {TOKEN}"}
encoded = None
if body is not None:
headers["Content-Type"] = "application/json"
encoded = json.dumps(body).encode("utf-8")
try:
with urllib.request.urlopen(
urllib.request.Request(BASE_URL + path, data=encoded, headers=headers, method=method)
) as response:
return json.load(response)
except urllib.error.HTTPError as error:
detail = error.read().decode("utf-8", errors="replace")
raise RuntimeError(f"API request failed: {error.code} {detail}") from error
if not TOKEN:
raise SystemExit("Set ASTRA_API_TOKEN to the bearer token shown in Astra settings.")
if len(sys.argv) < 3:
raise SystemExit("Usage: playlist-generator.py <playlist name> <query> [query ...]")
playlist_name, *queries = sys.argv[1:]
track_refs = []
for query in queries[:100]:
encoded_query = urllib.parse.quote(query, safe="")
result = request("GET", f"/v2/search?q={encoded_query}&types=track&limit=1")
if result.get("results"):
track_refs.append(result["results"][0]["ref"])
else:
print(f"No match: {query}", file=sys.stderr)
if not track_refs:
raise SystemExit("None of the queries matched a track.")
playlist = request("POST", "/v2/playlists", {"name": playlist_name})
request("POST", f"/v2/playlists/{urllib.parse.quote(playlist['ref'], safe='')}/items", {
"trackRefs": track_refs
})
print(f"Created {playlist['title']} with {len(track_refs)} tracks.")