All Unkept
Posted in: Linux, Music, Personal and misc  —  26 June 2008

MOC and last.fm

Update: people are having trouble with this now, I am unable to debug what is wrong, it seems like a problem with mocp.


When not using Amarok (e.g. if I'm low on memory due to using VMs or something), moc is very nice music player (Debian/Ubuntu package 'moc', but command 'mocp'). Here is how to get it to submit tracks to last.fm in a well behaved manner (that doesn't submit tracks if you are just skipping through a playlist):

  • Install lastfmsubmitd: sudo aptitude install lastfmsubmitd
  • Add yourself to lastfm group: sudo adduser [yourusername] lastfm
  • Download and save my MOC - last.fm script, and make it executable.
  • Alter your ~/.moc/config to set OnSongChange as per the instructions at the top of the script.
  • Completely quit moc and restart it.

The advantage of the script I wrote over just using OnSongChange is that my script will wait for half the length of the song before submitting to last.fm. Every 5 seconds it will check you are still listening to that song, and if not, it will quit. This way it will behave much more like Amarok and other well behaved last.fm clients.

Comments §

§ On 14 July 2008, Leonardo Bernardes wrote: 326
I didn't realize I could find something like that. Amazing script, thanks!

I just started using MOC today

§ On 22 August 2008, Skraave wrote: 351
I don't know if I'm thinking wrong, but why do you check every five seconds for the same song? Wouldn't it make sense to wait <length of song>/2 and then perform one single check?

§ On 5 September 2008, luke wrote: 353
Skraave: I wrote it that way so that if you do lots of skipping through tracks, you don't end up with hundreds of python processes -- each one will die after 5 seconds, rather than after the <length of song>/2

§ On 12 January 2009, Danny Tatom wrote: 396
I've done everything mentioned, but it won't submit to lastfm. Has this been broken since this post was made or what? :/

§ On 25 January 2009, kad1r wrote: 399
I got an error.
FATAL_ERROR: Error in config file, line 17.

§ On 17 February 2009, mythmystic wrote: 406
awesome tutorial! It works on my MOC ... i made a link to ur post from my blog i hope u dun't mind! Thx a lot...

§ On 18 August 2009, Anonymous Coward wrote: 472
2kad1r
Just update MOC

§ On 29 August 2009, surlyrider wrote: 474
Hi there. Thanks for this. I installed lastfmsubmit and added myself to the lastfm group. I put your script here: /home/surlyrider/.moc and made it executable (chmod a+x moc_submit_lastfm). You said I should edit my ~/.moc/config file but there was no 'config/ file in my ~/.moc directory, so I created one, which contains this line only: OnSongChange = "~/.moc/moc_submit_lastfm --artist %a --title %t --length %d --album %r" Anyway, it doesn't seem to be working for me. Can you think where I've gone wrong? Cheers and thanks again.

§ On 16 September 2009, avsej wrote: 483
Hi, in my moc it doesn't work too. I think that OnSongChange not fired by moc, or fired but sript missing environment and failed to start, because without this event (if i start script with proper parameters when moc is playing) it is working

§ On 8 October 2009, Vitality wrote: 494
Amazing! Thank you very much!
Ubuntu 9.04.

§ On 3 November 2009, kovtunos wrote: 502
what version of python must be installed?
now I have 2.5.4 and this script doesn't work at all :(

§ On 27 November 2009, vig wrote: 817
not working in debian squeeze

§ On 11 December 2009, Juanma wrote: 821
I follow all the steps but something goes wrong. If I alter my moc config file adding the OnSongChange line I can not run mocp. I don't know what to do with the script; I don't know where to place it and if it is necessary to be execute it everytime I run moc and want it to submit to Lastfm. Help me, please! Thank you!

§ On 10 February 2010, Zir wrote: 850
I have Ubuntu 9.10 and it's not working here...

I did exactly everything you've said and it's not working. The bottom of my config file looks like this:
OnSongChange = "~/.moc/moc_submit_lastfm --artist %a --title %t --length %d --album %r"

And it does nothing.

§ On 10 February 2010, luke wrote: 851
I don't know why it is not working. My tests show that with moc 2.5.0-alpha3 on Ubuntu Karmic, OnSongChange doesn't work at all.

§ On 1 March 2010, Håkan wrote: 862
Hi, great script! I've made a minor change so it wont scrobble paused tracks, I tend to pause quite often when at work.


--- moc_submit_lastfm.orig 2008-06-27 00:40:38.000000000 +0200
+++ moc_submit_lastfm 2010-02-19 09:41:23.000000000 +0100
@@ -33,7 +33,7 @@
     p = Popen(["mocp", "-i"], stdout=PIPE)
     out, err = p.communicate()
     lines = out.split("\n")
- for s in ["Artist: %s" % artist, "Album: %s" % album, "SongTitle: %s" % title]:
+ for s in ["State: PLAY", "Artist: %s" % artist, "Album: %s" % album, "SongTitle: %s" % title]:
         if not s in lines:
             return False
     return True

§ On 14 March 2010, Thomas wrote: 864
Great script, thanx alot :)

§ On 29 March 2010, harman wrote: 865
Does not seem to be working for me. Ubuntu 9.04

§ On 4 April 2010, eleventhcrane wrote: 866
I was having a bunch of trouble trying to get lastfmsubmitd to work. I did a bunch of fiddling with the permission for all of the files/directories involved. (I googled lastfmsubmitd and downloaded the latest package, which included some documentation.)

The last thing I did before it started working was to switch the order of 2 lines in "/etc/lastfmsubmitd.conf".

