Permalink
Browse files

+ libre.fm scrobbler

from #8
  • Loading branch information...
l29ah committed Nov 9, 2016
1 parent ac9bf90 commit 846723397100ad6be78cf134528348f4cf85cbec
Showing with 46 additions and 0 deletions.
  1. +46 −0 libre.fm/librefm.pl
View
@@ -0,0 +1,46 @@
#!/usr/bin/perl
# ~/.config/librefm looks like this:
# $login = 'vpupkin';
# $password = 'mycoolpassword';
$hs_url = "http://turtle.libre.fm/";
$client_id = "lsd";
$client_version = "1.0.4";
$debug = 1;
use LWP::UserAgent;
use Digest::MD5 qw(md5_hex);
use URI::Escape;
die "Usage: librefm.pl <artist> <title> <album> <length>" unless $#ARGV==3;
$ua = LWP::UserAgent->new;
do "$ENV{HOME}/.config/librefm";
foreach ("login", "password") {
die "No $_ in ~/.config/librefm" unless ${$_};
}
my $resp = $ua->get($hs_url."?hs=true&p=1.1&c=$client_id&v=$client_version&u=$login");
die "Unable to handshake with $hs_url" unless $resp->is_success;
($status, $token, $url, $int) = split(/\n/, $resp->content, 4);
print "Status: $status\nHash: $hash\nUrl: $url\n$int" if $debug;
die "Handshake failed, server returned: $status=\n" unless $status =~ /UP(TO)?DATE/;
$hash = md5_hex(md5_hex($password).$token);
@t = gmtime();
$req_str = "u=$login&s=$hash&a[0]=".uri_escape($ARGV[0]).'&t[0]='.uri_escape($ARGV[1]).'&b[0]='.uri_escape($ARGV[2]).'&m[0]=&l[0]='.uri_escape($ARGV[3]).'&i[0]='.uri_escape(sprintf('%04d-%02d-%02d %02d:%02d:%02d', $t[5] + 1900, $t[4] + 1, @t[3, 2, 1, 0]));
print "$req_str\n" if $debug;
my $req = HTTP::Request->new(POST=>$url);
$req->content_type('application/x-www-form-urlencoded');
$req->content($req_str);
$resp = $ua->request($req);
($status, $int) = split(/\n/, $resp->content, 2);
print "Server returned: $status\n" if $debug;
die "Scrobble failed, server returned: $status\n" unless $status == "OK";

0 comments on commit 8467233

Please sign in to comment.