<?
function showRoboTweets($q, $limit=5) {
if($q) {
$twitter = new rbc_latesttweets();
$twitter->getTweets($q, $limit);
echo $twitter->echoTweets();
}
else echo "<ul class=\"latesttweets\"><li class=\"error\">Kein Suchbegriff angegeben</li></ul>\n";
}
class rbc_latesttweets {
private $tweets = "";
private $type = "";
private $q = "";
public function getQueryType($q) {
if($q[0]=="#") {
$this->q = "%23".substr($q,1);
$this->type = 1;
}
elseif($q[0]=="@") {
$this->q = "to%3A".substr($q,1);
$this->type = 2;
}
elseif($q[0]=="%" && $q[1]=="@") {
$this->q = "%40".substr($q,2);
$this->type = 3;
}
else {
$this->q = "from:".$q;
$this->type = 4;
}
}
public function getTweets($q, $limit) {
$this->getQueryType($q);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.atom?q=".$this->q."&rpp=".$limit);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$res = curl_exec($ch);
if(!$res) $this->tweets = "\n\t<ul class=\"latesttweets\"><li class=\"error\">Verbindung fehlgeschlagen</li></ul>\n";
else $this->formatTweets($res);
curl_close($ch);
}
public function formatTweets($xmlstring) {
$xml = simplexml_load_string($xmlstring);
$output = "\n\t<ul class=\"latesttweets\">\n";
if(sizeOf($xml->entry)>0){
foreach($xml->entry as $tweet) {
$output .= "\t\t<li>";
$output .= $tweet->content."<span>".$this->formatTimestamp($tweet->updated);
if($this->type!=4) $output .= " von <a href=\"".$tweet->author->uri."\" title=\"".$tweet->author->name."\">".$tweet->author->name."</a>";
$output .= "</span>";
$output .= "</li>\n";
}
}
else $output .= "\t\t<li class=\"notice\">Keine Tweets gefunden</li>\n";
$output .= "\t</ul>\n";
$this->tweets = $output;
}
public function formatTimestamp($timestamp) {
$unix_timestamp = strtotime($timestamp);
$seconds = time() - $unix_timestamp;
$minutes = 0; $hours = 0; $days = 0; $weeks = 0; $months = 0; $years = 0;
if ($seconds == 0) $seconds = 1;
if ($seconds> 60) { $minutes = $seconds/60; }
else { return $this->germanize_time_since($seconds,'Sekunde'); }
if ( $minutes >= 60 ) { $hours = $minutes/60; }
else { return $this->germanize_time_since($minutes,'Minute'); }
if ( $hours >= 24) { $days = $hours/24; }
else { return $this->germanize_time_since($hours,'Stunde'); }
if ( $days >= 7 ) { $weeks = $days/7; }
else { return $this->germanize_time_since($days,'Tag'); }
if ( $weeks >= 4 ) { $months = $weeks/4; }
else { return $this->germanize_time_since($weeks,'Woche'); }
if ( $months>= 12 ) { $years = $months/12; return $this->germanize_time_since($years,'Jahr'); }
else { return $this->germanize_time_since($months,'Monat'); }
}
public function germanize_time_since($num,$word) {
$num = floor($num);
$addon = "n";
if($word=="Tag" || $word=="Monat") $addon = "en";
if ($num == 1) return "Vor ".$num." ".$word;
else return "Vor ".$num." ".$word.$addon;
}
public function echoTweets() {
if($this->tweets=="") return "\n\t<ul class=\"latesttweets\"><li class=\"error\">Upppss.. irgendwas ist total in die Hose</li></ul>\n";
else return $this->tweets;
}
}
?>