Implement retries on server error.
Signed-off-by: Abdulkadir Furkan Şanlı <me@abdulocra.cy>
This commit is contained in:
parent
b4746b2860
commit
a4e56f7a9e
37
lb_import.py
37
lb_import.py
@ -1,17 +1,18 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from time import sleep
|
||||
import json
|
||||
import pylistenbrainz
|
||||
import sys
|
||||
|
||||
auth_token = sys.argv[1]
|
||||
|
||||
def connect_client(auth_token):
|
||||
client = pylistenbrainz.ListenBrainz()
|
||||
client.set_auth_token(auth_token)
|
||||
return client
|
||||
|
||||
with open(sys.argv[2], "r") as read_file:
|
||||
data = json.load(read_file)
|
||||
|
||||
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']
|
||||
@ -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()
|
||||
|
Loading…
Reference in New Issue
Block a user