Contrib

Post here your patches, tips, themes, scripts and so on.

Python module for MOC

Forums:

I hacked together a Python module which connects to MOC via the file socket and listens for events. At the moment, it just gathers status information about the currently playing song, like artist, album, title, track, etc. It seems stable to me, but since I had to extract the client-server-communication out of the C source and I don't like C, it's actually very unstable. Additionally, I'm new to Python and this is my first code that does anything productive.

You can download it from here: http://pypi.python.org/pypi/MOC
Documentation is in the README file, but if something is missing, feel free to ask me.

It comes with two examples I use everyday:
- lastfmoc.py submits song information to last.fm (via lastfmsubmitd).
- mocstat.py uses dzen2 to show "currently playing" information on the desktop.
This is how it looks for me: http://666kb.com/i/b240g3zy7me5h89i9.png

white gray black theme

Forums:

Better behaved submission to last.fm

Forums:

I wrote a script that uses OnSongChange to trigger submissions to last.fm (using lastfmsubmitd), but one that waits until the song is half played before submitting. This way skipping through tracks after a few seconds will not cause submissions to last.fm.

Script is here:

http://files.lukeplant.fastmail.fm/public/moc_submit_lastfm

Blog entry:

http://lukeplant.me.uk/blog.php?id=1107301687

