cypherq
By: a guest | Sep 17th, 2009 | Syntax:
PHP | Size: 5.90 KB | Hits: 304 | Expires: Never
<?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_setopt($curl_handle, CURLOPT_URL
, $this -> tw_url);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS
, 'status='.$this -> message);
curl_setopt($curl_handle, CURLOPT_USERPWD
, $this -> user.':'.$this -> pass);
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_setopt($curl_handle, CURLOPT_POSTFIELDS
, 'body='.$this -> message);
curl_setopt($curl_handle, CURLOPT_USERPWD
, $this -> user.':'.$this -> pass);
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 ?
{
$fh = fopen($this -> tmp_fn, 'r') or
die("Can't open file");
if($tmp_data != $last_url)
{
$this -> post();
$fh = fopen($this -> tmp_fn, 'w+') or
die("Can't open file");
}
else
echo 'Bez zmian';
}
//Pliku nie ma, pierwsze odpalenie
else
{
$this -> post();
$fh = fopen($this -> tmp_fn, 'w+') or
die("Can't open file");
echo 'First Run';
}
}
}
?>