Script to Populate Play_Count and Last_Played from Last.fm

Frequently asked questions. Tutorials, tips and tricks.
baileyb622
Contributor
Contributor
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 Last.fm

Unread post by baileyb622 »

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 Last.fm I figured there should be a way to scrape that data and update the Madsonic database. Thus, I wrote the following python script: https://github.com/philosowaffle/script ... c_query.py

What you will need:
- A Last.fm API Key: http://www.last.fm/api
- Your Last.fm 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 lastfm_to_madsonic_query.py`

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 Last.fm. 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 http://my.madsonic.com/db.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
Rating: 30.77%
User avatar
Matt Zornig
Contributor
Contributor
Posts: 242
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 »

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