diff --git a/lb_import.py b/lb_import.py index 4f6f124..09f6f15 100755 --- a/lb_import.py +++ b/lb_import.py @@ -1,20 +1,21 @@ #!/usr/bin/env python3 +from time import sleep import json import pylistenbrainz import sys -auth_token = sys.argv[1] -client = pylistenbrainz.ListenBrainz() -client.set_auth_token(auth_token) -with open(sys.argv[2], "r") as read_file: - data = json.load(read_file) +def connect_client(auth_token): + client = pylistenbrainz.ListenBrainz() + client.set_auth_token(auth_token) + return client -for raw in data: + +def parse_listen(raw): listen = pylistenbrainz.Listen( - track_name = raw['track_metadata']['track_name'], - artist_name = raw['track_metadata']['artist_name'] + track_name=raw['track_metadata']['track_name'], + artist_name=raw['track_metadata']['artist_name'] ) if 'listened_at' in raw: listen.listened_at = raw['listened_at'] @@ -39,5 +40,29 @@ for raw in data: listen.artist_mbids = raw['track_metadata']['mbid_mapping']['artist_mbids'] if 'release_mbid' in raw['track_metadata']['mbid_mapping']: listen.release_mbid = raw['track_metadata']['mbid_mapping']['release_mbid'] - response = client.submit_single_listen(listen) - assert response['status'] == 'ok' + return listen + + +def main(): + client = connect_client(sys.argv[1]) + with open(sys.argv[2], 'r') as read_file: + exported = json.load(read_file) + print("Opened file, starting import...") + for raw_listen in exported: + listen = parse_listen(raw_listen) + for x in range(0, 5): + try: + client.submit_single_listen(listen) + submit_error = None + except BaseException: + submit_error = True + + if submit_error: + print("Error occurred, retrying in 5 seconds...") + sleep(5) + else: + break + + +if __name__ == '__main__': + main()