Getting current track information from VLC (using Perl!)

February 9th, 2010 gkurts No comments

So I’ve already done a post on getting track information from Rythmbox, but VLC felt cold and left out, so here it is. The process is pretty much the same, just tweaked a bit for VLC.

First, go into VLC, Tools -> Preferences, click the “All” radio button (near the bottom), Interface, click “Main interfaces” and check “HTTP…”. If you’re feeling adventurous, choose “HTTP” from the list and you can specify port (default is 8080), address to listen on, etc.. I’m not doing all of that.

Now, restart VLC and crank up a stream or MP3. You should be able to navigate to http://localhost:8080/requests/status.xml and see an XML file representing the current track information.

If you are listening to an MP3, the “Artist” and “Title” fields should be populated. If you are streaming music, the artist and title will appear in the now_playing field as “Artist – Title”. I have no clue why this is. It just is.

Simple enough.. Here is my Perl script to grab the info, thrash it about, and do what we want with it. Simple stick it in cron to run every minute (or whatever) – */1 * * * * /home/myuser/scripts/tuneUpdater.pl

#!/usr/bin/perl

use XML::Simple;
use LWP::Simple;
use DBI;

my $date = `date +"%D %r"`;
chomp $date;

## URL to vlc status.xml file
my $url = "http://localhost:8080/requests/status.xml";
my $data = get($url);

## exit if VLC isn't running
if ($data eq "") {
   exit(0);
}

## get the xml and read in the appropriate values
my $xs = XML::Simple->new(SuppressEmpty => 1);
my $root = $xs->XMLin($data);
my $information = $root->{'information'};
my $meta = $information->{'meta-information'};

## this is what we are after
my $title = $meta{'title'};
my $artist = $meta{'artist'};

## if the title or artist is blank, we must be streaming, so grab the
## now_playing field.
if (($title eq "") || ($artist eq "")) {
	my $nowplaying = $meta->{'now_playing'};

	($artist, $title) = split(/\ -\ /, $nowplaying);
}

## strip out Amped FM "Enhanced MP3" stuff.
$title =~ s/\ Enhanced\ MP3//;

## if we got this far safely, connect to the database.
$dbh = DBI->connect('DBI:mysql:mydb;host=somewebhost', 'luser', 'notreally')
    || die "Could not connect to database: $DBI::errstr";

my ($last_artist, $last_title) = &getLastSong();

## update the db if the artist or title differ from the last one updated.
if (($last_artist ne $artist) || ($last_title ne $title)) {
    &updateLastSong($artist, $title);
}

$dbh->disconnect();

## update the database to add the current artist and title
sub updateLastSong {
    my ($artist, $title) = @_;

    $sql_update = "INSERT INTO music_history (id, title, artiist, played) VALUES (null, '$title', '$artist', '$date')";
    $out_update = $dbh->prepare($sql_update);
    $out_update->execute;
    $out_update->finish;
}

## get the last artist and title played.
sub getLastSong {
    $sql_last = "SELECT artiist, title FROM music_history order by id desc limit 1";
    $out_last = $dbh->prepare($sql_last);
    $out_last->execute;
    my ($last_artist, $last_title) = $out_last->fetchrow_array;
    $out_last->finish;

    return ($last_artist, $last_title);
}

Feel free to contact me with any questions or comments!

GodMode in Windows 7

February 3rd, 2010 gkurts No comments

To enter “GodMode,” simply create a new folder and then rename the folder to the following:

GodMode.{ED7BA470-8E54-465E-825C-99712043E01C}

http://news.cnet.com/8301-13860_3-10423985-56.html?tag=mncol;txt

Categories: Hacks, Windows 7 Tags: ,

Putting a Recently Played Songlist in WordPress Sidebar

January 3rd, 2010 gkurts No comments

For no real reason other than saying that I did it, I wanted to have the last 10 songs that I’ve played on my PC listed in the sidebar of my blog. Simple enough..

Here’s what I have: I’m a fan of Last.FM, plus I have about 25Gigs of mp3s that I like to randomize. I also like simplicity, so I use RhythmBox on my Ubuntu desktop. It has a built-in interface to Last.FM (among others) and handles my mp3 library with ease. It also is easily extensible.

At first, I was going to write my own plugin to get the song info, then I found the NowPlaying XML plugin. Perfect! Simply unzip it to your ~/.gnome2/rhythmbox/plugins directory and you’re set (you may have to create the directory first, as did I). Fire up RhythmBox, click “Edit”, “Plugins” and make sure that your NowPlaying XML plugin is checked. Play a song and it should create a file at /tmp/nowplaying.xml.

The /tmp/nowplaying.xml file looks something like this:

<?xml version="1.0" ?><nowplaying><song><album>Saturate</album><track-number>0</track-number><title>Polyamorous</title><artist>Breaking Benjamin</artist><genre></genre><duration>177</duration><bitrate>0</bitrate></song></nowplaying>

Yep, all one line, but that’s okay. It has the data that we want…

Now, to get it into WordPress. First, I created a table in my blog database:

create table music_history (id int primary key not null auto_increment, title nvarchar(30), artiist nvarchar(30), played nvarchar(30));

Yes, I mis-spelled artist, and yes, I probably should have used a datetime data type for played, but work with me here…

Next, I created a Perl script on my workstation called updatePlayHistory.pl that looks something like this:

#!/usr/bin/perl

use Date::Format;
use XML::Simple;

$now = time2str("%D %r", time);

$file = '/tmp/nowplaying.xml';

## make sure that the file exists, if not just exit.
unless (-e $file) { exit(0); }

$xml = new XML::Simple;

## read the xml file in
$data = $xml->XMLin("/tmp/nowplaying.xml");

