I wrote this Bash script, lf-playcount-image.sh, for two things:
- Get my playcount of currently playing track with MPD on Last.fm, and
- Get the album cover art image URLs on Last.fm, so I can use them to show on Conky.
1 Configuration
1.1 For Last.fm
You will need to create a configuration file in ~/.lf-bash/config with these content:
APIKEY=YOUR_LASTFM_APIKEY
USERNAME=YOUR_LASTFM_USERNAME
For using Track.getinfo API, it’s required to supply API key. Since this is a Bash script, I don’t think it’s wise that I give you my API key even it’s not kind of secret key. You can apply for one. There are two things, I should tell you. First, I don’t have a key, either; Second, in that API page, there is an example link, just below the title. Take a look closer, I am sure you will understand why I want you to look at that link. Yes, I want you to see the response from Last.fm, not the parameters in that link. I swear! [finger crossed behind my back]
1.2 For Conky
The following is my own Conky code:
${if_running mpd}${color lightblue}MPD: ${color #408040}$mpd_status$color$alignr ${if_running mpdscribble}s$endif${if_match "$mpd_repeat" == "On"}r$endif${if_match "$mpd_random" == "On"}z$endif $mpd_vol%
${alignc}${color red}$mpd_title$color
${alignc}${color #9999ff}$mpd_album$color
${alignc}${color green}$mpd_artist$color
${alignc}${color yellow}Played <b>${execi 6 lf-playcount-image.sh}</b><b>${execi 6 cut -f 2 -d \ "/tmp/lf-playcount-image"}</b> times$color
[$mpd_elapsed/$mpd_length] ${color #ccddff}${mpd_bar 3,0}$color<b>${execpi 6 echo "\${image /tmp/lf-images/large.png -p 125,600 -s 200x200 -n -f 6}"}</b>${endif}
It looks like:
The first $execi is to let the script to update. It will get song info from MPD directly and use it to query on Last.fm. A cache file will be stored at /tmp/lf-playcount-image, it only gets updated when different song is played. In other words, if you play only one song repeatedly, the playcount will not be updated, this is a caveat.
The cache file will read:
a687c5e49a9740c93ae30eb38c8bc729 36 http://userserve-ak.last.fm/serve/64s/50562771.png http://userserve-ak.last.fm/serve/126/50562771.png http://userserve-ak.last.fm/serve/174s/50562771.png http://userserve-ak.last.fm/serve/300x300/50562771.png
First one is a hash for identify if it’s the same song. Second is the playcount, then small image URL, medium image URL, large image URL, and extra large image URL. They are space-separated values.
Once this script retrieves these information from Last.fm, it will also try to download those images to /tmp/lf-images/. That directory might look like:
% ll /tmp/lf-images/
total 700
-rw-r--r-- 1 livibetter livibetter 2374 May 20 2008 http:--cdn.last.fm-flatness-catalogue-album-jewelcase_large.png
-rw-r--r-- 1 livibetter livibetter 2081 May 20 2008 http:--cdn.last.fm-flatness-catalogue-album-jewelcase_medium.png
-rw-r--r-- 1 livibetter livibetter 1444 Jun 18 2009 http:--cdn.last.fm-flatness-catalogue-album-jewelcase_small.png
-rw-r--r-- 1 livibetter livibetter 33671 Aug 29 09:14 http:--userserve-ak.last.fm-serve-126-50562771.png
-rw-r--r-- 1 livibetter livibetter 162117 Aug 28 11:38 http:--userserve-ak.last.fm-serve-174s-50562771.png
-rw-r--r-- 1 livibetter livibetter 471950 Aug 29 09:14 http:--userserve-ak.last.fm-serve-300x300-50562771.png
-rw-r--r-- 1 livibetter livibetter 9718 Aug 21 23:53 http:--userserve-ak.last.fm-serve-64s-50562771.png
lrwxrwxrwx 1 livibetter livibetter 78 Aug 29 09:19 large.png -> /tmp/lf-images/http:--cdn.last.fm-flatness-catalogue-album-jewelcase_large.png
lrwxrwxrwx 1 livibetter livibetter 79 Aug 29 09:19 medium.png -> /tmp/lf-images/http:--cdn.last.fm-flatness-catalogue-album-jewelcase_medium.png
lrwxrwxrwx 1 livibetter livibetter 78 Aug 29 09:19 small.png -> /tmp/lf-images/http:--cdn.last.fm-flatness-catalogue-album-jewelcase_small.png
As you can see, it creates symbolic links, so you can always get the current song’s cover art at /tmp/lf-images/{small,large,medium,extralarge}.png. In the example above, there is no extra large image. The other files are cached, so this script won’t download multiple times for same image.
Second $execi is to get the playcount, it has nothing special if you know shell script.
The $execpi is to create a Conky $image variable, still nothing too special.
2 Notes
I haven’t tested this script seriously, you may see some error which I clearly haven’t met. If so and you want it to get fixed, please provide your Last.fm username, song’s title and album. These information is enough for me to reproduce the issue.
And if you are interested in the song from the screenshot above, its page on Last.fm is here, I also wrote about it few days ago.
3 Updated: Showing Love status
If you also want to show the love status like:
Be sure to read this follow-up post. <3
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.