Btplay.current

From Chumby Wiki
Revision as of 21:09, 21 April 2008 by Wayn3w (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

btplay.current is a CGI script that will print out the contents of btplay.properties as XML to make it easier for widgets to grab. In layman's terms, you can get the information about the currently playing music selection from btplay for use in your widgets.

Directions:

  1. Copy and paste this script into /psp/cgi-bin/btplay.current on your chumby
  2. change its permission to be executable by typing : chmod +x /psp/cgi-bin/btplay.current
  3. use the URL http://127.0.0.1/cgi-bin/custom/btplay.current to get the XML.
#!/usr/bin/perl -w

# Prints a recursive data structure as XML, 
# where the keys are tag names.
sub print_data
{
    my $tabstop = shift;
    my $tag = shift;
    my $data = shift;

    print "\t" x $tabstop;
    print "<", $tag, ">"; 
    if ( ref($data) eq "SCALAR" ) {
        print $$data;
    } elsif ( ref($data) eq "HASH" ) {
        print "\n";
        for my $key ( reverse keys %{$data} ) {
            print_data( $tabstop+1, $key, $data->{$key} );
        }
        print "\t" x $tabstop;
    } else {
        print $data;
    }
    print "</", $tag, ">\n"; 

}

#  Build up a hash of hashes:
#  name1/name2=data turns into
#  $hash{name1}{name2} = data;
#
#  This version allows any number of names on a line
#
sub build_data {
    my $filename = shift;
    my $hash = shift;

    open FILE, $filename or die "Cannot open $filename: $!";
    while(<FILE>) {
        chomp;
        my ( $names, $data ) = split /=/;
        my @names = split/\//, $names;

        my $current_hash = $hash;
        my $last = pop( @names );
        foreach my $name ( @names ) {
            if ( !exists $current_hash->{ $name } )  {
                $current_hash->{ $name } = {};
            }
            $current_hash = $current_hash->{ $name };
        }
        $current_hash->{ $last } = $data;
    }
    close FILE;
}
 
my %hash;
my $filename = "/var/run/btplay.properties";

print "Content-type: text/xml\n\n";
print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
build_data($filename, \%hash);
print_data(0 , "song", \%hash);

1;