Posted by cypherq on Thu 17 Sep 14:18 (modification of post by view diff)
report abuse | download | new post
- <?php
- /**
- * Last.fm to Twitter/Blip class
- *
- * Display your favorite songs from Last.fm list on Twitter of Blip.
- *
- * @author Harv - http://harv.pl adapted to OOP by cypherq - http://cypherq.wordpress.com
- * @license Beerware - http://pl.wikipedia.org/wiki/Beerware
- * @version 1.0
- * @example /example.php Simple example
- */
- class Blitter
- {
- /**
- * User, password. For Twitter and Blip both
- */
- private $user;
- private $pass;
- /**
- * Set service to use (Capital letter at first)
- * Ex. private $service = 'Blip';
- */
- private $service;
- /**
- * Last.fm user name
- */
- private $lfm_user;
- /**
- * Various support variables
- */
- private $tmp_fn = "lsfm_tw";
- private $buffer;
- private $message;
- /**
- * API's, URL's
- */
- private $tw_url = 'http://twitter.com/statuses/update.xml';
- private $bl_url = 'http://api.blip.pl/updates';
- /**
- * Constructor - only for assign variables passed by params
- *
- * @param string $user Blip/Twitter user name
- * @param string $pass Blip/Twitter password
- * @param string $lfm_user Last.fm username
- * @param string $service Service name (where we want post)
- */
- public function __construct($user, $pass, $lfm_user, $service)
- {
- $this -> user = $user;
- $this -> pass = $pass;
- $this -> lfm_user = $lfm_user;
- $this -> service = $service;
- }
- public function post()
- {
- switch($this -> service)
- {
- case 'Blip': $this -> postBlip();
- break;
- case 'Twitter': $this -> postTwitter();
- break;
- }
- }
- /**
- * Method sending message to Twitter
- *
- * @param string $message Prepared message to send
- */
- public function postTwitter()
- {
- $curl_handle = curl_init();
- curl_setopt($curl_handle, CURLOPT_URL, $this -> tw_url);
- curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
- curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl_handle, CURLOPT_POST,1);
- curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'status='.$this -> message);
- curl_setopt($curl_handle, CURLOPT_USERPWD, $this -> user.':'.$this -> pass);
- $this -> buffer = curl_exec($curl_handle);
- curl_close($curl_handle);
- if ($this -> buffer)
- {
- return TRUE;
- }
- else
- {
- return $this -> buffer;
- }
- }
- /**
- * Method sending message to Blip
- *
- * @param string $message Prepared message to send
- */
- public function postBlip()
- {
- $curl_handle = curl_init();
- curl_setopt($curl_handle, CURLOPT_URL, $this->bl_url);
- curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
- curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
- curl_setopt($curl_handle, CURLOPT_POST,1);
- curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'body='.$this -> message);
- curl_setopt($curl_handle, CURLOPT_USERPWD, $this -> user.':'.$this -> pass);
- $buffer = curl_exec($curl_handle);
- curl_close($curl_handle);
- if($this -> $buffer)
- {
- return TRUE;
- }
- else
- {
- return $this -> buffer;
- }
- }
- /**
- * Model method
- *
- * Parsing XML, preparing data, for post, updating tmp file
- *
- * @todo Kick tmp file operating lines to other method (needed?)
- */
- public function getList()
- {
- $lfm_url = 'http://ws.audioscrobbler.com/1.0/user/'.$this -> lfm_user.'/recentlovedtracks.xml';
- include_once "xx_xml.php";
- $raw = new xx_xml($lfm_url ,'url');
- $last_url = $raw -> data ['recentlovedtracks|track|url']['data'][0] ;
- $last_artist = $raw -> data ['recentlovedtracks|track|artist']['data'][0] ;
- $last_track = $raw -> data ['recentlovedtracks|track|name']['data'][0] ;
- $lat_date = $raw -> data ['recentlovedtracks|track|date']['data'][0] ;
- //Skroc URL
- $sh_url = 'http://is.gd/api.php?longurl='.$last_url;
- //Wiadomosc
- $this -> message = 'Last.fm: '.$last_artist.' - '.$last_track.' '.$short_url ;
- //Pierwszy raz ?
- {
- if($tmp_data != $last_url)
- {
- $this -> post();
- }
- else
- echo 'Bez zmian';
- }
- //Pliku nie ma, pierwsze odpalenie
- else
- {
- $this -> post();
- echo 'First Run';
- }
- }
- }
- ?>
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.