## get our song object
$song = $data->{song};

## get the song details
$title = $song->{title};
$artist = $song->{artist};

## set up our connection
use DBI;
$dbh = DBI->connect('DBI:mysql:blogdatabase;host=myhost', 'dbuser', 'dbpass')
    || die "Could not connect to the database: $DBI::errstr";

## get the last song inserted and see if it is the same as the current one
$sql_last = "SELECT id, artiist, title FROM music_history order by id desc limit 1";
$out_last = $dbh->prepare($sql_last);
$out_last->execute;
($id, $lastArtist, $lastTitle) = $out_last->fetchrow_array;
$out_last->finish;

## if its the same one, exit out
if (($lastArtist eq $artist) && ($lastTitle eq $title)) {
    $dbh->disconnect();
    exit(0);
}

## insert the song

$sql_insert = "INSERT INTO music_history (title, artiist, played) VALUES ('$title', '$artist', '$now')";
$out_insert = $dbh->prepare($sql_insert);
$out_insert->execute;
$out_insert->finish;

$dbh->disconnect();

The comments should be pretty self explanatory. Tweak as needed. Now, ‘chmod +x’ your script edit your crontab (‘crontab -e’) to run it every minute:

* * * * * * /home/gkurts/scripts/updatePlayHistory.pl

Last, we need to tweak WordPress to display the last 10 played songs. For this, I just copied some of the existing code for one of the widgets and modified it to select the last 10 played and loop through them..

<?php
   $songs = $wpdb->get_results("SELECT id, title, artiist, played from music_history order by id desc limit 10");
?>
   <div class="widget">
      <h3>Song History <span style="font-size: 10pt">(Last 10 played)</span></h3>

      <?php if($songs) : foreach($songs as $song) : ?>
         <ul>
            <li><b><?php echo $song->title; ?></b> by <b><?php echo $song->artiist; ?></b><p style="font-size: 7pt; margin-top: 1px; padding-left: 5px;"><?php echo $song->played; ?></p></li>
         </ul>
         <div class="fixed"></div>
      <?php endforeach; endif; ?>
   </div>

That should be it. It’s really pretty simple. If you use this or have any questions, let me know!

Categories: Linux, MySQL, Perl, Php, Ubuntu, WordPress Tags:

TweetDeck on Ubuntu 9.10 64-bit

January 2nd, 2010 gkurts 1 comment

If you’ve tried to install TweetDeck on Ubuntu 9.10 (Karmic Koala) 64-bit, you’ve probably run into the error saying that something like “TweetDeck has encountered problems”… blah blah blah “we are working with Adobe on it”… etc etc etc. (Sorry, I forgot to get a screenshot of the actual error).

The problem stems from Adobe AIR being dependent on 32-bit libraries and not being able (obviously) to find them in a 64-bit environment. Luckily, there is an easy solution for it!

Assuming that you have already installed Adobe AIR and TweetDeck at this point (if not, go ahead, it’s safe), do the following:

  • close TweetDeck if for some reason you already haven’t.
  • wget http://frozenfox.freehostia.com/cappy/getlibs-all.deb
  • sudo dpkg -i getlibs-all.deb
  • sudo apt-get install lib32asound2 lib32gcc1 lib32ncurses5 lib32stdc++6 lib32z1 libc6 libc6-i386 lib32nss-mdns
  • sudo apt-get install ia32-libs
  • sudo getlibs -l libgnome-keyring.so
  • sudo getlibs -l libgnome-keyring.so.0
  • sudo getlibs -l libgnome-keyring.so.0.1.1
  • sudo cp /usr/lib/libadobecertstore.so /usr/lib32
  • restart TweetDeck by clicking the application icon and you should be good to go.

Enjoy!

Categories: Linux, Ubuntu Tags:

Fighting with VMWare Clones

January 1st, 2010 gkurts No comments

Why can’t things just be simple? Really, it’s only a few steps… Surely the folks at VMWare could fix this. Ugh!

When cloning a Linux virtual machine (Ubuntu Server, in my case – YMMV based on distro), the clone’s networking fails to start up. Doing an ifconfig only shows the loopback adapter and no amount of rebooting, adding /  removing the adapter in the VM’s settings, or cussing will make it work.

Thanks to this post I found by Jamis Buck (thanks!), it’s easily fixed (okay, his post was for VMWare Fusion, but it works equally well in VMWare Workstation):

  • “sudo hostname blah”, to set the hostname. This doesn’t change it permanently, so you’ll also want to:
  • “sudo vim /etc/hostname”. Change the contents of the file to the hostname you want. Then:
  • “sudo vim /etc/hosts”. Replace all mentions of the old hostname with the new hostname.
  • “sudo vim /etc/udev/rules.d/70-persistent-net.rules”. There will be two entries in this file. The first points eth0 at the old MAC address, and the second points eth1 at the new. Go ahead and delete the first entry, and change “eth1” to “eth0” in the second (and now only) entry.
  • “sudo shutdown -r now” to restart your virtual machine.

Again, why can’t they fix this?!?! Oh well, enough ranting for the day.

Categories: Linux, VMWare Tags:

Linkage for 1/1/2010

December 31st, 2009 gkurts No comments

Save MySQL and the Internet

So Oracle is buying Sun, and in-turn, is getting MySQL… Something they’ve tried to aquire several times in the past. Monty Widenus, the original founder of MySQL, is pleading for people to sign a petition for Sun to structure the sale agreement in such a way to prevent Oracle from getting becoming the owner of MySQL.

Visit http://www.helpmysql.org/en/petition and sign the petition to help do your part!

Visit Monty’s blog for a thorough Q&A on the matter. Good reading.

Categories: MorningPaper, MySQL, Open Source Tags: