-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv2liquid.py
More file actions
executable file
·167 lines (148 loc) · 5.5 KB
/
Copy pathcsv2liquid.py
File metadata and controls
executable file
·167 lines (148 loc) · 5.5 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
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
#!/usr/bin/python3
import csv
import os
import json
import tempfile
import shutil
from pathlib import Path
from hashlib import blake2b
from urllib.parse import urlparse
from contextlib import chdir
import argparse
alternate_m3u = "alternate.m3u"
all_m3u = "all.m3u"
hbase_json = "hbase.json"
dbase_json = "dbase.json"
playlists_dir = "/var/app/playlists"
parser = argparse.ArgumentParser(
prog="csv2liquid.py",
formatter_class=argparse.RawDescriptionHelpFormatter,
description=f"""Parse your CSV catalogue file into two .m3u playlists.\n
- {all_m3u}
Contains everything not filtered into alternative.m3u
- {alternate_m3u}
Contain only content filtered by artist matching the --alternative STRING
Radio rotates between playlists by default.
""",
epilog="Praise sweet baby Jeezus!",
)
parser.add_argument(
"-a",
"--alternative",
default="noalternativem3ufile",
help="Artist to match for alternative playlist. e.g. -a Beatles (for only Beatles songs)",
)
parser.add_argument(
"filename",
default="media.csv",
help="Your content catalogue file (default: ./media.csv)",
)
args = parser.parse_args()
def strip_scheme(url: str) -> str:
schemaless = urlparse(url)._replace(scheme="").geturl()
return schemaless[2:] if schemaless.startswith("//") else schemaless
allowed_keys = ["title", "artist", "coverurl", "videourl", "comment", "date", "show"]
stream_dir = Path(__file__).parent.absolute()
finalize_sh = "finalize.sh"
update_docker_sh = "update-docker.sh"
with open(args.filename, "r") as thresh:
rs = csv.reader(thresh)
header = next(rs)
labels = tuple(header)
dr = csv.DictReader(thresh, labels)
target_dir = stream_dir
with chdir(stream_dir):
with (
tempfile.NamedTemporaryFile(
suffix=".tmp", prefix="all", dir=target_dir, delete=False
) as main,
tempfile.NamedTemporaryFile(
suffix=".tmp", prefix="alternate", dir=target_dir, delete=False
) as alternative,
tempfile.NamedTemporaryFile(
suffix=".tmp", prefix="dbase", dir=target_dir, delete=False, mode="w+"
) as dbase,
tempfile.NamedTemporaryFile(
suffix=".tmp", prefix="hash", dir=target_dir, delete=False, mode="w+"
) as hbase,
):
dbs = []
hashes = {}
for row in dr:
str = "annotate:"
h = blake2b(digest_size=16)
h.update(json.dumps(row).encode("UTF-8"))
hash = h.hexdigest()
id = json.dumps(hash)
items = []
items.append(f"hash={id}")
try:
cov = json.dumps(strip_scheme(row["coverurl"]))
if len(cov) > 0:
items.append(f"coveruri={cov}")
except Exception:
pass
for key in row:
if key in allowed_keys:
item = json.dumps(row[key])
items.append(f"{key}={item}")
str += ",".join(items)
str += f':wget:{row["url"]}\n'
if args.alternative in row["artist"]:
alternative.write(str.encode("UTF-8"))
else:
main.write(str.encode("UTF-8"))
outrow = dict(row)
outrow.update({"hash": hash})
dbs.append(outrow)
outrow.update({"m3u": str})
hashes.update({hash: outrow})
json.dump(dbs, dbase, indent=2)
json.dump(hashes, hbase, indent=2)
print(f"List created -> {main.name}")
print(f"List created -> {alternative.name}")
print(f"DB created -> {dbase.name}")
print(f"Hashes created -> {hbase.name}")
altout = f"'{target_dir}/{alternate_m3u}'"
allout = f"'{target_dir}/{all_m3u}'"
if os.path.exists(finalize_sh):
os.remove(finalize_sh)
if os.path.exists(update_docker_sh):
os.remove(update_docker_sh)
with open(finalize_sh, "w") as f:
print(
f"""#!/bin/bash
### CLEANUP TEMP AND REPLACE EXISTING FILES ###
/bin/rm -vf {altout} {allout};
cp -vf "{hbase.name}" "{hbase_json}"; chmod a+r {hbase_json};
cp -vf "{dbase.name}" "{dbase_json}"; chmod a+r {dbase_json};
# To preserve order you can
# ./finalize.sh noshuf
#
cat "{alternative.name}" | shuf > {altout};
[[ -n $1 ]] && cat "{alternative.name}" > {altout}
chmod a+r {altout}; echo "Created {altout}";
cat "{main.name}" | shuf > {allout};
[[ -n $1 ]] && cat "{main.name}" > {allout}
chmod a+r {allout}; echo "Created {allout}";
rm -vf *.tmp
""",
file=f,
)
print("Run finalize.sh to overwrite existing playlists and JSON data.")
os.chmod(finalize_sh, 0o755)
with open(update_docker_sh, "w") as f:
print(
f"""#!/bin/bash -x
######### OPTIONAL #####################################
### UPDATE PLAYLISTS IN AN ALREADY RUNNING CONTAINER ###
### ###
docker cp {altout} tank:{playlists_dir}/{alternate_m3u}
docker cp {allout} tank:{playlists_dir}/{all_m3u}
docker cp {hbase_json} tank:{playlists_dir}/{hbase_json}
docker cp {dbase_json} tank:{playlists_dir}/{dbase_json}
""",
file=f,
)
os.chmod(update_docker_sh, 0o755)
print("[OPTIONAL] Run update-docker.sh to install the playlists in a running container")