Add final output, account for some errors

Signed-off-by: Abdulkadir Furkan Şanlı <me@abdulocra.cy>
This commit is contained in:
Abdulkadir Furkan Şanlı 2021-11-17 12:01:13 +01:00
parent 1111246cc4
commit 797021bac2
Signed by: afk
GPG Key ID: C8F00588EE6ED1FE

View File

@ -7,11 +7,23 @@ import sys
def connect_client(auth_token):
client = pylistenbrainz.ListenBrainz()
client.set_auth_token(auth_token)
try:
client = pylistenbrainz.ListenBrainz()
client.set_auth_token(auth_token)
except BaseException:
print("Unable to connect to ListenBrainz.")
return client
def open_export(export_file):
try:
with open(export_file, 'r') as read_file:
export_json = json.load(read_file)
except FileNotFoundError:
print("File does not exist.")
return export_json
def parse_listen(raw):
listen = pylistenbrainz.Listen(
track_name=raw['track_metadata']['track_name'],
@ -45,8 +57,7 @@ def parse_listen(raw):
def main():
client = connect_client(sys.argv[1])
with open(sys.argv[2], 'r') as read_file:
exported = json.load(read_file)
exported = open_export(sys.argv[2])
print("Opened file with {} listens, starting import...".format(len(exported)))
imported = 0
for raw_listen in exported:
@ -67,6 +78,7 @@ def main():
sleep(sleep_time)
else:
break
print("\nSuccesfully imported {} listens.".format(imported))
if __name__ == '__main__':