improve write file + restore ELS format
This commit is contained in:
@@ -51,7 +51,7 @@ import os
|
||||
import plistlib
|
||||
|
||||
|
||||
class SetEncoder(json.JSONEncoder):
|
||||
class JsonCustomEncoder(json.JSONEncoder):
|
||||
def default(self, obj):
|
||||
if isinstance(obj, set):
|
||||
return list(obj)
|
||||
@@ -66,11 +66,6 @@ class ITunesParser:
|
||||
Parse an iTunes Library and produce JSON - for ELS
|
||||
"""
|
||||
|
||||
SONG_INDEX = "itunes-songs"
|
||||
ALBUM_INDEX = "itunes-albums"
|
||||
ARTIST_INDEX = "itunes-artists"
|
||||
# TODO Put variables in a config files or in a python library
|
||||
|
||||
def __init__(self):
|
||||
self._tracks = {}
|
||||
self._albums = {}
|
||||
@@ -94,7 +89,7 @@ class ITunesParser:
|
||||
plist = plistlib.load(open(library_file, "rb"))
|
||||
return plist["Tracks"]
|
||||
|
||||
def parse(self, library_file):
|
||||
def parse(self, library_file) -> dict:
|
||||
"""
|
||||
Return an output JSON for an ELS Bulk request - Not a correct format
|
||||
This method call process_album & process_artist
|
||||
@@ -262,41 +257,44 @@ class ITunesParser:
|
||||
class WriteElsJson:
|
||||
@staticmethod
|
||||
def write_elements(
|
||||
elements: dict,
|
||||
o_name: str,
|
||||
elements: list,
|
||||
element_type: str,
|
||||
json_style: str,
|
||||
els_index=ITunesParser.SONG_INDEX,
|
||||
els=False,
|
||||
):
|
||||
"""
|
||||
Write songs to a JSON
|
||||
"""
|
||||
output_filename = f"{o_name}.{json_style}"
|
||||
output_filename = f"es-{element_type}.{json_style}"
|
||||
if json_style == "els":
|
||||
output_filename += ".json"
|
||||
|
||||
with io.open(output_filename, "wb") as ofile:
|
||||
if json_style == "json":
|
||||
ofile.write(bytes("[\n", "UTF-8"))
|
||||
for persistent_id, song in elements.items():
|
||||
if els:
|
||||
json_dump_option = {"ensure_ascii": False, "cls": JsonCustomEncoder}
|
||||
|
||||
with open(output_filename, "w", encoding="utf-8") as ofile:
|
||||
match json_style:
|
||||
case "json":
|
||||
json_str = json.dumps(elements, **json_dump_option)
|
||||
ofile.write(
|
||||
json_str.replace("}, {", "},\n{")
|
||||
) # One line = one record
|
||||
case "jsonl":
|
||||
for el in elements:
|
||||
json.dump(el, ofile, **json_dump_option)
|
||||
ofile.write("\n")
|
||||
case "els":
|
||||
for el in elements:
|
||||
json_track_index = {
|
||||
"index": {
|
||||
"_index": els_index,
|
||||
"_id": persistent_id,
|
||||
"_index": f"itunes-{element_type}",
|
||||
"_id": el["Persistent ID"],
|
||||
}
|
||||
}
|
||||
# file.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8'))
|
||||
# file.write(bytes("\n", 'UTF-8'))
|
||||
|
||||
ofile.write(
|
||||
bytes(json.dumps(song, indent=None, cls=SetEncoder), "UTF-8")
|
||||
)
|
||||
if json_style == "json":
|
||||
ofile.write(
|
||||
bytes(",", "UTF-8")
|
||||
) # TODO Doesn't work -> last line...
|
||||
ofile.write(bytes("\n", "UTF-8"))
|
||||
if json_style == "json":
|
||||
ofile.write(bytes("]\n", "UTF-8"))
|
||||
json.dump(json_track_index, ofile, **json_dump_option)
|
||||
ofile.write("\n")
|
||||
json.dump(el, ofile, **json_dump_option)
|
||||
ofile.write("\n")
|
||||
case _:
|
||||
print("ERROR: no write format")
|
||||
|
||||
|
||||
#### main block ####
|
||||
@@ -333,7 +331,7 @@ parser.add_argument(
|
||||
parser.add_argument(
|
||||
"-F",
|
||||
"--format",
|
||||
choices=["json", "jsonl"],
|
||||
choices=["json", "jsonl", "els"],
|
||||
default="json",
|
||||
help="Choose JSON style",
|
||||
)
|
||||
@@ -347,12 +345,15 @@ if __name__ == "__main__":
|
||||
itunes_parser = ITunesParser().parse(args.file)
|
||||
|
||||
print("Writing JSON files...")
|
||||
WriteElsJson.write_elements(itunes_parser["songs"], "es-songs", args.format)
|
||||
|
||||
WriteElsJson.write_elements(
|
||||
itunes_parser["artists"], "es-artists", args.format, ITunesParser.ARTIST_INDEX
|
||||
[x for _, x in itunes_parser["songs"].items()], "songs", args.format
|
||||
)
|
||||
WriteElsJson.write_elements(
|
||||
itunes_parser["albums"], "es-albums", args.format, ITunesParser.ARTIST_INDEX
|
||||
[x for _, x in itunes_parser["artists"].items()], "artists", args.format
|
||||
)
|
||||
WriteElsJson.write_elements(
|
||||
[x for _, x in itunes_parser["albums"].items()], "albums", args.format
|
||||
)
|
||||
|
||||
print("Done!")
|
||||
|
||||
Reference in New Issue
Block a user