Remove these advertisements by upgrading your account for only $5/month
001: <?php 002: 003: /** 004: * Last.fm to Twitter/Blip class 005: * 006: * Display your favorite songs from Last.fm list on Twitter of Blip. 007: * 008: * @author Harv - http://harv.pl adapted to OOP by cypherq - http://cypherq.wordpress.com 009: * @license Beerware - http://pl.wikipedia.org/wiki/Beerware 010: * @version 1.0 011: * @example /example.php Simple example 012: */ 013: class Blitter 014: { 015: 016: /** 017: * User, password. For Twitter and Blip both 018: */ 019: private $user; 020: private $pass; 021: 022: /** 023: * Set service to use (Capital letter at first) 024: * Ex. private $service = 'Blip'; 025: */ 026: private $service; 027: 028: /** 029: * Last.fm user name 030: */ 031: private $lfm_user; 032: 033: /** 034: * Various support variables 035: */ 036: private $tmp_fn = 'lsfm_tw'; 037: private $xml_file_name = 'class.xml.php'; 038: private $buffer; 039: private $message; 040: 041: /** 042: * API's, URL's 043: */ 044: private $tw_url = 'http://twitter.com/statuses/update.xml'; 045: private $bl_url = 'http://api.blip.pl/updates'; 046: 047: /** 048: * Constructor - only for assign variables passed by params 049: * 050: * @param string $user Blip/Twitter user name 051: * @param string $pass Blip/Twitter password 052: * @param string $lfm_user Last.fm username 053: * @param string $service Service name (where we want post) 054: */ 055: public function __construct($user, $pass, $lfm_user, $service) 056: { 057: $this -> user = $user; 058: $this -> pass = $pass; 059: $this -> lfm_user = $lfm_user; 060: $this -> service = $service; 061: } 062: 063: public function post() 064: { 065: $this -> service = 'post'.$this -> service; 066: if(is_callable($this -> service, true, $callablename)) { call_user_func(array(get_class($this), $this -> service)); } 067: } 068: 069: /** 070: * Method send message to Twitter 071: * 072: * @param string $message Prepared message to send 073: */ 074: public function postTwitter() 075: { 076: $curl_handle = curl_init(); 077: curl_setopt($curl_handle, CURLOPT_URL, $this -> tw_url); 078: curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2); 079: curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); 080: curl_setopt($curl_handle, CURLOPT_POST,1); 081: curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'status='.$this -> message); 082: curl_setopt($curl_handle, CURLOPT_USERPWD, $this -> user.':'.$this -> pass); 083: $this -> buffer = curl_exec($curl_handle); 084: curl_close($curl_handle); 085: 086: if ($this -> buffer) 087: { 088: return TRUE; 089: } 090: else 091: { 092: return $this -> buffer; 093: } 094: } 095: 096: /** 097: * Method send message to Blip 098: * 099: * @param string $message Prepared message to send 100: */ 101: public function postBlip() 102: { 103: $curl_handle = curl_init(); 104: curl_setopt($curl_handle, CURLOPT_URL, $this -> bl_url); 105: curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2); 106: curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); 107: curl_setopt($curl_handle, CURLOPT_POST,1); 108: curl_setopt($curl_handle, CURLOPT_POSTFIELDS, 'body='.$this -> message); 109: curl_setopt($curl_handle, CURLOPT_USERPWD, $this -> user.':'.$this -> pass); 110: $this -> buffer = curl_exec($curl_handle); 111: curl_close($curl_handle); 112: 113: if($this -> buffer) 114: { 115: return TRUE; 116: } 117: else 118: { 119: return $this -> buffer; 120: } 121: } 122: 123: /** 124: * Model method 125: * 126: * Parsing XML, preparing data, for post, updating tmp file 127: * 128: * @todo Kick tmp file operating lines to other method (needed?) 129: */ 130: public function getList() 131: { 132: $lfm_url = 'http://ws.audioscrobbler.com/1.0/user/'.$this -> lfm_user.'/recentlovedtracks.xml'; 133: 134: include_once($this -> xml_file_name); 135: $raw = new xx_xml($lfm_url ,'url'); 136: 137: $last_url = $raw -> data ['recentlovedtracks|track|url']['data'][0] ; 138: $last_artist = $raw -> data ['recentlovedtracks|track|artist']['data'][0] ; 139: $last_track = $raw -> data ['recentlovedtracks|track|name']['data'][0] ; 140: $lat_date = $raw -> data ['recentlovedtracks|track|date']['data'][0] ; 141: 142: //Skroc URL 143: $sh_url = 'http://is.gd/api.php?longurl='.$last_url; 144: $short_url = file_get_contents($sh_url); 145: 146: //Wiadomosc 147: $this -> message = 'Last.fm: '.$last_artist.' - '.$last_track.' '.$short_url ; 148: 149: //Pierwszy raz ? 150: if(file_exists($this -> tmp_fn)) 151: { 152: 153: $fh = fopen($this -> tmp_fn, 'r') or die("Can't open file"); 154: $tmp_data = fread($fh, filesize($this -> tmp_fn)); 155: fclose($fh); 156: 157: if($tmp_data != $last_url) 158: { 159: $this -> post(); 160: 161: $fh = fopen($this -> tmp_fn, 'w+') or die("Can't open file"); 162: fwrite($fh, $last_url); 163: fclose($fh); 164: } 165: else 166: echo 'Bez zmian'; 167: } 168: //Pliku nie ma, pierwsze odpalenie 169: else 170: { 171: $this -> post(); 172: 173: $fh = fopen($this -> tmp_fn, 'w+') or die("Can't open file"); 174: fwrite($fh, $last_url); 175: fclose($fh); 176: echo 'First Run'; 177: } 178: } 179: } 180: ?>
| Revision | Author | Commited | Message | |
|---|---|---|---|---|
| 5 |
|
Wed 18 Nov, 2009 14:50:57 +0000 | class.blitter.php edited. |
Diff
|
| 4 |
|
Tue 17 Nov, 2009 19:14:07 +0000 |
Diff
|
|
| 2 |
|
Tue 17 Nov, 2009 18:34:43 +0000 | Blitter package upload. |
Remove these advertisements by upgrading your account for only $5/month