weerock.pl 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#   Copyright [2010] [Sebastian Köhler]
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#===============================================================================
#
#         FILE:  weerock.pl
#
#  DESCRIPTION:  WeeChat Pluging to show others youre good taste in music
#                
#                Supported Players:
#                   audacious
23
#                   banshee
24
#                   exaile
25 26 27
#                   moc
#                   mpc
#                   ncmpcpp
28 29
#                   pytone
#                   quod libet
30
#                   rhytmbox(requires Net::DBus)
31
#
32
# REQUIREMENTS:  weechat 0.3.0, one of the above players
33 34
#       AUTHOR:  Sebastian Köhler (sk), sebkoehler@whoami.org.uk
#      WEBSITE:  http://hg.whoami.org.uk/weerock
35
#      VERSION:  0.3
36 37 38 39 40 41 42 43
#      CREATED:  31.01.2010 04:17:41
#===============================================================================

use strict;

my $description = "Rock this chat!";
my $helptext = "Use this command to show your current song in the channel\n\n";

44 45
weechat::register('weerock','Sebastian Köhler','0.3','Apache 2.0',
                   $description,'','');
46

47 48
weechat::hook_command('audacious',$description,"","$helptext","","audacious",
                      "");
49

50 51 52 53 54 55 56
weechat::hook_command('banshee',$description,"","$helptext","","banshee","");

weechat::hook_command('exaile',$description,"","$helptext","","exaile","");

weechat::hook_command('moc',$description,"","$helptext","","moc","");

weechat::hook_command('mpc',$description,"",
57 58 59 60 61 62
                      "$helptext".
                      "SETTINGS\n".
                      "    /set plugins.var.perl.weerock.mpc_host PASSWORD\@IP\n".
                      "        Default: localhost\n".
                      "    /set plugins.var.perl.weerock.mpc_port PORT\n".
                      "        Default: 6600\n",
63
                      "","mpc","");
64

65
weechat::hook_command('ncmpcpp',$description,"",
66 67 68 69 70 71
                      "$helptext".
                      "SETTINGS".
                      "    /set plugins.var.perl.weerock.ncmpcpp_host PASSWORD\@IP\n".
                      "        Default: localhost\n".
                      "    /set plugins.var.perl.weerock.ncmpcpp_port PORT\n".
                      "        Default: 6600\n",
72
                      "","ncmpcpp","");
73

74
weechat::hook_command('pytone',$description,"","$helptext","","pytone","");
75

76
weechat::hook_command('quodlibet',$description,"","$helptext","","quodlibet",
77 78
                      "");

79
weechat::hook_command('rhythmbox',$description,"","$helptext","","rhythmbox",
80 81
                      "");

82 83
weechat::hook_command('weerock',$description,"","Show help for weerock","",
                      "weerock","");
84 85 86

return weechat::WEECHAT_RC_OK;

87 88 89 90 91 92 93 94 95
sub audacious {
    load_defaults();
    my $cmd = "audtool2 --current-song-tuple-data artist ".
                       "--current-song-tuple-data album ".
                       "--current-song-tuple-data title ".
                       "--current-song-output-length-seconds ".
                       "--current-song-length-seconds 2> /dev/null";
    my $exp = "(.*)\n(.*)\n(.*)\n(.*)\n(.*)";
    my ($artist,$album,$title,$ct,$tt) = ("","","","","");
96
    
97 98 99 100 101 102 103
    if(`pgrep audacious`) {
        ($artist,$album,$title,$ct,$tt) = `$cmd` =~ /$exp/;
    
        $ct = sec_to_min($ct); 
        $tt = sec_to_min($tt);
    }
    echo_to_channel(build_message($artist,$album,$title,$ct,$tt));
104 105
}

106 107 108 109 110 111 112 113 114 115 116 117 118
sub banshee {
    load_defaults();
    my $cmd = "banshee --query-artist --query-album --query-title ".
                      "--query-position --query-duration 2> /dev/null";
    my $exp = "artist: (.*)\nalbum:\ (.*)\ntitle: (.*)\nposition: ".
              "(.*),.*\nduration: (.*),.*";
    my ($artist,$album,$title,$ct,$tt) = ("","","","","");
    
    if(`pgrep banshee`) {
        ($artist,$album,$title,$ct,$tt) = `$cmd` =~ /$exp/;
    
        $ct = sec_to_min($ct);
        $tt = sec_to_min($tt);
119
    }
120
    echo_to_channel(build_message($artist,$album,$title,$ct,$tt));
121 122
}

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
sub exaile {
    load_defaults();
    my $cmd = "exaile --get-artist --get-album --get-title ".
              "--current-position --get-length 2>/dev/null";
    my $exp = "(.*)\n(.*)\n(.*)\n(.*)\n(.*)";
    my ($artist,$album,$title,$ct,$tt) = ("","","","","");

    if(`pgrep exaile`) {
        ($album,$artist,$title,$tt,$ct) = `$cmd` =~ /$exp/;
        
        $tt = sec_to_min(int($tt));
    }   
    echo_to_channel(build_message($artist,$album,$title,$ct,$tt));
}

