Now Listening To…
I just got a “Now Listening” thing setup for Wordpress and amaroK. I modified the Now Playing script to use scp to copy a file containing info on what’s playing to the webserver. I then wrote a PHP script which parsed this file and inserted the info into a table. Then I wrote a WordPress plugin to extract the info from the table and append it onto the end of a post. Sometime I’ll post a patch and put it up on my website. I’ve got two other posts that I wrote a few days ago (when I was board), but never finished them–maybe they’ll make it up sometime.
It’s a nasty hack, but it works.
EDIT 2007/05/16 :: No, I never made it any prettier, however, if you care, here’s the a letter I sent to someone on how to implement a similar thing in your blog. Use with caution, and always make a backup of your files and DB. I’m not responsible if anything blows up. You keep all the pieces.
I’ve got a script running in Amarok that uses uploads info on the currently playing song with FTP into a file like this. This file gets
the info off my computer and onto the server.
On the server side of things:
I have a table called now_playing in the same DB as WP. It’s created like this:
CREATE TABLE `now_playing` (
`id` int(10) unsigned NOT NULL auto_increment,
`post_id` bigint(20) unsigned NOT NULL default '0',
`title` varchar(255) default NULL,
`artist` varchar(255) default NULL,
`album` varchar(255) default NULL,
PRIMARY KEY (`id`)
);
In wp-admin/post.php is where most of the code is. The code starts around line 37 inside the
'switch($action) { case 'post':'
statement.
This is the code I have:
case 'post':
check_admin_referer();
$post_ID = write_post();
// deal with nowPlaying
$file = "/home/www/wildgardenseed.com/Taj/amarok/nowPlaying.txt";
if (!$lines=@file($file)) {
//echo "Failed to open file";
$music_query = "INSERT INTO now_playing(post_id,title,artist,album) VALUES ('$post_ID', NULL,NULL,NULL)";
$result = $wpdb->query($music_query);
}
else {
foreach ($lines as $line_num => $line) {
switch ($line) {
case (strpos($line,"isPlaying")):
if ($line=="isPlaying: true\n") {
$info["is_playing"]=true;
}
else {
$info["is_playing"]=false;
}
break;
case (strpos($line,"artist")):
$info["artist"]=str_replace("\n","",substr($line,8));
break;
case (strpos($line,"title")):
$info["title"]=str_replace("\n","",substr($line,7));
break;
case (strpos($line,"album")):
$info["album"]=str_replace("\n","",substr($line,7));
break;
}
}
if ($info["is_playing"]) {
$music_query = "INSERT INTO now_playing (post_id,title,artist,album) VALUES ('$post_ID', '".mysql_escape_string($info["title"])."', '".mysql_escape_string($info["artist"])."', '".mysql_escape_string($info["album"])."')";
$result = $wpdb->query($music_query);
}
else {
$music_query = "INSERT INTO now_playing (post_id,title,artist,album) VALUES ('$post_ID',
'Silence',NULL,NULL)";
$result = $wpdb->query($music_query);
}
}
// Done with nowPlaying
// Redirect.
[…continue normal code]
Of course, change the $file variable to contain the full path to the file that you’ve uploaded.
The above code reads the file and stores it in the DB. To display the info at the end of each post, I wrote this plugin. It goes in wp-content/plugins/nowplaying.php:
<?php
# -- WordPress Plugin Interface -----------------------------------------------
/*
Plugin Name: Now Playing
Plugin URI: http://www.wildgardenseed.com/Taj
Description: Now Playing takes info from the now_playing table and
appends it to the post_content
Version: 1.0.0
Author: Taj Morton
Author URI: http://www.wildgardenseed.com/Taj/blog
*/
function add_nowplaying($content) {
$song=get_the_song();
//if all columns are null, then don't output anything--if amarok
isn't running, then we're listening to "Silence"
if ($song->title=='Silence') {
$content.="Listening to <strong>Silence</strong>";
}
elseif($song->artist) {
$content.="<p>Listening to
<strong>".htmlentities($song->title)."</strong> by
<strong>".htmlentities($song->artist)."</strong> on
<strong>".htmlentities($song->album)."</strong>.</p>";
}
return $content;
}
add_filter('the_content','add_nowplaying');
?>
That just might work! Be sure to make a backup first, and always play with a local copy. Good luck!
August 10th, 2006 at 9:51 am
Ever get that kludged up into a prettier bit of code? I’d love to run it as a plugin on my WP site.
May 16th, 2007 at 12:48 pm
yeah that sounds like a cool plugin…
you finished it already?