Share Pastebin
Guest
Public paste!

cypherq

By: a guest | Sep 17th, 2009 | Syntax: PHP | Size: 5.90 KB | Hits: 304 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1. <?php
  2.  
  3. /**
  4.  * Last.fm to Twitter/Blip class
  5.  *
  6.  * Display your favorite songs from Last.fm list on Twitter of Blip.
  7.  *
  8.  * @author Harv - http://harv.pl adapted to OOP by cypherq - http://cypherq.wordpress.com
  9.  * @license Beerware - http://pl.wikipedia.org/wiki/Beerware
  10.  * @version 1.0
  11.  * @example /example.php Simple example
  12.  */
  13. class Blitter
  14. {
  15.  
  16.     /**
  17.      * User, password. For Twitter and Blip both
  18.      */
  19.     private $user;
  20.     private $pass;
  21.  
  22.     /**
  23.      * Set service to use (Capital letter at first)
  24.      * Ex. private $service = 'Blip';
  25.      */
  26.     private $service;
  27.  
  28.     /**
  29.      * Last.fm user name
  30.      */
  31.     private $lfm_user;
  32.  
  33.     /**
  34.      * Various support variables
  35.      */
  36.     private $tmp_fn = "lsfm_tw";
  37.     private $buffer;
  38.     private $message;
  39.  
  40.     /**
  41.      *  API's, URL's
  42.      */
  43.     private $tw_url  = 'http://twitter.com/statuses/update.xml';
  44.     private $bl_url  = 'http://api.blip.pl/updates';
  45.  
  46.     /**
  47.      * Constructor - only for assign variables passed by params
  48.      *
  49.      * @param string $user Blip/Twitter user name
  50.      * @param string $pass Blip/Twitter password
  51.      * @param string $lfm_user Last.fm username
  52.      * @param string $service Service name (where we want post)
  53.      */
  54.     public function __construct($user, $pass, $lfm_user, $service)
  55.     {
  56.         $this -> user = $user;
  57.         $this -> pass = $pass;
  58.         $this -> lfm_user = $lfm_user;
  59.         $this -> service = $service;
  60.     }
  61.  
  62.     public function post()
  63.     {
  64.         switch($this -> service)
  65.         {
  66.             case 'Blip': $this -> postBlip();
  67.                 break;
  68.  
  69.                 case 'Twitter': $this -> postTwitter();
  70.                     break;
  71.                 }
  72.             }
  73.  
  74.     /**
  75.       * Method sending message to Twitter
  76.       *
  77.       * @param string $message Prepared message to send
  78.       */
  79.             public function postTwitter()
  80.             {
  81.                 $curl_handle = curl_init();
  82.                 curl_setopt($curl_handle, CURLOPT_URL, $this -> tw_url);
  83.                 curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
  84.                 curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
  85.                 curl_setopt($curl_handle, CURLOPT_POST,1);
  86.                 curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'status='.$this -> message);
  87.                 curl_setopt($curl_handle, CURLOPT_USERPWD, $this -> user.':'.$this -> pass);
  88.                 $this -> buffer = curl_exec($curl_handle);
  89.                 curl_close($curl_handle);
  90.  
  91.                 if ($this -> buffer)
  92.                 {
  93.                     return TRUE;
  94.                 }
  95.                 else
  96.                 {
  97.                     return $this -> buffer;
  98.                 }
  99.             }
  100.  
  101.     /**
  102.      * Method sending message to Blip
  103.      *
  104.      * @param string $message Prepared message to send
  105.      */
  106.             public function postBlip()
  107.             {
  108.                 $curl_handle = curl_init();
  109.                 curl_setopt($curl_handle, CURLOPT_URL, $this->bl_url);
  110.                 curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
  111.                 curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
  112.                 curl_setopt($curl_handle, CURLOPT_POST,1);
  113.                 curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'body='.$this -> message);
  114.                 curl_setopt($curl_handle, CURLOPT_USERPWD, $this -> user.':'.$this -> pass);
  115.                 $buffer = curl_exec($curl_handle);
  116.                 curl_close($curl_handle);
  117.  
  118.                 if($this -> $buffer)
  119.                 {
  120.                     return TRUE;
  121.                 }
  122.                 else
  123.                 {
  124.                     return $this -> buffer;
  125.                 }
  126.             }
  127.  
  128.             /**
  129.              * Model method
  130.              *
  131.              * Parsing XML, preparing data, for post, updating tmp file
  132.              *
  133.              * @todo Kick tmp file operating lines to other method (needed?)
  134.              */
  135.             public function getList()
  136.             {
  137.                 $lfm_url = 'http://ws.audioscrobbler.com/1.0/user/'.$this -> lfm_user.'/recentlovedtracks.xml';
  138.  
  139.                 include_once "xx_xml.php";
  140.                 $raw = new xx_xml($lfm_url ,'url');
  141.  
  142.                 $last_url = $raw -> data ['recentlovedtracks|track|url']['data'][0] ;
  143.                 $last_artist = $raw -> data ['recentlovedtracks|track|artist']['data'][0] ;
  144.                 $last_track = $raw -> data ['recentlovedtracks|track|name']['data'][0] ;
  145.                 $lat_date = $raw -> data ['recentlovedtracks|track|date']['data'][0] ;
  146.  
  147.                 //Skroc URL
  148.                 $sh_url = 'http://is.gd/api.php?longurl='.$last_url;
  149.                 $short_url = file_get_contents($sh_url);
  150.  
  151.                 //Wiadomosc
  152.                 $this -> message = 'Last.fm: '.$last_artist.' - '.$last_track.' '.$short_url ;
  153.  
  154.                 //Pierwszy raz ?
  155.                 if(file_exists($this -> tmp_fn))
  156.                 {
  157.  
  158.                     $fh = fopen($this -> tmp_fn, 'r') or die("Can't open file");
  159.                     $tmp_data = fread($fh, filesize($this -> tmp_fn));
  160.                     fclose($fh);
  161.  
  162.                     if($tmp_data != $last_url)
  163.                     {
  164.                         $this -> post();
  165.  
  166.                         $fh = fopen($this -> tmp_fn, 'w+') or die("Can't open file");
  167.                         fwrite($fh, $last_url);
  168.                         fclose($fh);
  169.                     }
  170.                     else
  171.                     echo 'Bez zmian';
  172.                 }
  173.                 //Pliku nie ma, pierwsze odpalenie
  174.                 else
  175.                 {
  176.                     $this -> post();
  177.  
  178.                     $fh = fopen($this -> tmp_fn, 'w+') or die("Can't open file");
  179.                     fwrite($fh, $last_url);
  180.                     fclose($fh);
  181.                     echo 'First Run';
  182.                 }
  183.             }
  184.         }
  185. ?>