sub moc {
    load_defaults();
    my $cmd = "mocp -i 2> /dev/null";
    my $exp = "Artist: (.*)\n.*: (.*)\n.*: (.*)\n.*: (.*)\n.*\n.*\n.*: (.*)";
142
    
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    my ($artist,$album,$title,$ct,$tt) = ("","","","","");
    if(`pgrep mocp`) {
        ($artist,$title,$album,$tt,$ct) = `cmd` =~ /$exp/;
    }
    echo_to_channel(build_message($artist,$album,$title,$ct,$tt));
}

sub mpc {
    load_defaults();
    my $host = weechat::config_get_plugin("mpc_host");
    my $port = weechat::config_get_plugin("mpc_port");
    my $cmd = "mpc status -h $host -p $port -f \"%artist% #| ".
              "%album% #| %title%\"";
    my $exp = '(.*) \| (.*) \| (.*)\n.*(\d+:\d{2})/(\d+:\d{2})';
    my ($artist,$album,$title,$ct,$tt) = ("","","","","");
158
    
159 160 161 162
    if(`pgrep mpd`) {
        ($artist,$album,$title,$ct,$tt) = `$cmd` =~ /$exp/;
    }
        echo_to_channel(build_message($artist,$album,$title,$ct,$tt));
163 164
}

165 166 167 168 169 170 171 172 173 174 175 176
sub ncmpcpp {
    load_defaults();
    my $host = weechat::config_get_plugin("ncmpcpp_host");
    my $port = weechat::config_get_plugin("ncmpcpp_port");
    my $cmd = "ncmpcpp -h $host -p $port --now-playing ".
              "'%a ^ %b ^ %t' 2> /dev/null";
    my $exp = '(.*) \^ (.*) \^ (.*)';
    
    my ($artist,$album,$title) = ("","","");

    if(`pgrep mpd`) {
        ($artist,$album,$title) = `$cmd` =~ /$exp/;
177
    }
178 179 180 181 182 183 184 185 186 187 188
    echo_to_channel(build_message($artist,$album,$title,"",""));
}

sub pytone {
    load_defaults();
    my $cmd = "pytonectl getplayerinfo 2> /dev/null";
    my $exp = '(.*) - (.*) \( (\d?\d:\d\d)\/ (\d?\d:\d\d)\)';
    my ($artist,$title,$ct,$tt) = ("","","","");

    if(`pgrep pytone`) {
        ($artist,$title,$ct,$tt) = `$cmd` =~ /$exp/;
189
    }
190
    echo_to_channel(build_message($1,"",$2,$3,$4));
191 192
}

193 194 195 196 197
sub quodlibet {
    load_defaults();
    my $cmd = "quodlibet --print-playing 2> /dev/null";
    my $exp = "(.*) \- (.*) \- .* \- (.*)";
    my ($artist,$album,$title) = ("","","");
198

199 200
    if(`pgrep quodlibet`) {
        ($artist,$album,$title) = `$cmd` =~ /$exp/;
201
    }
202
    echo_to_channel(build_message($artist,$album,$title,"",""));
203 204 205 206
}

sub rhythmbox {
    require Net::DBus;
207 208 209 210 211 212 213
    
    load_defaults();

    my ($artist,$album,$title,$ct,$tt) = ("","","","","");

    if(`pgrep rhythmbox`) {
        my $bus = Net::DBus->session;
214
        my $rboxservice = $bus->get_service("org.gnome.Rhythmbox");
215 216
        my $rboxplayer = $rboxservice->get_object("/org/gnome/Rhythmbox/Player");
        my $rboxshell = $rboxservice->get_object("/org/gnome/Rhythmbox/Shell");
217 218 219
        
        if($rboxplayer->getPlaying()) {
            my $song = $rboxshell->getSongProperties($rboxplayer->getPlayingUri());
220
            my $ct = sec_to_min($rboxplayer->getElapsed());
221 222 223 224 225 226 227 228 229 230
            if(exists $song->{'artist'}) {
                $artist = $song->{'artist'};
            }
            if(exists $song->{'title'}) {
                $title = $song->{'title'};
            }
            if(exists $song->{'album'}) {
                $album = $song->{'album'};
            }
            if(exists $song->{'duration'}) {
231
                $tt = sec_to_min($song->{'duration'});
232 233
            }
        }
234 235
    }
    echo_to_channel(build_message($artist,$album,$title,$ct,$tt));
236 237
}

