<?php
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
for any comment send a mail to finss@free.fr
*/
function tribune_lastfm_info()
{
return t('Post the last entry on audioscrobbler');
}
function tribune_lastfm_help()
{
$help = t('Type "/lastfm <em>login</em>" to get the last song scrobbled
at http://last.fm for the login <em>login</em>.');
return $help;
}
function tribune_lastfm_filter (&$post)
{
if (preg_match(':^/lastfm (.+)$:', $post['message'] , $matches) )
{
$new_message = tribune_lastfm_getinfos($matches[1],$post);
return $new_message;
}
elseif (preg_match(':^/lastfm$:', $post['message']) )
{
if( user_is_logged_in() )
{
$login_lastfm = $post['login'];
}
else
{
$login_lastfm = $post['info'];
$login_lastfm = preg_replace('/([^\w?!;_\-]+)/iS','',$login_lastfm);
// on ne garde que les carac alphanumeriques les ! et autres signes mais pas les / ou les espaces de l'ua comme login lastfm
}
$new_message = tribune_lastfm_getinfos( $login_lastfm, $post);
return $new_message;
}
}
function tribune_lastfm_getinfos( $login_lastfm, &$post)
{
$message = $login_lastfm;
$url = "http://ws.audioscrobbler.com/1.0/user/".$login_lastfm."/recenttracks.xml";
try
{
if(! $ch = curl_init() )
{
die("Erreur de chargement");
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(!$result = curl_exec($ch))
{
die("Erreur de chargement");
}
elseif( $result =="No user exists with this name.") // retour non-xml de la part de audioscrobbler
{
$message = "Unknow user ".$login_lastfm;
'info' => 'LastFMbot',
'message' => $message,
'login' => 'LastFMbot',
);
// $post = false; // n'affiche pas le post de l'utilisateur
return array ($new_message);
}
$xml = new SimpleXMLElement(/*utf8_encode(*/$result, LIBXML_NOERROR | LIBXML_NOWARNING);
$node = $xml->xpath("/recenttracks/track");
$timestamp = $node[0]->date['uts']-0; // le zero est la pour forcer le type a etre un entier
$date = format_date($timestamp,'large',variable_get('date_default_timezone', 0));
$message = $login_lastfm.' '.t('listened ').$node[0]->artist.' - '.$node[0]->name.t(' on ').t($date );
}
catch (Exception $e)
{
// le message de l'utilisateur est affiché ainsi que l'erreur
'info' => 'LastFMbot',
'message' => $e->getMessage(),
'login' => 'error',
);
return array ($new_message);
}
'info' => 'LastFMbot',
'message' => $message,
'login' => 'LastFMbot',
);
// $post = false; // n'affiche pas le post de l'utilisateur
return array ($new_message);
}