lastfm

lastfm.inc

  1. <?php
  2. /*
  3.             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  4.                     Version 2, December 2004
  5.  
  6.  Everyone is permitted to copy and distribute verbatim or modified
  7.  copies of this license document, and changing it is allowed as long
  8.  as the name is changed.
  9.  
  10.             DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
  11.    TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  12.  
  13.   0. You just DO WHAT THE FUCK YOU WANT TO.
  14.  
  15. for any comment send a mail to finss@free.fr
  16.  
  17. */
  18.  
  19. function tribune_lastfm_info()
  20. {
  21.   return t('Post the last entry on audioscrobbler');
  22. }
  23.  
  24. function tribune_lastfm_help()
  25. {
  26.   $user = array_shift(user_load_self(array()));
  27.   $help = t('Type "/lastfm <em>login</em>" to get the last song scrobbled
  28. at http://last.fm for the login <em>login</em>.');
  29.   return $help;
  30. }
  31.  
  32. function tribune_lastfm_filter (&$post)
  33. {
  34.   if (preg_match(':^/lastfm (.+)$:', $post['message'] , $matches) )
  35.     {
  36.       $new_message = tribune_lastfm_getinfos($matches[1],$post);
  37.       return $new_message;
  38.       }
  39.   elseif (preg_match(':^/lastfm$:', $post['message']) )
  40.     {
  41.       if( user_is_logged_in() )
  42.         {
  43.           $login_lastfm = $post['login'];
  44.         }
  45.       else
  46.         {
  47.           $login_lastfm = $post['info'];
  48.           $login_lastfm = preg_replace('/([^\w?!;_\-]+)/iS','',$login_lastfm);
  49. // on ne garde que les carac alphanumeriques les ! et autres signes mais pas les / ou les espaces de l'ua comme login lastfm
  50.         }
  51.       $new_message = tribune_lastfm_getinfos( $login_lastfm, $post);
  52.       return $new_message;
  53.     }
  54. }
  55.  
  56.  
  57. function tribune_lastfm_getinfos( $login_lastfm, &$post)
  58. {
  59.   $message = $login_lastfm;
  60.   $url = "http://ws.audioscrobbler.com/1.0/user/".$login_lastfm."/recenttracks.xml";
  61.   try
  62.     {
  63.       if(!  $ch = curl_init() )
  64.         {
  65.           die("Erreur de chargement");
  66.         }
  67.       curl_setopt($ch, CURLOPT_URL, $url);
  68.       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  69.       if(!$result = curl_exec($ch))
  70.         {
  71.           die("Erreur de chargement");
  72.         }
  73.       elseif( $result =="No user exists with this name.") // retour non-xml de la part de audioscrobbler
  74.         {
  75.           $message = "Unknow user ".$login_lastfm;
  76.           $new_message = array(
  77.                                'info' => 'LastFMbot',
  78.                                'message' => $message,
  79.                                'login' => 'LastFMbot',
  80.                                );
  81.           //      $post = false;                // n'affiche pas le post de l'utilisateur
  82.           return array ($new_message);
  83.         }
  84.       $xml = new SimpleXMLElement(/*utf8_encode(*/$result, LIBXML_NOERROR | LIBXML_NOWARNING);
  85.       $node = $xml->xpath("/recenttracks/track");
  86.       $timestamp = $node[0]->date['uts']-0; // le zero est la pour forcer le type a etre un entier
  87.       $date = format_date($timestamp,'large',variable_get('date_default_timezone', 0));
  88.       $message =  $login_lastfm.' '.t('listened ').$node[0]->artist.' - '.$node[0]->name.t(' on ').t($date );
  89.     }
  90.   catch (Exception $e)
  91.     {
  92.       // le message de l'utilisateur est affiché ainsi que l'erreur
  93.       $new_message = array(
  94.                            'info' => 'LastFMbot',
  95.                            'message' => $e->getMessage(),
  96.                            'login' => 'error',
  97.                            );
  98.       return array ($new_message);
  99.     }
  100.   $new_message = array(
  101.                        'info' => 'LastFMbot',
  102.                        'message' => $message,
  103.                        'login' => 'LastFMbot',
  104.                        );
  105.   //  $post = false;                // n'affiche pas le post de l'utilisateur
  106.   return array ($new_message);
  107. }
  108.  
  109.