improve write file + restore ELS format
This commit is contained in:
@@ -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"))
|
with open(output_filename, "w", encoding="utf-8") as ofile:
|
||||||
for persistent_id, song in elements.items():
|
match json_style:
|
||||||
if els:
|
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 = {
|
json_track_index = {
|
||||||
"index": {
|
"index": {
|
||||||
"_index": els_index,
|
"_index": f"itunes-{element_type}",
|
||||||
"_id": persistent_id,
|
"_id": el["Persistent ID"],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
# file.write(bytes(json.dumps(json_track_index, indent=None, cls=SetEncoder), 'UTF-8'))
|
json.dump(json_track_index, ofile, **json_dump_option)
|
||||||
# file.write(bytes("\n", 'UTF-8'))
|
ofile.write("\n")
|
||||||
|
json.dump(el, ofile, **json_dump_option)
|
||||||
ofile.write(
|
ofile.write("\n")
|
||||||
bytes(json.dumps(song, indent=None, cls=SetEncoder), "UTF-8")
|
case _:
|
||||||
)
|
print("ERROR: no write format")
|
||||||
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"))
|
|
||||||
|
|
||||||
|
|
||||||
#### 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!")
|
||||||
|
|||||||
Reference in New Issue
Block a user