improve write file + restore ELS format

This commit is contained in:
2025-10-22 17:02:58 +02:00
parent e761946398
commit 95a6d0d6a1

View File

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