Script to scrobble plays from Last Played date

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey for Windows 4.

Moderators: Gurus, Addon Administrators

mcow
Posts: 834
Joined: Sun Sep 21, 2008 9:35 pm
Location: Cupertino, California

Script to scrobble plays from Last Played date

Post by mcow »

I found a Python library for last.fm which has scrobble support: PyScrobble. I wrote a Python script that steps thru current selection in MediaMonkey and scrobbles each track, using the Last Played date.

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[1]
    password = None if len(sys.argv) < 3 else sys.argv[2]
    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)
mcow
Posts: 834
Joined: Sun Sep 21, 2008 9:35 pm
Location: Cupertino, California

Re: Script to scrobble plays from Last Played date

Post by mcow »

After using my script for a couple weeks, I've finally determined that the cutoff time is six hours before scrobbling; not related to UTC at all.

I think if I switched to using the Scrobble 2.0 protocol, which requires either hacking the library or finding a different one, and I got an API key, I could scrobble plays from any arbitrary period, limited only by my sign-in date to last.fm. But I'm not sure when I'll get the time to work on that.
abatistas1709
Posts: 5
Joined: Sun Feb 12, 2012 9:48 am

Re: Script to scrobble plays from Last Played date

Post by abatistas1709 »

It would be great if this script could be upgraded in order to run inside MediaMonkey (without the need to run an external program). To do this, it should be written in VBScript, right? Is it possible? I'd love to help doing this, but my knowledge is limited.
mcow
Posts: 834
Joined: Sun Sep 21, 2008 9:35 pm
Location: Cupertino, California

Re: Script to scrobble plays from Last Played date

Post by mcow »

What would be great would be if the MM scrobbler plugin were accessible by script. My script is kinda junky and doesn't work that well. MM has already solved the issue of scrobbling effectively, including managing your last.fm login credentials; if they'd just expose a hook so we could use it, then you could pick and choose what you want scrobbled.
Post Reply