From a4e56f7a9ebb125f5b65e53266623b3d36f807a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Abdulkadir=20Furkan=20=C5=9Eanl=C4=B1?= Date: Wed, 17 Nov 2021 00:53:50 +0100 Subject: [PATCH] Implement retries on server error. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Abdulkadir Furkan Şanlı --- lb_import.py | 45 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 10 deletions(-) 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()