lfmCMD.pl - last.fm Command Line Utility (Windows, Linux, OS X)

Overview
lfmCMD - Command Line
lfmCOL - Data Collector
Generic Playlist Transfer
Essential Profile Transfer
Your last.fm Neighbours
Do-It-Yourself Scrobbler
MusicBrainz Scrobbler
Webservice - Word Cloud
iTunes Radio Scrobbler
Site Notice

lfmCMD.pl is a small but powerful Perl program. It is a generic last.fm webservices client for Windows, Linux, OS X and allows you to call every last.fm API service (for desktop applications) from a command line / command window (terminal, console).

Usage:

perl lfmCMD.pl method="name" [param1="value"] ... [paramN="value"]

Examples (read / get services):

perl lfmCMD.pl method="artist.getTopAlbums" artist="Cher"
perl lfmCMD.pl method="tag.getTopArtists" tag="disco"
perl lfmCMD.pl method="playlist.fetch" playlistURL="lastfm://playlist/album/2026126"

Example (write / post service; replace "sk", ... with your data):

perl lfmCMD.pl method="playlist.create" title="Best of Cher" description="Nutrimentum spiritus." sk="3a50...030b"

Link to utility: lfmCMD-20090817.zip

General informations:
- You need a Perl interpreter to run the Perl script (lfmCMD.pl).
- Linux / OS X: Perl is already available
- Windows: You have to download Perl (Link to free ActivePerl: http://www.activestate.com/activeperl/)
- You typically use the utility from a command window (prompt).
- Windows: Start, Programs, Accessories, Command prompt.
- Linux / OS X: Start a terminal (console).
- Recommended terminal size setting is 132 x 43.
- Help informations: Start the utility without any parameters.
- The last.fm http request is logged to file "last.fm.Request.txt".
- The last.fm xml response is logged to file "last.fm.Response.xml".

Technical notes:
- The utility comes with a configuration file (lfmCMD.cfg).
- You typically configure your "terminal encoding" and "proxy setting".
- lfmCMD.pl has full unicode (utf8) support.
- You have to configure the terminal encoding according to your codepage setting.
- Windows: eg. "cp850" / use "mode con" to find out your codepage setting.
- Linux / OS X: eg. "utf8" / have a look at your terminal configuration.
- You need a session key for write access into your last.fm profile.
- The session key (similar to a password) will then be infinite valid.
- Keep the session key save and private - you need it again and again.
- Proxy support and configuration is possible.
- HTTP timeout configuration ist possible.

How to get a session key (sk) ?

Notes:
- The process to gain a sk is only required once !
- A sk is only necessary for API write services !

1. Call method "auth.getToken":
- perl  lfmCMD.pl  method="auth.getToken"
- You will get a token (60 minutes valid) in the last.fm xml response (type last.fm.Response.xml).

2. Authorize lfmCMD.pl to access your last.fm account:
- Open your standard browser and login to your last.fm account.
- Build an URL like this:
  - http://www.last.fm/api/auth/?api_key=APIKEY&token=TOKEN
  - api_key: '0996e89f272c714ec0bd463ea17faf6c'
  - token: see step 1
  - e.g. http://www.last.fm/api/auth/?api_key=0996e89f272c714ec0bd463ea17faf6c&token=519...5c7
- Open the URL in your browser (you will see the last.fm application permission page).
- Grant permission to lfmCMD.pl / wait some seconds for last.fm server processing.

3. Call method "auth.getSession":
- e.g. perl  lfmCMD.pl  method="auth.getSession"  token="519...5c7"
  - token: see step 1
- You will get a session key (sk) (infinite valid) in the last.fm xml response (type last.fm.Response.xml).

4. Session key:
- Keep the session key save (similar to a password).
- Everybody who knows your session key has write access to your account.
- You can invalidate the session key by removing this application.
- Removing is done on the settings page of your last.fm profile.

What can I do with lfmCMD.pl ?

⇒ lfmCMD.pl saves the last.fm API service response (without any modifications) into a XML file. If you need another format you can easily convert it to your destination format. Or -if your application supports XML import (e.g. Excel)- you can directly import the response file.

⇒ You can also use lfmCMD.pl in conjunction with batch files (very powerful). This allows you to fully automate processes.
Example - Fetch all tracks in an user library:

perl lfmCMD.pl method="library.getTracks" page="1" user="toc-rox"
copy last.fm.Request.txt page1.library.getTracks.txt
copy last.fm.Response.xml page1.library.getTracks.xml
rem your data conversion or import here
...
perl lfmCMD.pl method="library.getTracks" page="8" user="toc-rox"
copy last.fm.Request.txt page8.library.getTracks.txt
copy last.fm.Response.xml page8.library.getTracks.xml
rem your data conversion or import here
...

⇒ You can grab your weekly chart data (e.g. track chart).

Step 1: Fetch the "from ... to" data. Get a list of available charts for a user, expressed as date ranges which can be sent to the chart services.

perl lfmCMD.pl method="user.getWeeklyChartList" user="rabourn"

Response:
weeklychartlist user="rabourn"
  chart from="1125835201" to="1126440001"
  chart from="1126440001" to="1127044801"
  ...
  chart from="1242561600" to="1243166400"
  chart from="1243166400" to="1243771200"
weeklychartlist

Epoch date range: The Unix epoch is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds. Examples (helpful link: http://www.epochconverter.com/):

from = 1242561600 = GMT = Sun May 17 12:00:00 2009
to   = 1243166400 = GMT = Sun May 24 12:00:00 2009

Step 2: Fetch the chart data. Get a track chart for a user profile, for a given date range. If no date range is supplied, it will return the most recent track chart for this user.

perl lfmCMD.pl method="user.getWeeklyTrackChart" from="1242561600" to="1243166400" user="rabourn"
copy last.fm.Response.xml lastfm-WeeklyTrackChart-20090517-20090524.xml

Response:
The users chart data in XML format (in file "last.fm.Response.xml").
You can import the data for instance direct into Excel or you can convert the data to another format.

⇒ Shout messages to one or more friends.

If you want to shout a message to a friend or a group of friends you can easily do it with lfmCMD.pl and a simple batch file (e.g. on Windows lfmSM2F.bat).

Usage (Windows): lfmSM2F "Message"

Example (Windows): lfmSM2F "Hello, Hello, I'm Back Again (Again!)"

@ECHO OFF
REM lfmSM2F.bat, last.fm Shout Message 2 Friends
REM Version 0.1-29.05.2009

IF [%1] == [] GOTO :EXIT
IF NOT [%2] == [] GOTO :EXIT

ECHO ON
perl lfmCMD.pl method="user.shout" user="Garry" sk="your session key" message=%1
perl lfmCMD.pl method="user.shout" user="Glitter" sk="your session key" message=%1
@ECHO OFF
GOTO :END

:EXIT
ECHO.
ECHO Usage : %0 "Message"
ECHO Example : %0 "Hello, Hello, I'm Back Again (Again!)"
ECHO.

:END

Remarks:
- Copy and paste the lines above into a file "lfmSM2F.bat".
- Add a line "lfmCMD.pl method=..." for each friend.
- Set the parameters "user" and "sk" to your data.

⇒ Add tags to a track.

perl lfmCMD.pl method=track.addTags artist="Supertramp" track="Dreamer" tags="progressive rock, supertramp, 80s" sk="4ec...b0d"

Remark:
- A nonexistent track will be added automatically to your music lib.