diff --git a/lb_import.py b/lb_import.py index 75d57b8..e1af736 100755 --- a/lb_import.py +++ b/lb_import.py @@ -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__':