#!/usr/bin/perl
# mpdblip script
# released on BSD licence
# author Michal "Fogel" Fogelman (fogel@nocnemarki.net)
# August 2010
use strict;
require LWP::UserAgent;
# mpd settings
my $port = '6600';
my $host = 'localhost';
# blip settings
my $user = 'user';
my $pass = 'pass';
my $tag = '#slucham';
my $verbose = 0;
my @lines = `mpc -h $host -p $port`;
my $hour = `date +%H`;
my $ua = LWP::UserAgent->new;
$ua->agent("mpdblip");
sub get_page {
my $url = shift;
my $request = HTTP::Request->new(GET => "$url");
my $result = $ua->request($request);
return $result->content;
}
sub youtube {
my $query = shift;
if
(get_page("http://www.google.com/search?hl=en&q=\"$query\"\+site:youtube.com")
=~ /href="(http:\/\/www\.youtube\.com.+?)"/) {
return $1;
}
return "";
}
sub www_title {
my $url = shift;
my $content = get_page($url);
if ($content =~ /
(.+?)<\/title>/i) {
return $1;
} elsif ($content =~ //i) {
return $1;
}
return "";
}
if ($ARGV[0] =~ /--help/) {
print "\nusage: mpdblip [--no-youtube] [text]\n\n";
exit;
}
if ($lines[1] =~ m/playing/) {
my $line = "$tag mpd:";
chop($line .= ' ' . $lines[0]);
if ($lines[1] =~ /\s+(\d+\:\d+\/\d+\:\d+)\s+/) {
$line .= " ($1)";
}
my $link = "";
if ($ARGV[0] !~ /^--no-youtube$/) {
$link = youtube($lines[0]);
my $title = www_title($link);
$title =~ s/"/"/g;
print "$link\nURL: $title\nInclude link [Y/n/c]?";
my $ans = ;
if ($ans =~ /^n/i) {
} elsif ($ans =~ /^c/i) {
print "canceled\n";
exit;
} else {
$line .= " $link";
}
}
else { shift }
foreach my $arg(@ARGV) {
$line .= ' ' . $arg;
}
if ($hour >= 1 && $hour <= 5) {
$line = "#nocnemarki " . $line;
}
if (length($line) > 160) {
print "status too long\n";
} else {
my $v = "";
if ($verbose == 1) {
$v = '-v';
}
my $resp = `curl $v -H'Accept: application/json'
-H'X-Blip-API: 0.02' -H'X-Blip-Application: mpdblip' -u $user:$pass -F
\"update[body]=$line\" http://api.blip.pl/updates`;
if ($resp =~
/"created_at":"\d+-\d+-\d+\s\d+:\d+:\d+","id":(\d+)}/) {
print "status has been created:
http://blip.pl/s/$1\n";
} else {
if ($verbose == 1) { print "$resp\n\n" }
print "error occured\n";
}
}
} else {
print "not playing at the moment\n";
}