It can go into contrib if wanted. IMO, it is nicer than current script in contrib that does this, since it waits for an appropriate length of time rather than submitting as soon as a change is noticed (and also: it doesn't require a background process to be run first, and doesn't depend on the 'lastfm' python module, which doesn't seem to be the same on my system).

Luke

Yet another Last.fm submit script

Forums:

I wanted to scrobble songs through the official Last.fm client instead of via lastfmsubmitd, so that I could get song/artist/album info in the client and have audio fingerprints submitted to Last.fm.

I tried to use fagga's mocstat script, but I found it easier to hack the relevant logic in the script instead of using the available hooks. Also, I had a few problems to make it work correctly, so I deleted some parts of the code and added some "utf8" declarations - now it only sends commands to the last.fm client, and it may not work right with non-Unicode locales. Of course all this can be added to the original script, but I only had time for a quick hack - sorry...

So, here it is:

http://members.hellug.gr/adia/m/moc-lastfm.pl

I use it through the following shell function:

m () { 
    if [ -z "`pgrep -x last.fm`" ]; then
        lastfm -tray &
    fi;
    if [ -z "`pgrep -f moc-lastfm`" ]; then
        ~/.bin/moc-lastfm.pl >> ~/.mocstat/moc-lastfm.log 2>&1 &
    fi;
    mocp "$@"
}

lyrics fetcher (perl script)

Forums:

If you want to see the lyrics for the song you're listening to, you might be interested in this script. It's a quick hack (I do only quick hacks :D), but it works at least for the most popular tracks.

It's a frontend for David Precious' Lyrics::Fetcher CPAN Module, that outputs the lyrics to a pager of your choice. To use it, you can map a function key to it, e.g. F1, select a track, hit it and hope that it's found:

ExecCommand1 = "/home/kb/bin/moc-lyrics --artist %r --title %t"

Here it is:

#!/usr/bin/perl
#===============================================================================
#
# FILE: moc-lyrics.pl
#
# USAGE: ./moc-lyrics.pl
#
# DESCRIPTION: Retrieves lyrics from some sites
#
# NOTES: Lyrics::Fetcher is free software by David Precious
# (URL: http://search.cpan.org/~bigpresh/)
# AUTHOR: kb,
# COMPANY: "freelance" :-)
# VERSION: ---
# CREATED: 12/02/2007 11:08:50 PM CET
# REVISION: $Id: moc-lyrics.pl 14 2008-04-04 17:46:38Z kb $
#===============================================================================

use strict;
use warnings;
use Lyrics::Fetcher;
use Encode;
use Getopt::Lazy
'artist|a=s' => 'Artist',
'title|t=s' => 'Song Title',
-summary => 'Looks up lyrics in LyricWiki',
-usage => '%c %o',
;
getopt();
exit usage unless ($artist && $title);

our $INDENT = 4; # Number of spaces for indentation (if any)
our $HEADING = 1; # Print a Heading?
our $SOURCE = 1; # Show Source?
our $PAGER = 'less'; # Pager to show lyrics in

our $lyrics;
our $source;
# Comment in/out what you want. order does matter.
# For more check out Lyrics::Fetcher's CPAN page
our @sources = (
'LyricWiki',
#'AZLyrics',
#'AstraWeb',
#'LyricsNet',
#'LyricsTime',
);

sub get_lyrics {
my $source = shift;
return Lyrics::Fetcher->fetch( $artist, $title, $source );
}

# Catch lyrics for every source, until one yields a result
foreach my $site (@sources) {
$lyrics = get_lyrics($source);
if ($lyrics) {
$source = $site;
last;
}
}

die "No lyrics found, sorry\n" unless $lyrics;

if ($HEADING) {
my $header = decode('utf8', "$artist - $title\n");
$header .= "=" x length($header) . "\n";
$lyrics = $header . $lyrics;
}

if ($SOURCE) {
$lyrics .= "\n\n[ retrieved from $source ]\n";
}

if ($INDENT) {
$lyrics = indent( $lyrics );
sub indent {
my $lyrics = shift;
my $prepend = ' ' x $INDENT;
my $temp;
foreach( split( "\n", $lyrics ) ) {
s/^/$prepend/;
$temp .= $_ . "\n";
}
return $temp;
}
}

open LESS, "|$PAGER";
print LESS $lyrics;
close LESS;

exit 1;

yet another script for song change event

Forums:

Having testet the script for long time, I've added some lines to make configuration a little bit easier and here it is. The script should work with any version. It does not poll for song changes, but is cacluating the end of the current played song.

Following things are already in the script: submitting to lastfm, status changes to gaim, setting xroot with cover, updating ~/.np:

------------------------------------------------------------------------------------------------------------------
#!/bin/bash -x

lasttitle=""
lastalbum=""

# Command to display cover
displayCover=1
updateNowPlaying=0
updateGaim=1
updateLastFm=1
mocIsOnFingerPort=1

LOGFILE=~/.np

if (( $displayCover )); then
XSETROOT="gconftool-2 -t str -s /desktop/gnome/background/picture_filename"
fi

# finger stream tcp nowait.1000 mocp /home/mocp/mocp_as_finger.sh mocp_as_finger.sh

# This is the mocp_as_finger.sh:
# #!/bin/sh
# export HOME=/home/mocp
# /usr/bin/mocp -i

while [[ 1 ]]; do

if (( $updateNowPlaying )); then
lastline=`tail -1 $LOGFILE`
fi

SongTitle=""
Artist=""
Album=""
actualtime=""
currenttime=""
starttime=""
timeleft=""
State=""

if (( $mocIsOnFingerPort )); then
eval $(finger @localhost|perl -n -e 'print if s/(^.*?): (.*$)/$1="$2"/;'|perl -p -e "s/\`\'\"/ /;")
else
eval $(mocp -i|perl -p -e 's/(^.*?): (.*$)/$1="$2"/;'|perl -p -e "s/\`\'\"/ /;")
fi

if [[ "$State" != "PLAY" ]]; then
if [[ "$idleset" == "false" ]]; then
gaim-remote "setstatus?status=available&message="
idleset=true
fi
sleep 30;
continue;
fi

idleset=false

newline="$SongTitle - $Artist - $Album"

if [[ "$SongTitle" = "" ]]; then
newline="$Title"
fi

if [[ "$newline" = "" ]]; then
newline="$File"
fi

# Nothing is played at the moment, so we'll try again later
if [[ "$newline" = "" ]]; then
sleep 5;
continue;
fi

let actualtime=`date +%s`%86400+`date +%:z|cut -d":" -f1`*3600

let currenttime=`echo $CurrentTime|cut -s -d":" -f1,2 --output-delimiter="*60+"`
tlMinutes=`echo $TimeLeft|cut -s -d":" -f1|sed -e 's/^0//'`
tlSeconds=`echo $TimeLeft|cut -s -d":" -f2|sed -e 's/^0//'`

let timeleft=tlMinutes*60+tlSeconds

let starttime=actualtime-currenttime

let hours=starttime/3600
let minutes=starttime%3600/60
let seconds=starttime%60

start=`printf "%02d:%02d:%02d" $hours $minutes $seconds`

let timeleft=timeleft+5 # Wait for pause is over

# Something wrong, but don't know exactly what
if (($timeleft<=0)); then
timeleft=20
fi

if [[ $updateNowPlaying && "$lastline" != "$newline" ]];then
# xrootconsole can't handle utf-8, so we convert
newline_latin=`echo $newline |recode utf8..latin1`
echo $newline_latin >>$LOGFILE
fi

newtitle="$SongTitle - $Artist"
if [[ "$lasttitle" != "$newtitle" ]]; then

if (( $updateLastFm )); then
/usr/lib/lastfmsubmitd/lastfmsubmit --artist "$Artist" \
--album "$Album" --title "$SongTitle" --length $TotalSec
fi

if (( $updateGaim )); then
message=`echo $newtitle|sed -e "s/&/And/"`
status=`gaim-remote getstatus`
gaim-remote "setstatus?status=$status&message=♫ $message"
fi

lasttitle=$newtitle

if (( $displayCover )); then
if [[ "$lastalbum" != "$Album" ]]; then
olddir=`pwd`
newdir=`dirname "$File"`
cd "$newdir"
cover=`ls *.jpg|head -n1`
if [[ "$cover" != "" ]]; then
$XSETROOT "`pwd`/$cover"
fi
cd "$olddir"
lastalbum=$Album
fi
fi
fi

sleep $timeleft

done

# An idea for using ~/.np for desktops where xrootconsole is still working:
# put into: ~/.xsession, or similar:
# xrootconsole -geometry 132x1+135-35 -fn -adobe-courier-bold-r-normal-*-*-140-75-75-m-*-iso8859-15 ~/.np &

new irssi mocp script

Forums:

Made another, a bit more advanced mocp script for irssi than the one @ the contrib-page.

iMOCP is a script that make Irssi able to talk with MOCP. Put the script in ~/.irssi/scripts or
~/.irssi/scripts/autorun to make it autoload, when you start Irssi. If you dosen't use the autorun folder
then you have to load iMOCP with this command:

/script load imocp.pl

iMOCP commands:
/mocp play - Play track

/mocp pause - Pause track
/mocp p - Toggle between play and pause

/mocp next - Play next track
/mocp n

/mocp prev - Play previous track
/mocp b

/mocp info - Display track information i status window
/mocp i

/mocp npf - Print now playing filename to channel.
/mocp np - Print current track title to chat window. This only supports id3-tag yet.

http://illvilja.org/imocp.pl

another script for moc - status and song change

Forums:

Hello there,

I wrote a little perl script called mocstat that implements some kind of song change and general status information functionality. It runs in the background and invokes continuously (every 500ms) "mocp --info". The gathered status information will be submitted to two scripts via environment variables.

  • ~/.mocstat/update is called every time "mocp --info" is called.
  • ~/.mocstat/songchange is called when the current song has been playing for 30 seconds.

[br]

Here is an example for ~/.mocstat/update:

#!/bin/sh
echo -e "$TITLE" > ~/.conky/mocstat.title
echo -e "$CURRENTTIME" > ~/.conky/mocstat.currenttime
echo -e "$TOTALTIME" > ~/.conky/mocstat.totaltime
echo -e "$PERCENTDONE" > ~/.conky/mocstat.percentdone

This puts the status information in some FIFOs which I can read in my conkyrc via ${exec cat ~/.conky/mocstat.title} or ${execbar cat ~/.conky/mocstat.percentdone} or something like this.
I use FIFOs because normal files are sometimes empty when conky reads them which makes the display flickering. By using a FIFO, conky hangs until there is something to read. (Create a FIFO with mknod or mkfifo.)

[br]

Here is an example for ~/.mocstat/songchange to scrobble your music to last.fm via lastfmsubmitd:


#!/bin/sh
if [ -n "$ARTIST" ] && [ -n "$SONGTITLE" ] && [ -n "$TOTALTIME" ]; then
/usr/lib/lastfmsubmitd/lastfmsubmit --encoding UTF-8 --artist "$ARTIST" --title "$SONGTITLE" --length "$TOTALTIME" --album "$ALBUM"
fi

If the necessary information for scrobbling (artist, title and length) is available, submit it to lastfmsubmitd which will make sure it gets to last.fm.

[br]

When you run this script for the first time, it creates ~/.mocstat/ automatically. You can then create and edit ~/.mocstat/update and/or ~/.mocstat/songchange. And don't forget to make them executable.

I put mocstat & in my ~/.xsession, so it gets started when I log in and works silently in the background.

And finally the download link: http://www.sendspace.com/file/hypep2

Feel free to send any constructive feedback to fagga@gmx.de.

Pages

Subscribe to RSS - Contrib