In the INSTALL doc, it says the file should look like:
[account]
username = WaltKelly
password = amondayfriday13

Mine looked like:
[account]
password = amondayfriday13
username = WaltKelly

I don't know if switching it back to how INSTALL describes it fixed my problem or if it was that I gave myself ownership privileges to the files INSTALL lists. In any event, last.fm now reflects my recent moc plays.

§ On 15 April 2010, Brendan S wrote: 867
I didn't have that config file, but did just gzip -d ~/.moc/config.gz , which created an example config there. I read there is also one at /usr/share/doc/moc/examples/ . If it still isn't working for whoever, you may want to check that MOC is using the right config file, either by specifying it with -C /path/to/configfile or -M /path/to/.moc (as mentioned here, ~/.moc )

After specifying the right config file, I saw a few other places things could go weird, not sure if they'll fix anything, but;

1. Like eleventhcrane said, check the order in /etc/lastfmsubmitd.conf
2. Make sure MOC is using the right MOCDir/config file
3. Make sure that OnSongChange doesn't have the default "/path/to/file" thing!

Not sure if it's working for me yet...

§ On 23 April 2010, webfreak wrote: 868
You need to add FULL path to the script in moc config file. So if the path is ~/.moc/scripts/lastfm, you need to change it to /home/your_user/.moc/scripts/lastfm
In most cases mentioned above that should be enaught to work.

§ On 9 May 2010, nikolas wrote: 875
ugh, i had lastfmnotifd working, then i tried to follow these instructions and now i have neither this script nor lastfmnotifd working. it's terribly aggravating. if anyone feels like helping me troubleshoot, i've posted with my email attached to my name. it would be greatly appreciated.

nsanow at gmail dot com

§ On 21 June 2010, Fancycakes wrote: 897
I can't get this to work! I've installed lastfmsubmitd, added myself to the lastfm group, downloaded and saved the MOC script, made it executable, altered the ~/.moc/config file with absolute paths, restarted MOC....

I'm at a loss.

I changed permission in /var/spool/lastfm to 777 because none of the songs' info files appeared there otherwise. The current ownership of /var/spool/lastfm is lastfm:root. Shouldn't it be root:lastfm or lastfm:lastfm ?

I'm certain that my /etc/lastfmsubmitd.conf file is edited correctly.

§ On 21 June 2010, Fancycakes wrote: 898
Sorry guys, disregard my last post. Of all things to forget to do, I forgot to do 'sudo /etc/init.d/lastsubmitd restart'

Works perfectly now!

§ On 3 September 2010, neohomerico wrote: 931
Can't get this to work, Ubuntu 10.04.

Fancycakes, would you tell step by step what you did?

§ On 21 December 2010, david wrote: 970
it works fine ubuntu lucid after a second afternoon at getting it to work :) maybe quitting the moc server and restarting...?

§ On 23 March 2011, Jojan wrote: 1004
Nice script. I noticed, however, that you can change line 58-67 with

→ wait = length/2
→ time.sleep(wait)
→ if still_playing(options.artist, options.album, options.title):
→ → submit_to_lastfm(options.artist, options.album, options.title, options.length)
→ → exit(0)
→ exit(1)

where → marks the indentation. The only benifit of the original, as I see it, is that it will make the script exit sooner if you change song before waiting the wait number of seconds. In this you will only have to make on comparison during the song, and not 2·length/5 (two ifs, but only every fifth second) which is 72 for 3 minute.

The problem with this change would be if you step through alot of songs you will have alot of sleeping scripts. But hopefully time.sleep is well made, and does not hurt too much.

§ On 26 March 2011, luke wrote: 1005
@Jojan. I already explained my reason for not doing it that way, in my first comment above.

BTW, my blog software copes with indentation without hacks:

class Foo:
   def bar(self):
       pass

And if you turn on Restructured text you can use a proper 'code-block'

§ On 14 April 2011, Jojan wrote: 1023
@Luke. Ah, I did not see that, but it was as I pointed out. I have now made a little fix for that in my own code. I add up the sleep time in a separate variable, and when that is above 600 seconds (10 minutes) then I see if the song is still playing. If not, then exit.

if length < 15:
    submit_to_lastfm(options.artist, options.album, options.title, options.length)
    exit(0)

now = int(Popen(["mocp", "-Q", "%cs"], stdout=PIPE).communicate()[0].split("\n")[0])
wait = length/4

c_wait = 0
limit = 600 # 600 seconds == 10 minutes
while now < wait:
    c_wait += (wait-now)
    time.sleep(wait-now)
    # Check if it is still playing every limit seconds
    if c_wait > limit:
        if not still_playing(options.artist, options.album, options.title):
            exit(1)
        c_wait -= limit
    now = int(Popen(["mocp", "-Q", "%cs"], stdout=PIPE).communicate()[0].split("\n")[0])

if still_playing(options.artist, options.album, options.title):
    submit_to_lastfm(options.artist, options.album, options.title, options.length)
    exit(0)

exit(1)

§ On 19 May 2011, Seethi wrote: 1057
There's no config file in .moc directory.
If i make one i get error in line 17.

§ On 14 July 2011, ffuentes wrote: 1232
I don't know why but it gives me an error:

lastfmsubmitd: no account info found; exiting


WTF?! (I've made /etc/lastfmsubmitd.conf edition of course)

§ On 16 July 2011, luke wrote: 1233

I'm afraid I can't offer any more support for this script, or use of lastfmsubmitd, so I'm closing comments on this blog post. If you have solutions rather than questions feel free to e-mail me.



Closed for comments.