luisignaciocc / youtube-music-scrobbler

a python script to scrobble your YouTube Music history to Last.fm
MIT License
29 stars 5 forks source link

Cleaning database if history not matching DB #12

Closed roos-robert closed 2 months ago

roos-robert commented 2 months ago

This update brings improvements to the multiple scrobbles of a song functionality.

Script now compares the records returned from the YT API to the entries in the local DB. If a record exists in the DB but not in the API response it will be deleted from the DB.

We can assume that since we filter on "Today" that if a record does not exist at time x but then at future time y; it's a new scrobble.

This also keeps the local DB nice and tidy/performance improvements.

It also makes the script work regardless of time-zone of the user since we only modify the local DB based on the API response now.

Sidenote for people looking at this script for educational purpouses: Note that constructing the DELETE statement dynamically can be risky with a large number of records due to SQL injection risks and query length limits. In a production environment, you'd want to handle this more robustly, perhaps by using a temporary table or other safe means. Since we know this will run locally and we are not dealing with enterprise-size clusters of data here I did the query the quick and dirty way.