Blitter / trunk / blitter / class.blitter.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
<?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 $xml_file_name = 'class.xml.php';
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()
{
$this -> service = 'post'.$this -> service;
if(is_callable($this -> service, true, $callablename)) { call_user_func(array(get_class($this), $this -> service)); }
}
/**
* Method send 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 send 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);
$this -> 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($this -> xml_file_name);
$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;
$short_url = file_get_contents($sh_url);
//Wiadomosc
$this -> message = 'Last.fm: '.$last_artist.' - '.$last_track.' '.$short_url ;
//Pierwszy raz ?
if(file_exists($this -> tmp_fn))
{
$fh = fopen($this -> tmp_fn, 'r') or die("Can't open file");
$tmp_data = fread($fh, filesize($this -> tmp_fn));
fclose($fh);
if($tmp_data != $last_url)
{
$this -> post();
$fh = fopen($this -> tmp_fn, 'w+') or die("Can't open file");
fwrite($fh, $last_url);
fclose($fh);
}
else
echo 'Bez zmian';
}
//Pliku nie ma, pierwsze odpalenie
else
{
$this -> post();
$fh = fopen($this -> tmp_fn, 'w+') or die("Can't open file");
fwrite($fh, $last_url);
fclose($fh);
echo 'First Run';
}
}
}
?>
|
History for Blitter/trunk/blitter/class.blitter.php
| Revision | Author | Commited | Message |
|---|---|---|---|
5
Diff
|
|
Wed 18 Nov, 2009 14:50:57 +0000 | class.blitter.php edited. |
4
Diff
|
|
Tue 17 Nov, 2009 19:14:07 +0000 | |
| 2 |
|
Tue 17 Nov, 2009 18:34:43 +0000 | Blitter package upload. |
Diff
View complete history
Commits for Blitter:/trunk/blitter/class.blitter.php