238 239 240
sub weerock {
    my $bold   = weechat::color("bold");
    my $unbold = weechat::color("-bold");
241
    
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    my $help = "%bold%NAME%unbold%\n".
               "    weerock - $description\n".
               "%bold%COMMANDS%unbold%\n".
               "    /mpc\n".
               "    /moc\n".
               "    /ncmpcpp\n".
               "    /rhythmbox\n\n".
               "    For more information do:\n".
               "    /help command\n\n".
               "%bold%GLOBAL SETTINGS%unbold%\n".
               "    /set plugins.var.perl.weerock.format STRING\n".
               "        STRING will be send to the Channel\n".
               "        Following Variables are available:\n".
               "            %artist% - Artist\n".
               "            %album%  - Album\n".
               "            %title%  - Title\n".
               "            %ct%     - Current Time\n".
               "            %tt%     - Total Time\n".
               "        Default: \"%artist%(%album%) - %title% [%ct%/%tt%]\"\n".
               "    /set plugins.var.perl.weerock.left_string STRING\n".
               "        STRING is printed before the song information\n".
               "        Default: \"/me np\"\n".
               "    /set plugins.var.perl.weerock.right_string STRING\n".
               "        STRING is printed after the song information\n".
               "        Default: \"\"";
267
    
268 269 270
    $help =~ s/%bold%/$bold/g;
    $help =~ s/%unbold%/$unbold/g;
    echo_to_buffer($help);
271 272 273 274 275 276 277
}

#
##
### Basic functions
##
#
278
sub echo_to_channel {
279 280 281 282
    my ($string) = @_;
    my $buffer = weechat::current_buffer;
    my $left_string = weechat::config_get_plugin("left_string");
    my $right_string = weechat::config_get_plugin("right_string" eq "");
283
    weechat::command($buffer, "$left_string" . $string . "$right_string");
284 285
}

286
sub echo_to_buffer {
287 288 289 290 291
    my ($string) = @_;
    my $buffer = weechat::current_buffer;
    weechat::print($buffer,$string);
}

292
sub sec_to_min {
293
    my ($sec) = @_;
294
    if(! $sec) { return 0; }
295 296 297
    return int($sec/60).":".sprintf("%02d",$sec%60);
}

298
sub build_message {
299 300 301 302 303 304 305 306 307 308 309 310 311 312
    my ($artist, $album, $title, $ct, $tt) = @_;
    my $message = weechat::config_get_plugin("format");

    $message =~ s/%artist%/$artist/g;
    $message =~ s/%album%/$album/g;
    $message =~ s/%title%/$title/g;
    $message =~ s/%ct%/$ct/g;
    $message =~ s/%tt%/$tt/g;
    $message =~ s/\n//g;

    return $message;
}


313
sub load_defaults {
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    if(weechat::config_get_plugin("mpc_host") eq "") {
        weechat::config_set_plugin("mpc_host", "localhost");
    }
    if(weechat::config_get_plugin("mpc_port") eq ""){
        weechat::config_set_plugin("mpc_port", 6600);
    }
    if(weechat::config_get_plugin("ncmpcpp_host") eq ""){
        weechat::config_set_plugin("ncmpcpp_host", "localhost");
    }
    if(weechat::config_get_plugin("ncmpcpp_port") eq ""){
        weechat::config_set_plugin("ncmpcpp_port", "6600"); 
    }
    if(weechat::config_get_plugin("left_string" eq "")) {
        weechat::config_set_plugin("left_string", "/me np");
    }
    if(weechat::config_get_plugin("right_string" eq "")) {
        weechat::config_set_plugin("right_string", "");
    }
    if(weechat::config_get_plugin("format") eq "") {
333 334
        weechat::config_set_plugin("format", "%artist%(%album%) ".
                                             "- %title% [%ct%/%tt%]");
335 336
    }
}