Skip to content

Commit

Permalink
Merge pull request #11 from roos-robert/multiple-plays-scrobbler
Browse files Browse the repository at this point in the history
Corrections for multiple plays
  • Loading branch information
luisignaciocc authored Jun 13, 2024
2 parents 291d2ae + ca1f0fa commit a51275f
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

load_dotenv()


class TokenHandler(http.server.SimpleHTTPRequestHandler):
def do_get_token(self):
self.send_response(200)
Expand All @@ -30,11 +29,9 @@ def do_GET(self):
else:
http.server.SimpleHTTPRequestHandler.do_GET(self)


class TokenServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
token = None


class Process:
def __init__(self):
self.api_key = os.environ['LAST_FM_API']
Expand All @@ -58,12 +55,6 @@ def __init__(self):
)
''')
self.conn.commit()
yesterday = (datetime.now() - timedelta(days=1)
).strftime('%Y-%m-%d %H:%M:%S')
cursor.execute('''
DELETE FROM scrobbles WHERE scrobbled_at < :yesterday
''', {"yesterday": yesterday})
self.conn.commit()
cursor.close()

def get_token(self):
Expand Down Expand Up @@ -132,7 +123,7 @@ def execute(self):
VALUES (:trackName, :artistName, :albumName, :ts, :arrayPosition)
''', record)
self.conn.commit()
print(f"Inserted new scrobble for {record['trackName']} by {record['artistName']}.")
print(f"NEW: Scrobble for {record['trackName']} by {record['artistName']}.")
elif scroble[5] > record["arrayPosition"]:
# Existing record found and needs to be updated
cursor.execute('''
Expand All @@ -141,9 +132,15 @@ def execute(self):
WHERE track_name = :trackName AND artist_name = :artistName AND album_name = :albumName
''', record)
self.conn.commit()
print(f"Updated scrobble for {record['trackName']} by {record['artistName']} with new array position.")
print(f"UPDATE: Update scrobble for {record['trackName']} by {record['artistName']} with new array position (new listen).")
else:
# Existing record found and no update is needed
# Existing record found that won't be sent to Last.FM, but local records needs updating
cursor.execute('''
UPDATE scrobbles
SET scrobbled_at = :ts, array_position = :arrayPosition
WHERE track_name = :trackName AND artist_name = :artistName AND album_name = :albumName
''', record)
self.conn.commit()
continue

xml_response = lastpy.scrobble(
Expand All @@ -168,6 +165,5 @@ def execute(self):
cursor.close()
self.conn.close()


if __name__ == '__main__':
Process().execute()

0 comments on commit a51275f

Please sign in to comment.