Skip to content

Instantly share code, notes, and snippets.

@zag2me
Created September 28, 2018 20:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zag2me/9b8625100ded08c08ab9e6bc1c580b50 to your computer and use it in GitHub Desktop.
Save zag2me/9b8625100ded08c08ab9e6bc1c580b50 to your computer and use it in GitHub Desktop.
# Python 2.7
import sqlite3
from sqlite3 import Error
# Function to write a line to the log file
def write_to_log(message):
with open('log.txt', 'a') as the_file:
the_file.write(message.encode('utf-8') + '\n')
# Create a database connection
def create_connection(db_file):
try:
conn = sqlite3.connect(db_file)
return conn
except Error as e:
print(e)
return None
# Select records from the Last.FM database with artist filter
def select_task_by_artist(conn, artist):
cur = conn.cursor()
cur.execute("SELECT * FROM plays WHERE artist=?", (artist,))
rows = cur.fetchall()
for row in rows:
print(row)
# show records from the Last.FM database with artist/title filter
def list_by_artist_title(conn, artist, title):
cur = conn.cursor()
cur.execute("SELECT * FROM plays WHERE artist=? AND title=?", (artist, title))
rows = cur.fetchall()
for row in rows:
print(row)
# show records from the Kodi database with artist/title filter
def kodi_by_artist_title(conn2, artist, title):
cur = conn2.cursor()
cur.execute("SELECT * FROM song WHERE strArtistDisp=? AND strTitle=?", (artist, title))
rows = cur.fetchall()
for row in rows:
print(row)
# Count records from the Last.FM database with artist/title filter
def count_by_artist_title(conn, artist, title):
counter = 0
cur = conn.cursor()
cur.execute("SELECT * FROM plays WHERE artist=? AND title=?", (artist, title))
rows = cur.fetchall()
# Loop through the rows and count up as we go along
for row in rows:
counter = counter + 1
return counter
def update_task(conn2, plays, artist, title):
# Update the song database iTimePlayed record with a new number
sql = "UPDATE song " \
"SET iTimesPlayed = %s " \
"WHERE idSong IN (SELECT idSong FROM song WHERE strArtistDisp = \'%s' " \
"AND strTitle = \'%s\' " \
"ORDER BY idSong ASC LIMIT 1)" \
% (plays, artist, title)
# Write the SQL to the screen
print"DEBUG:", sql
# Execute the SQL statement to update the database
c = conn2.cursor()
c.execute(sql)
# Write the SQL to a log file so we can check it
# write_to_log(sql)
def main():
# create a database connection
database = "databases\lastfm.db"
conn = create_connection(database)
# create a database2 connection
database2 = "databases\MyMusic72.db"
conn2 = create_connection(database2)
# Itterate through all the last fm database 1 by 1
cur = conn.cursor()
cur.execute("SELECT DISTINCT Artist, Title FROM plays")
rows = cur.fetchall()
for row in rows:
# Copy the artist and title here and remove ' as i'm too tired to work out prepared statements for now
artistName = row[0].replace("'", "")
songTitle = row[1].replace("'", "")
# Search for the number of plays found in the last fm database
with conn:
print
print "Number of track plays in Last.fm history found for:", \
artistName, "-", \
songTitle, "=", \
count_by_artist_title(conn, artistName, songTitle)
#Assign the number to a variable
trackCount = count_by_artist_title(conn, artistName, songTitle)
# print "Track Count = ", trackCount
with conn2:
# Now update the Kodi Database
update_task(conn2, trackCount, artistName, songTitle)
# The main program start
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment