<?php
class NP_AudioScrobbler extends NucleusPlugin {
function getName() { return 'AudioScrobbler plugin'; }
function getAuthor() { return 'Jef Pober'; }
function getURL() { return 'http://fuckhedz.com'; }
function getVersion() { return '1.0'; }
function getDescription() { return 'Shows a Now Playing underneath each item'; }
function getEventList() { return array('PostAddItem'); }
function install() {
$create_q = sql_query('CREATE TABLE IF NOT EXISTS ' . sql_table('plug_audioscrobbler') . ' (' .
'itemid INT(9) PRIMARY KEY NOT NULL, ' .
'title VARCHAR(255) NOT NULL)');
$this->createMemberOption('asid','Audio Scrobbler ID','text','');
$this->createBlogOption('enable','Enable AudioScrobbler display for this blog','yesno','yes');
$this->createOption('offset','Time offset (in hours to GMT)','text','-6');
}
function event_PostAddItem($data) {
global $member;
$itemid = $data['itemid'];
$asid = $this->getMemberOption($member->id,'asid');
if ($asid) {
$feedURL = 'http://ws.audioscrobbler.com/rdf/history/' . $asid;
$result = $this->readFeed($feedURL);
$timestamp = $this->parse_w3cdtf(trim($result[2]['date']));
$title = trim($result[2]['description']);
$time = time() + $this->getOption('offset')*60*60;
$diff = $timestamp - $time;
if ($diff > -1800 && $diff < 900) {
if ($title) {
sql_query('INSERT INTO ' . sql_table('plug_audioscrobbler') . " VALUES ($itemid,'$title')");
}
}
}
}
function doTemplateVar(&$item) {
global $blog;
$itemid = $item->itemid;
$blogid = $blog->id;
if ($this->getBlogOption($blogid,'enable') == 'yes') {
$result = sql_query('SELECT title FROM ' . sql_table('plug_audioscrobbler') . " WHERE itemid=$itemid");
if (mysql_num_rows($result) > 0) {
$title = mysql_result($result,0);
echo 'NP: ' . $title;
}
}
}
function readFeed($feedURL) {
// which URL to get
global $manager, $blog, $saxparser, $CONF, $contents, $last_modified_time;
$feedURL_parts = parse_url($feedURL);
$path = isset($feedURL_parts["path"]) ? $feedURL_parts["path"] : "/";
$filename = isset($feedURL_parts["host"]) ? $feedURL_parts["host"] : "feedfile";
$unique = isset($feedURL_parts["query"]) ? $feedURL_parts["query"] : "";
$contents = "";
$data = "";
$tag = "";
$isItem = false;
$i = 0;
unset($saxparser);
$saxparser = xml_parser_create();
xml_parser_set_option($saxparser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler($saxparser, 'sax_start', 'sax_end');
xml_set_character_data_handler($saxparser, 'sax_data');
if (!function_exists('sax_start')) {
function sax_start($parser, $name, $attribs) {
global $tag, $isItem, $i, $isChannel;
switch ($name){
case "channel":
$i++;
$isChannel = true;
break;
case "item":
$i++;
$isItem = true;
break;
case "image";
case "url";
case "docs";
case "language";
case "generator";
case "copyright";
case "dc:date":
case "title":
case "link":
case "pubDate";
case "description":
case "author";
case "category":
case "guid":
if ($isItem || $isChannel) $tag = $name;
break;
default:
$isItem = false;
$isChannel = false;
break;
}
}
}
if (!function_exists('sax_end')) {
function sax_end($parser, $name) {
}
}
if (!function_exists('sax_data')) {
function sax_data($parser, $data) {
global $tag, $isItem, $contents, $isChannel, $i;
if ($data != "\n" && $isItem) {
switch ($tag) {
case "dc:date";
case "description":
if($tag=='dc:date') $tag = 'date';
if (!isset($contents[$i-1][$tag]) || !strlen($contents[$i-1][$tag])) {
$contents[$i-1][$tag] = addslashes($data);
} else {
$contents[$i-1][$tag].= addslashes($data);
}
break;
}
}
}
}
$fp = fopen($feedURL, "r");
while ($data = fread($fp, 4096)) {
//echo $data;
$file .= $data;
}
$file = stripslashes($file);
$parsedOkay = xml_parse($saxparser, $file, feof($fp));
xml_parser_free($saxparser);
fclose($fp);
return $contents;
}
function parse_w3cdtf ($date_str) {
# regex to match wc3dtf
$pat = "/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2})(:(\d{2}))?(?:([-+])(\d{2}):?(\d{2})|(Z))?/";
if ( preg_match( $pat, $date_str, $match ) ) {
list( $year, $month, $day, $hours, $minutes, $seconds) =
array( $match[1], $match[2], $match[3], $match[4], $match[5], $match[6]);
# calc epoch for current date assuming GMT
$epoch = gmmktime( $hours, $minutes, $seconds, $month, $day, $year);
$offset = 0;
if ( $match[10] == 'Z' ) {
# zulu time, aka GMT
}
else {
list( $tz_mod, $tz_hour, $tz_min ) =
array( $match[8], $match[9], $match[10]);
# zero out the variables
if ( ! $tz_hour ) { $tz_hour = 0; }
if ( ! $tz_min ) { $tz_min = 0; }
$offset_secs = (($tz_hour*60)+$tz_min)*60;
# is timezone ahead of GMT? then subtract offset
#
if ( $tz_mod == '+' ) {
$offset_secs = $offset_secs * -1;
}
$offset = $offset_secs;
}
$epoch = $epoch + $offset;
return $epoch;
}
else {
return -1;
}
}
}
?>
audioscrobbler : Plugins