import time, sqlite3, sys
sys.path.insert(1, 'j:\\Spotty\\secrets_and_database')

target_database = r"j:\Spotty\secrets_and_database\\Enormous Feed.db"
source_database = r"j:\Spotty\secrets_and_database\\Enormous Feed2.db"
uri1_list = []
title1_list = []
artist1_list = []
album1_list = []
title_good = 0
title_change = 0
title_not_there = 0
artist_good = 0
artist_change = 0
artist_not_there = 0
album_good = 0
album_change = 0
album_not_there = 0
record1 = 0
record2 = 0
tit = open("title_change.txt", "w")
tit.close()
art = open("artist_change.txt", "w")
art.close()
alb = open("album_change.txt", "w")
alb.close()

def create_connection(db_file):  # NOTE Connect to the database
    conn = None
    conn = sqlite3.connect(db_file)
    return conn

conn1 = create_connection(source_database) #EF2
cur1 = conn1.cursor()

conn2 = create_connection(target_database) #EF
cur2 = conn2.cursor()

cur1.execute("SELECT * FROM New_Track_List ORDER BY ARTIST_NAME ASC") #EF2
rows1 = cur1.fetchall() #EF2
for row1 in rows1: #EF2
    print("Working on Record " + str(record1), end='\r')
    uri1 = row1[0] #EF2
    title1 = row1[1] #EF2
    artist1 = row1[2] #EF2
    album1 = row1[4] #EF2
    uri1_list.append(uri1) #EF2
    title1_list.append(title1) #EF2
    artist1_list.append(artist1) #EF2
    album1_list.append(album1) #EF2
    record1+=1

print()
cur2.execute("SELECT * FROM Track_List ORDER BY ARTIST_NAME ASC") #EF
rows2 = cur2.fetchall() #EF
for row2 in rows2: #EF
    print("Working on Record " + str(record2), end='\r')
    uri2 = row2[0] #EF
    title2 = row2[1] #EF
    artist2 = row2[2] #EF
    album2 = row2[3] #EF

    try:
        uri_index = uri1_list.index(uri2)
    except:
        title_not_there+=1
        #print("MISSING " + str(uri2) + " - " + str(track2) + " - " + str(artist2))
        continue
    new_title = title1_list[uri_index] #EF2
    if title2 == new_title: # if EF = EF2
        title_good+=1
    else:
        title_change+=1 #if EF != EF2   
        tit_upd = "Changing:- URI= '" + str(uri2) + "', from Title= '" + str(title2) + "' TO Title= '" + str(new_title) + "', Artist= '" + str(artist2) + "', Album= '" + str(album2) + '\n'"'"
        tit = open("title_change.txt", "a")
        tit.write(tit_upd)
        tit.close()
        new_title = new_title.replace('"', 'inch')
        cur2.execute('UPDATE Track_List SET TRACK_NAME = "' + new_title + '" WHERE URI= "' + uri2 + '"')
        conn2.commit()

    try:
        uri_index = uri1_list.index(uri2)
    except:
        artist_not_there+=1
        #print("MISSING " + str(uri2) + " - " + str(title2) + " - " + str(artist2))
        continue
        #record2+=1
    new_artist = artist1_list[uri_index]
    if artist2 == new_artist:
        artist_good+=1
    else:
        artist_change+=1
        art_upd = "Changing:- URI= '" + str(uri2) + "', Title= '" + str(title2) + "', From Artist = '" + str(artist2) + "'TO Artist= '" + str(new_artist) + "', Album= '" + str(album2) + '\n'"'"
        art = open("artist_change.txt", "a")
        art.write(art_upd)
        art.close()
        new_artist = new_artist.replace("'", "\'")
        cur2.execute('UPDATE Track_List SET ARTIST_NAME = "' + new_artist + '" WHERE URI= "' + uri2 + '"')
        conn2.commit()

    try:
        uri_index = uri1_list.index(uri2)
    except:
        album_not_there+=1
        print("MISSING " + str(uri2) + " - " + str(album2) + " - " + str(album2))
        continue
        #record2+=1
    new_album = album1_list[uri_index]
    if album2 == new_album:
        album_good+=1
    else:
        album_change+=1
        alb_upd = "Changing:- URI= '" + str(uri2) + "', Title= '" + str(title2) + "', Artist = '" + str(artist2) + "'From Album= '" + str(album2) + "'TO Album= '" + str(new_album) + '\n'"'"
        alb = open("album_change.txt", "a")
        alb.write(alb_upd)
        alb.close()
        new_album = new_album.replace("'", "\'")
        cur2.execute('UPDATE Track_List SET ALBUM_NAME = "' + new_album + '" WHERE URI= "' + uri2 + '"')
        conn2.commit()
    record2+=1

print()
print(artist_good)
print(artist_change)
print(artist_not_there)
print(title_good)
print(title_change)
print(title_not_there)
print(album_good)
print(album_change)
print(album_not_there)
print()
