Skip to content

Instantly share code, notes, and snippets.

@doitian
Created August 23, 2009 04:08
Show Gist options
  • Save doitian/173151 to your computer and use it in GitHub Desktop.
Save doitian/173151 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# Author: Ian Yang <doit dot ian (at) gmail dot com>
# URL: http://gist.github.com/gists/173151
#
# This script depends on script `notify-send.py', which can be
# downloaded from http://gist.github.com/172910. Script
# `notify-send.py' requires python library `pynotify'.
#
# This script sends mpc status to `notify-osd'. I have not tested in
# other notification daemon.
#
# The icon is searched in the same directory with the song. It's
# recommended to name them as cover.(jpg|png) or
# folder.(jpg|png). Otherwise cover is selected randomly from all
# image files.
#
# If the file does not contain mp3 tag info, it is assumed that your
# file is arranged as
#
# %artist%/%album%/%title%.mp3
#
# in the music directory.
#
# The music directory is fetched from environment variable
# `MPD_MUSIC_DIR' or searched from the option music_directory in
# `/etc/mpd.conf'.
#
use strict;
use File::Basename qw /dirname/;
use List::Util qw/shuffle/;
# change it to 0 if you are using `notify-send', then replacing bubble
# is not support.
my $have_notify_send_py = 0;
# default icon used when cannot find cover in the song directory.
my $default_icon = "sonata";
# file storing notification id
my $id_file = "~/.mpc_notification_id";
my $debug;
my @status = `mpc --format "%file%[\n%artist%///%album%///%track% %title%]" status 2> /dev/null`;
print @status if $debug;
my $status = "stop";
my ($file, $artist, $album, $title, $progress);
if (@status >= 3) {
chomp($file = shift @status);
print "file=$file\n" if $debug;
if (@status == 3) {
chomp(my $format = shift @status);
$format =~ s/^\s+//;
($artist, $album, $title) = split qr|///|, $format, 3;
} else {
my @file_components = split qr|/|, $file;
shift @file_components while @file_components > 3;
($artist, $album, $title) = @file_components;
$title =~ s/\.\w*$//;
}
print "artist=$artist\nalbum=$album\ntitle=$title\n" if $debug;
if ((shift @status) =~ qr|^.*\[(.*)\]\s*[0-9#/]*\s*([0-9:/]*.*\(.*%\)).*$|) {
$status = $1;
$progress = $2;
print "status=$1\nprogress=$2\n" if $debug;
}
}
my ($music_directory, $icon);
if ($file) {
if (-d $ENV{'MPD_MUSIC_DIR'}) {
$music_directory = $ENV{'MPD_MUSIC_DIR'}
} elsif (-r "/etc/mpd.conf") {
if (open MPDCONF, "<", "/etc/mpd.conf") {
for (<MPDCONF>) {
if (/^music_directory.*"(.*)\/?".*$/ && -d $1) {
$music_directory = $1;
last;
}
}
close MPDCONF;
}
}
}
print "music_directory=$music_directory\n" if $debug;
if (-d $music_directory) {
my $song_directory = dirname $music_directory . "/" . $file;
print "song_directory=$song_directory\n" if $debug;
my @files = <\Q$song_directory\E/*.{jpg,JPG,png,PNG}>;
my @covers = grep /(cover|folder).\w*$/i, @files;
@files = @covers if @covers;
$icon = $files[int(rand(@files))] if @files;
print "icon=$icon\n" if $debug;
}
my $id = 0;
if ($have_notify_send_py && -r $id_file && open ID_FILE, "<", $id_file) {
my @id_contents = <ID_FILE>;
$id = int($id_contents[0]) if @id_contents;
close ID_FILE;
}
my ($summary, $body);
if ($title) {
$summary = $title;
$body = "by <b>$artist</b> from <b>$album</b>";
if ($status && $progress) {
$body .= "<br />$progress<br />[$status]";
}
}
else {
$summary = "MPC is stop";
}
print "summary=$summary\nbody=$body\n" if $debug;
$icon = "sonata" if !$icon;
$icon = "-i \Q$icon";
if ($have_notify_send_py) {
my @out_new_id = `notify-send.py -r $id -n MPC $icon \Q$summary\E \Q$body\E`;
my $new_id = 0;
$new_id = int($out_new_id[0]) if @out_new_id;
print $new_id . "\n";
if (open ID_FILE, ">", $id_file) {
print ID_FILE $new_id;
close ID_FILE;
}
}
else {
system "notify-send $icon \Q$summary\E \Q$body\E";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment