Add final output, account for some errors
Signed-off-by: Abdulkadir Furkan Şanlı <me@abdulocra.cy>
This commit is contained in:
		
							
								
								
									
										16
									
								
								lb_import.py
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								lb_import.py
									
									
									
									
									
								
							@@ -7,11 +7,23 @@ import sys
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def connect_client(auth_token):
 | 
					def connect_client(auth_token):
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
        client = pylistenbrainz.ListenBrainz()
 | 
					        client = pylistenbrainz.ListenBrainz()
 | 
				
			||||||
        client.set_auth_token(auth_token)
 | 
					        client.set_auth_token(auth_token)
 | 
				
			||||||
 | 
					    except BaseException:
 | 
				
			||||||
 | 
					        print("Unable to connect to ListenBrainz.")
 | 
				
			||||||
    return client
 | 
					    return client
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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.")
 | 
				
			||||||
 | 
					    return export_json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def parse_listen(raw):
 | 
					def parse_listen(raw):
 | 
				
			||||||
    listen = pylistenbrainz.Listen(
 | 
					    listen = pylistenbrainz.Listen(
 | 
				
			||||||
        track_name=raw['track_metadata']['track_name'],
 | 
					        track_name=raw['track_metadata']['track_name'],
 | 
				
			||||||
@@ -45,8 +57,7 @@ def parse_listen(raw):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def main():
 | 
					def main():
 | 
				
			||||||
    client = connect_client(sys.argv[1])
 | 
					    client = connect_client(sys.argv[1])
 | 
				
			||||||
    with open(sys.argv[2], 'r') as read_file:
 | 
					    exported = open_export(sys.argv[2])
 | 
				
			||||||
        exported = json.load(read_file)
 | 
					 | 
				
			||||||
    print("Opened file with {} listens, starting import...".format(len(exported)))
 | 
					    print("Opened file with {} listens, starting import...".format(len(exported)))
 | 
				
			||||||
    imported = 0
 | 
					    imported = 0
 | 
				
			||||||
    for raw_listen in exported:
 | 
					    for raw_listen in exported:
 | 
				
			||||||
@@ -67,6 +78,7 @@ def main():
 | 
				
			|||||||
                sleep(sleep_time)
 | 
					                sleep(sleep_time)
 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                break
 | 
					                break
 | 
				
			||||||
 | 
					    print("\nSuccesfully imported {} listens.".format(imported))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user