2021-11-16 23:00:45 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-11-17 00:53:50 +01:00
|
|
|
from time import sleep
|
2021-11-16 23:00:45 +01:00
|
|
|
import json
|
|
|
|
import pylistenbrainz
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
2021-11-17 00:53:50 +01:00
|
|
|
def connect_client(auth_token):
|
2021-11-17 12:01:13 +01:00
|
|
|
try:
|
|
|
|
client = pylistenbrainz.ListenBrainz()
|
|
|
|
client.set_auth_token(auth_token)
|
|
|
|
except BaseException:
|
|
|
|
print("Unable to connect to ListenBrainz.")
|
2021-11-17 12:07:07 +01:00
|
|
|
sys.exit(1)
|
2021-11-17 00:53:50 +01:00
|
|
|
return client
|
2021-11-16 23:00:45 +01:00
|
|
|
|
2021-11-17 00:53:50 +01:00
|
|
|
|
2021-11-17 12:01:13 +01:00
|
|
|
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.")
|
2021-11-17 12:07:07 +01:00
|
|
|
sys.exit(1)
|
2021-11-17 12:01:13 +01:00
|
|
|
return export_json
|
|
|
|
|
|
|
|
|
2021-11-17 00:53:50 +01:00
|
|
|
def parse_listen(raw):
|
2021-11-16 23:00:45 +01:00
|
|
|
listen = pylistenbrainz.Listen(
|
2021-11-17 00:53:50 +01:00
|
|
|
track_name=raw['track_metadata']['track_name'],
|
|
|
|
artist_name=raw['track_metadata']['artist_name']
|
2021-11-16 23:00:45 +01:00
|
|
|
)
|
|
|
|
if 'listened_at' in raw:
|
|
|
|
listen.listened_at = raw['listened_at']
|
|
|
|
if 'user_name' in raw:
|
|
|
|
listen.username = raw['user_name']
|
|
|
|
if 'release_name' in raw['track_metadata']:
|
|
|
|
listen.release_name = raw['track_metadata']['release_name']
|
|
|
|
if 'additional_info' in raw['track_metadata']:
|
|
|
|
listen.additional_info = raw['track_metadata']['additional_info']
|
|
|
|
if 'tracknumber' in raw['track_metadata']['additional_info']:
|
|
|
|
listen.tracknumber = raw['track_metadata']['additional_info']['tracknumber']
|
|
|
|
if 'spotify_id' in raw['track_metadata']['additional_info']:
|
|
|
|
listen.spotify_id = raw['track_metadata']['additional_info']['spotify_id']
|
|
|
|
if 'isrc' in raw['track_metadata']['additional_info']:
|
|
|
|
listen.spotify_id = raw['track_metadata']['additional_info']['isrc']
|
|
|
|
if 'listening_from' in raw['track_metadata']['additional_info']:
|
|
|
|
listen.listening_from = raw['track_metadata']['additional_info']['listening_from']
|
|
|
|
if 'mbid_mapping' in raw['track_metadata']:
|
|
|
|
if 'recording_mbid' in raw['track_metadata']['mbid_mapping']:
|
|
|
|
listen.recording_mbid = raw['track_metadata']['mbid_mapping']['recording_mbid']
|
|
|
|
if 'artist_mbids' in raw['track_metadata']['mbid_mapping']:
|
|
|
|
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']
|
2021-11-17 00:53:50 +01:00
|
|
|
return listen
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
client = connect_client(sys.argv[1])
|
2021-11-17 12:01:13 +01:00
|
|
|
exported = open_export(sys.argv[2])
|
2021-11-17 11:31:17 +01:00
|
|
|
print("Opened file with {} listens, starting import...".format(len(exported)))
|
|
|
|
imported = 0
|
2021-11-17 00:53:50 +01:00
|
|
|
for raw_listen in exported:
|
|
|
|
listen = parse_listen(raw_listen)
|
|
|
|
for x in range(0, 5):
|
|
|
|
try:
|
|
|
|
client.submit_single_listen(listen)
|
2021-11-17 11:31:17 +01:00
|
|
|
imported = imported + 1
|
|
|
|
print("Imported {} listens.".format(imported), end='\r')
|
2021-11-17 00:53:50 +01:00
|
|
|
submit_error = None
|
|
|
|
except BaseException:
|
|
|
|
submit_error = True
|
|
|
|
|
|
|
|
if submit_error:
|
2021-11-17 11:31:17 +01:00
|
|
|
sleep_time = x * 5
|
|
|
|
print(
|
|
|
|
"\nError occurred, retrying in {} seconds...".format(sleep_time))
|
|
|
|
sleep(sleep_time)
|
2021-11-17 00:53:50 +01:00
|
|
|
else:
|
|
|
|
break
|
2021-11-17 12:01:13 +01:00
|
|
|
print("\nSuccesfully imported {} listens.".format(imported))
|
2021-11-17 00:53:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|