Skip to content

Commit

Permalink
Merge pull request #10 from roos-robert/multiple-plays-scrobbler
Browse files Browse the repository at this point in the history
Inserting new records/updating existing, evaluating if new listen
  • Loading branch information
luisignaciocc authored Jun 13, 2024
2 parents 3009a80 + 2914f38 commit 291d2ae
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def __init__(self):
track_name TEXT,
artist_name TEXT,
album_name TEXT,
scrobbled_at TEXT DEFAULT CURRENT_TIMESTAMP
scrobbled_at TEXT DEFAULT CURRENT_TIMESTAMP,
array_position INTEGER
)
''')
self.conn.commit()
Expand Down Expand Up @@ -103,26 +104,48 @@ def execute(self):
history = ytmusic.get_history()
i = 0
cursor = self.conn.cursor()
for item in history:
for index, item in enumerate(history):
if item["played"] == "Today":
record = {
"artistName": item["artists"][0]["name"],
"trackName": item["title"],
"ts": self.formatted_date,
"albumName": item["album"]["name"] if "album" in item and item["album"] is not None else None,
"arrayPosition": index,
}
if record["artistName"].endswith(" - Topic"):
continue
if record["albumName"] is None:
record["albumName"] = record["trackName"]

scroble = cursor.execute(
'SELECT * FROM scrobbles WHERE track_name = :trackName AND artist_name = :artistName AND album_name = :albumName', {
"trackName": record["trackName"],
"artistName": record["artistName"],
"albumName": record["albumName"]
}).fetchone()
if scroble:

if scroble is None:
# No existing record, insert a new one
cursor.execute('''
INSERT INTO scrobbles (track_name, artist_name, album_name, scrobbled_at, array_position)
VALUES (:trackName, :artistName, :albumName, :ts, :arrayPosition)
''', record)
self.conn.commit()
print(f"Inserted new scrobble for {record['trackName']} by {record['artistName']}.")
elif scroble[5] > record["arrayPosition"]:
# Existing record found and needs to be updated
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()
print(f"Updated scrobble for {record['trackName']} by {record['artistName']} with new array position.")
else:
# Existing record found and no update is needed
continue

xml_response = lastpy.scrobble(
record["trackName"],
record["artistName"],
Expand All @@ -135,21 +158,16 @@ def execute(self):
accepted = scrobbles.get('accepted')
ignored = scrobbles.get('ignored')
if accepted == '0' and ignored == '1':
print("Error scrobbling " + record["trackName"] +
" by " + record["artistName"] + ".")
print(f"Error scrobbling {record['trackName']} by {record['artistName']}.")
print(xml_response)
else:
cursor.execute('''
INSERT INTO scrobbles (track_name, artist_name, album_name, scrobbled_at)
VALUES (:trackName, :artistName, :albumName, :ts)
''', record)
self.conn.commit()
i += 1
print("Scrobbled " + str(i) + " songs")

print(f"Scrobbled {i} songs")

cursor.close()
self.conn.close()


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

0 comments on commit 291d2ae

Please sign in to comment.