xmpd/scripts/manifest_legacy_to_v1_json.py

53 lines
1.5 KiB
Python
Raw Permalink Normal View History

"""
Converts legacy manifest to v1 json
"""
import uuid
import json
import sys
def main(inp: str, out: str):
manifest = {
"songs": {},
"playlists": {}
}
with open(inp, "r", encoding="utf-8") as f:
data = json.load(f)
_format = data["format"] # unused
for pname in data["playlists"]:
pid = str(uuid.uuid4())
manifest["playlists"][pid] = {
"name": pname,
"author": "Unknown",
"songs": []
}
for sname in data["playlists"][pname]["songs"]:
asn = sname.split(" - ", 2)
author = None
name = None
if len(asn) < 2:
author = "Unknown"
name = sname
else:
author = asn[0]
name = asn[1]
song = data["playlists"][pname]["songs"][sname]
sid = str(uuid.uuid4())
manifest["playlists"][pid]["songs"].append(sid)
manifest["songs"][sid] = {
"name": name,
"author": author,
"url": song["url"],
"source_type": song["typ"]
}
converted = json.dumps(manifest)
with open(out, "w", encoding="utf-8") as f:
f.write(converted)
if __name__ == "__main__":
if len(sys.argv) < 3:
print(f"Usage: {sys.argv[0]} [in] [out]")
sys.exit(1)
main(sys.argv[1], sys.argv[2])