I have to say: this is not a reliable script; last.fm seems to reject some plays. I have not been able to scrobble a song played the day before (or earlier), and even plays from the early morning sometimes don't get scrobbled. I haven't figured out the problem; I think the library may be accessing an older last.fm service, which has been limited. (The cog_scrobbler program can make older scrobbles like these, but that requires manually searching for each album and sometimes having to type in the song durations.)
The rejections might hinge on a difference in the date in UTC (in London, that is) at the time of play (8:30AM PDT) and the attempted scrobble time (6PM).
When I come home after running the iPod for the morning and evening commutes, I first sync to MediaMonkey, then I run the script. I have so far always been able to scrobble the evening plays, but the morning ones have failed more than twice. The missed scrobbles are annoying, but when it works, it's a great way to synch last.fm with iPod plays.
I have a shortcut on my desktop to launch this script, with my username & password on the command line; it just opens a dos window that shows a (very ugly) display of the scrobbling attempts, with "OK" if it succeeds and the date of the last play if the library call fails for some reason.
Debugging hints welcome!
Here's the script. This is not great Python, I know; it is, at least, short:
Code: Select all
import sys, os import datetime import codecs import win32com.client import pythoncom # imports from PyScrobble http://code.google.com/p/pyscrobble/ from scrobble.submit import scrobbleTrack, status from scrobble.artist import Artist from scrobble.track import Track from scrobble.user import User def createTrack(mmTrack): return Track(Artist(codecs.encode(mmTrack.ArtistName, 'utf_8')), codecs.encode(mmTrack.Title, 'utf_8'), \ albumname=codecs.encode(mmTrack.AlbumName, 'utf_8'), length=mmTrack.SongLength/1000) def local_submit(track, user): timestamp = track.LastPlayed if timestamp.year > 2000: # "valid time" test pstrk = createTrack(track) if pstrk.albumname is None: raise Exception("bad creation: no album name") result = scrobbleTrack(user, pstrk, datetime.datetime.fromtimestamp(int(timestamp))) if result is False: print status print timestamp elif result is None: print timestamp pass else: print "not scrobbled" def standard_op(user=None, password=None): if user is None: raise Exception("no user specified!") PSuser = User(user, password) SDB = win32com.client.Dispatch('SongsDB.SDBApplication') # Examine MediaMonkey's current track selection seltracks = [SDB.SelectedSongList.Item(i) for i in range(SDB.SelectedSongList.Count)] for trk in seltracks: print trk.Title local_submit(trk, PSuser) # scrobble it and print OK if successful if __name__ == '__main__': import time name = None if len(sys.argv) < 2 else sys.argv password = None if len(sys.argv) < 3 else sys.argv if name is not None: print "logging in", name, if password is not None: print "with password", print standard_op(name, password) time.sleep(4.20)