Script to Populate Play_Count and Last_Played from

Frequently asked questions. Tutorials, tips and tricks.
Posts: 9
Joined: 14 Jan 2016, 18:26
Has thanked: 1 time
Been thanked: 7 times

Script to Populate Play_Count and Last_Played from

Unread post by baileyb622 » 23 Mar 2016, 22:20

So over the weekend I managed to really mess up my madsonic install, db, and backup db. The ultimate solution was a completely fresh install. Unfortunately this meant I lost all of my play statistics. Since I scrobble everything to I figured there should be a way to scrape that data and update the Madsonic database. Thus, I wrote the following python script: ...

What you will need:
- A API Key:
- Your Username
- Python 2.7.11

1) In the script, replace YOUR_API_KEY_HERE and YOUR_USER_NAME_HERE with the correct values respectively.

2) From the command line run `python`

3) Be patient, depending on your number of scrobbles this could take a while. If you find it takes too long, or you exceed your API queries you can adjust how far back in time to look by changing the for-loops at lines 77 and 97.

4) When the script completes you will find a file called 'temp.txt' in the same directory. Inside of this file should be a long list of UPDATE statements, one for every song you have ever scrobbled to Now you may not have all of these songs in your Madsonic library, but that is ok, the database will simply ignore any songs it cannot find. These statements will set the play_count and last_played fields in your database for each of the songs listed. IMPORTANT - These statements will REPLACE any previous data in these two fields.

5) Go to you database view

6) Copy and paste the UPDATE statements into the DB console and click OK. IMPORTANT - These statements will REPLACE any previous data in these two fields. If you copy too many statements into the console at once it may error out, that's ok. Just refresh the page and try doing fewer queries at a time. I found that about 1000 queries at once was the threshold.

And that's it! You should now have playcount and lastplayed data re-populated in your database!
These users thanked the author baileyb622 for the post (total 4):
Madsonicpa2kMatt ZornigrZn

User avatar
Matt Zornig
Posts: 241
Joined: 14 Feb 2017, 12:03
Has thanked: 38 times
Been thanked: 28 times

Re: Script to Populate Play_Count and Last_Played from Last.

Unread post by Matt Zornig » 20 Feb 2017, 16:57

I don't think, I will ever use this, but I'd like to thank you for contributing this script to the community!
I find your explanation and hints very useful for people looking for such an easy way to get their playcounts back.

Post Reply