Implement retries on server error.
Signed-off-by: Abdulkadir Furkan Şanlı <me@abdulocra.cy>
This commit is contained in:
parent
b4746b2860
commit
a4e56f7a9e
45
lb_import.py
45
lb_import.py
@ -1,20 +1,21 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from time import sleep
|
||||||
import json
|
import json
|
||||||
import pylistenbrainz
|
import pylistenbrainz
|
||||||
import sys
|
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:
|
def connect_client(auth_token):
|
||||||
data = json.load(read_file)
|
client = pylistenbrainz.ListenBrainz()
|
||||||
|
client.set_auth_token(auth_token)
|
||||||
|
return client
|
||||||
|
|
||||||
for raw in data:
|
|
||||||
|
def parse_listen(raw):
|
||||||
listen = pylistenbrainz.Listen(
|
listen = pylistenbrainz.Listen(
|
||||||
track_name = raw['track_metadata']['track_name'],
|
track_name=raw['track_metadata']['track_name'],
|
||||||
artist_name = raw['track_metadata']['artist_name']
|
artist_name=raw['track_metadata']['artist_name']
|
||||||
)
|
)
|
||||||
if 'listened_at' in raw:
|
if 'listened_at' in raw:
|
||||||
listen.listened_at = raw['listened_at']
|
listen.listened_at = raw['listened_at']
|
||||||
@ -39,5 +40,29 @@ for raw in data:
|
|||||||
listen.artist_mbids = raw['track_metadata']['mbid_mapping']['artist_mbids']
|
listen.artist_mbids = raw['track_metadata']['mbid_mapping']['artist_mbids']
|
||||||
if 'release_mbid' in raw['track_metadata']['mbid_mapping']:
|
if 'release_mbid' in raw['track_metadata']['mbid_mapping']:
|
||||||
listen.release_mbid = raw['track_metadata']['mbid_mapping']['release_mbid']
|
listen.release_mbid = raw['track_metadata']['mbid_mapping']['release_mbid']
|
||||||
response = client.submit_single_listen(listen)
|
return listen
|
||||||
assert response['status'] == 'ok'
|
|
||||||
|
|
||||||
|
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