Report abuse

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
<?
	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 = "";

		// Was soll gesucht werden?
		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;
			}
		}

		// Tweets von Twitter holen
		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);
		}
		
		// HTML-Auswurf generieren
		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;
		}

		// Atom Zeitstempel für Menschen lesbar machen
		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'); }
		}
		
		// Ausdeutschen
		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;
		}

		// Rückgabe
		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;
		}
	}
?>