from datetime import datetime from json import dump, load from requests import Session from mysql.connector import connect from env import ENV if __name__ == "__main__": # Grab a snapshot of the unique_artist table. temp_unique_artist_snapshot_file = "temp_unique_artist_snapshot.json" try: unique_artist_snapshot = load(open(temp_unique_artist_snapshot_file, "r")) except FileNotFoundError: database_connection = connect( host=ENV["database_host"], user=ENV["database_user"], passwd=ENV["database_pass"], database=ENV["database_data"] ) cursor = database_connection.cursor() cursor.execute("SELECT id, rai_id FROM unique_artist") unique_artist_snapshot = { str(rai_id): pk for (pk, rai_id) in cursor.fetchall() } dump(unique_artist_snapshot, open(temp_unique_artist_snapshot_file, "w"), indent=4) database_connection.close() # Grab a snapshot of the artist_info table. temp_artist_info_snapshot_file = "temp_artist_info_snapshot.json" try: artist_info_snapshot = load(open(temp_artist_info_snapshot_file, "r")) except FileNotFoundError: database_connection = connect( host=ENV["database_host"], user=ENV["database_user"], passwd=ENV["database_pass"], database=ENV["database_data"] ) cursor = database_connection.cursor() cursor.execute("SELECT artist_id, unique_artist_id FROM artist_info WHERE unique_artist_id IS NOT NULL") artist_info_snapshot = { str(artist_id): rai_id # Before the migration the value in the unique_artist_id column is the rai_id. for (artist_id, rai_id) in cursor.fetchall() } dump(artist_info_snapshot, open(temp_artist_info_snapshot_file, "w"), indent=4) database_connection.close() # Build artist_info update payloads. temp_artist_info_update_payloads = "temp_artist_info_update_payloads.json" try: artist_info_update_payloads = load(open(temp_artist_info_update_payloads, "r")) except FileNotFoundError: artist_info_update_payloads = {} for artist_id, rai_id in artist_info_snapshot.items(): if rai_id is not None: rai_id = str(rai_id) unique_artist_id = int(unique_artist_snapshot[rai_id]) artist_info_update_payloads[artist_id] = { "unique_artist_id": unique_artist_id } dump(artist_info_update_payloads, open(temp_artist_info_update_payloads, "w"), indent=4) # Use an API endpoint to perform the update. print(datetime.now()) session = Session() counter = 0 for artist_id, payload in artist_info_update_payloads.items(): counter += 1 response = session.put('https://ows-artist.theorchard.io/artist/{}'.format(artist_id), json=payload) if response.status_code != 200: print(counter, artist_id, payload, response.status_code, response.text) break if not counter % 10000: print(counter, datetime.now(), artist_id, payload) print(datetime.now())