Skip to content

Commit

Permalink
Merge pull request #12 from roos-robert/multiple-plays-scrobbler
Browse files Browse the repository at this point in the history
  • Loading branch information
luisignaciocc authored Jun 15, 2024
2 parents a51275f + 602fa92 commit c08fb45
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions start.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,41 @@ def execute(self):
history = ytmusic.get_history()
i = 0
cursor = self.conn.cursor()

# Performing scrobbling of songs, cleaning up local DB if needed first, then adding/updating records to local DB and Last.FM
# Step 1: Collect all today's scrobbles
today_records = []

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"]

today_records.append((record["trackName"], record["artistName"], record["albumName"]))

# Step 2: Delete all records in the database that are not in today's scrobbles
if today_records:
placeholders = ', '.join(['(?, ?, ?)'] * len(today_records))
flat_today_records = [item for sublist in today_records for item in sublist]
cursor.execute(f'''
DELETE FROM scrobbles
WHERE (track_name, artist_name, album_name) NOT IN (
{placeholders}
)
''', flat_today_records)
self.conn.commit()

# Step 3: Insert or update today's scrobbles
i = 0 # Initialize i for scrobble timing
for index, item in enumerate(history):
if item["played"] == "Today":
record = {
Expand Down

0 comments on commit c08fb45

Please sign in to comment.