Half working "currently playing script" for Xchat - Horscht - 2012-11-03
Hello There,
a few years ago I created a perl script (with the help of a friend) that allowed me to use the HTTP API to grab the currently playing item from xbmc and output the results to Xchat2. Unfortunatelly with the HTTP API being deprecated now I can no longer use it. I decided to put it here, maybe someone can modify it so it uses the JSON API.
Code: # get_xbmc.pl
# by StarG (c) 2007
#
# licenced under gplv3
# see www.gnu.org/copyleft for details
use LWP::UserAgent;
use Data::Dumper;
use strict;
use Encode;
require 5.6.0; # minimum for utf8 support
# enter here the url to the xmbc webserver including get playlist command
my $xbmc_url = "http://xbmc:[email protected]:8080/xbmcCmds/xbmcHttp?command=getcurrentlyplaying";
my $xbmc_delay = 15;
my $xbmc_active = 0;
my $xmbc_version = 0.1;
my $bullet = "\x{2022}";
initXBMC();
sub collectData {
# my $doc = get($xbmc_url);
my $doc;
my %data;
my $ua = LWP::UserAgent->new();
$ua->timeout(1);
my $request = HTTP::Request->new('GET', $xbmc_url);
my $response = $ua->request($request);
#if ( length($doc) > 0 )
if ( !$response->is_error() )
{
$doc = $response->content();
while ( $doc =~ /<li>(.+?):([^<]+?)$/mig )
{
my $key = $1;
my $value = $2;
# cut trailing backslashes
chomp( $value );
chomp( $value );
# convert possible utf8 content to ascii
$value = decode( "utf8", $value );
$data{$key} = $value;
}
}
else
{
$data{'error'} = 1;
}
# print Dumper( \%data );
return \%data;
}
sub initXBMC {
Xchat::register( "xbmc song paster", $xmbc_version, "...", sub { } );
Xchat::hook_command( "gog", \&pasteStatusXBMC );
}
sub pasteStatusXBMC {
my $dataref = collectData();
my %data = %{$dataref} if $dataref =~ /HASH/;
my $msg;
my $prc = "unknown";
my $valuein = "";
my $filenew = "";
if ( defined $data{'Percentage'} )
{
if ( $data{'Percentage'} <= 10 )
{
$prc = "[ _________]";
}
elsif ($data{'Percentage'} <= 20 )
{
$prc = "[_ ________]";
}
elsif ($data{'Percentage'} <= 30 )
{
$prc = "[__ _______]";
}
elsif ($data{'Percentage'} <= 40 )
{
$prc = "[___ ______]";
}
elsif ($data{'Percentage'} <= 50 )
{
$prc = "[____ _____]";
}
elsif ($data{'Percentage'} <= 60 )
{
$prc = "[_____ ____]";
}
elsif ($data{'Percentage'} <= 70 )
{
$prc = "[______ ___]";
}
elsif ($data{'Percentage'} <= 80 )
{
$prc = "[_______ __]";
}
elsif ($data{'Percentage'} <= 90 )
{
$prc = "[________ _]";
}
elsif ($data{'Percentage'} <= 100 )
{
$prc = "[_________ ]";
}
}
if ( $data{'error'} ne 1 )
{
if ( defined $data{'Type'} )
{
if ( $data{'Type'} eq "Video" )
{
if ( $data{'Show Title'} ne "")
{
$msg = "[XBMC Playing] $bullet TV-Show $bullet [ $data{'Show Title'} - Season:$data{'Season'} Episode:$data{'Episode'} - $data{'Title'} ] $bullet $prc $bullet [ $data{'Percentage'}% ]";
}
elsif ($data{'Title'} ne "")
{
$msg = "[XBMC Playing] $bullet Video $bullet [ $data{'Title'} ] $bullet $prc $bullet [ $data{'Percentage'}% ]";
}
else
{
$valuein = rindex($data{'Filename'},"/") + 1;
$filenew = substr($data{'Filename'},$valuein);
$msg = "[XBMC Playing] $bullet Video $bullet [ $filenew ] $bullet $prc $bullet [ $data{'Percentage'}% ]";
}
}
elsif ( $data{'Type'} eq "Audio" )
{
$msg = "is listening to \"$data{'Title'}\" by \"$data{'Artist'}\" from the Album \"$data{'Album'}\"";
}
elsif ( $data{'Type'} eq "Picture" )
{
$msg = "[XBMC Playing] $bullet Picture $bullet [ $data{'Filename'} $bullet Width:$data{'Width'} $bullet Height:$data{'Height'} ]";
}
else
{
$msg = "unknown state";
}
}
else
{
$msg = "XBMC not playing anything!";
}
}
else
{
$msg = "XBMC offline"
}
if ( $data{'Type'} eq "Video" || $data{'Type'} eq "Audio" || $data{'Type'} eq "Picture")
{
Xchat::command("me $msg" )
}
else
{
Xchat::command("echo $msg" )
}
}
sub handleXMBC {
if ( $xbmc_active eq 0 )
{
$xbmc_active = 1;
Xchat::print( "[XBMC] showing songs!");
}
else
{
$xbmc_active = 0;
Xchat::print( "[XBMC] stopped!");
}
}
1;
RE: Half working "currently playing script" for Xchat - baspt - 2012-11-21
This script should work: https://github.com/baspt/xbmc-hexchat
RE: Half working "currently playing script" for Xchat - khaoohs - 2012-11-24
(2012-11-21, 16:42)baspt Wrote: This script should work: https://github.com/baspt/xbmc-hexchat
Thank you.
RE: Half working "currently playing script" for Xchat - Horscht - 2012-11-24
Thank you, I will try to get it working on my system later. I tried it just now and Xchat froze. I'm gonna look into that
|