Commit 0e92e864ca66e676701d5c0934d86a37420cc993
1 parent
bbff3509
Exists in
master
and in
7 other branches
Inclusão do aplicativo twitter2geo.php para conversão de mensagens no Twitter em arquivos KML
Showing
1 changed file
with
162 additions
and
0 deletions
Show diff stats
@@ -0,0 +1,162 @@ | @@ -0,0 +1,162 @@ | ||
1 | +<?php | ||
2 | +/* | ||
3 | +Title: twitter2geo.php | ||
4 | + | ||
5 | +Converte mensagens registradas no twitter, para um determinado TAG, em um arquivo georreferenciado | ||
6 | + | ||
7 | +As mensagens são consideradas apenas qd possuírem as TAGs #x ou #y, ou então, #lat ou #long | ||
8 | + | ||
9 | +São aceitos todos os parâmetros de busca da API do Twitter, como definidos em http://search.twitter.com/ | ||
10 | + | ||
11 | +Licenca | ||
12 | + | ||
13 | +i3Geo Interface Integrada de Ferramentas de Geoprocessamento para Internet | ||
14 | + | ||
15 | +Direitos Autorais Reservados (c) 2006 Ministério do Meio Ambiente Brasil | ||
16 | +Desenvolvedor: Edmar Moretti edmar.moretti@mma.gov.br | ||
17 | + | ||
18 | +Este programa é software livre; você pode redistribuí-lo | ||
19 | +e/ou modificá-lo sob os termos da Licença Pública Geral | ||
20 | +GNU conforme publicada pela Free Software Foundation; | ||
21 | +tanto a versão 2 da Licença. | ||
22 | +Este programa é distribuído na expectativa de que seja útil, | ||
23 | +porém, SEM NENHUMA GARANTIA; nem mesmo a garantia implícita | ||
24 | +de COMERCIABILIDADE OU ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA. | ||
25 | +Consulte a Licença Pública Geral do GNU para mais detalhes. | ||
26 | +Você deve ter recebido uma cópia da Licença Pública Geral do | ||
27 | +GNU junto com este programa; se não, escreva para a | ||
28 | +Free Software Foundation, Inc., no endereço | ||
29 | +59 Temple Street, Suite 330, Boston, MA 02111-1307 USA. | ||
30 | + | ||
31 | +Arquivo: i3geo/twitter2geo.php | ||
32 | + | ||
33 | +Parametros: | ||
34 | + | ||
35 | +ajuda - se for definida na URL, mostra uma ajuda ao usuário | ||
36 | + | ||
37 | +tipo - tipo de arquivo que será retornado | ||
38 | + | ||
39 | +Valores: | ||
40 | + | ||
41 | + twitter - retona os resultados originais encontrados | ||
42 | + | ||
43 | + kml - retorna os dados no formato kml | ||
44 | + | ||
45 | +Exemplos: | ||
46 | + | ||
47 | +twitter2geo.php?ajuda | ||
48 | + | ||
49 | +twitter2geo.php?tipo=kml&q=twit2kml | ||
50 | +*/ | ||
51 | +include("classesphp/carrega_ext.php"); | ||
52 | +/* | ||
53 | +Código de consulta à API adaptado de http://www.reynoldsftw.com/2009/02/using-jquery-php-ajax-with-the-twitter-api/ | ||
54 | +*/ | ||
55 | +$par = array_merge($_POST,$_GET); | ||
56 | +$chaves = array_keys($par); | ||
57 | + | ||
58 | +if($par["ajuda"] || !$par["tipo"]){ | ||
59 | + echo "<pre>Title: twitter2geo.php | ||
60 | + Converte mensagens registradas no twitter, para um determinado TAG, em um arquivo georreferenciado | ||
61 | + As mensagens são consideradas apenas qd possuírem as TAGs #x ou #y, ou então, #lat ou #long | ||
62 | + São aceitos todos os parâmetros de busca da API do Twitter, como definidos em http://search.twitter.com/ | ||
63 | + | ||
64 | + Parametros: | ||
65 | + | ||
66 | + ajuda - se for definida na URL, mostra uma ajuda ao usuário | ||
67 | + tipo - tipo de arquivo que será retornado | ||
68 | + | ||
69 | + Valores: | ||
70 | + | ||
71 | + twitter - retona os resultados originais encontrados | ||
72 | + kml - retorna os dados no formato kml | ||
73 | + | ||
74 | + Exemplos: | ||
75 | + | ||
76 | + twitter2geo.php?ajuda | ||
77 | + twitter2geo.php?tipo=kml&q=twit2kml"; | ||
78 | + exit; | ||
79 | +} | ||
80 | +$query = array(); | ||
81 | +foreach($chaves as $chave) | ||
82 | +{$query[] = $chave."=".$par[$chave];} | ||
83 | +$query = implode("&",$query); | ||
84 | +$s = PHP_SHLIB_SUFFIX; | ||
85 | +if(!function_exists('curl_init')) | ||
86 | +{@dl( 'php_curl'.'.'.$s );} | ||
87 | +if(!function_exists('curl_init')) | ||
88 | +{echo "curl não instalado";} | ||
89 | +$curl = curl_init(); | ||
90 | +curl_setopt ($curl, CURLOPT_URL, "http://search.twitter.com/search.json?".($query)); | ||
91 | +curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | ||
92 | +$result = curl_exec ($curl); | ||
93 | +curl_close ($curl); | ||
94 | +$result = fixEncoding($result); | ||
95 | +if(strtolower($par["tipo"]) == "twitter"){ | ||
96 | + //echo header("Content-type: application/json"); | ||
97 | + echo $result; | ||
98 | + exit; | ||
99 | +} | ||
100 | +$result = json_decode( $result, true ); | ||
101 | +$kml = '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.0"><Document><name>twitter</name>'; | ||
102 | +$kml .= '<Folder><description>'.$q.'</description><name>'.$q.'</name><open>1</open>'; | ||
103 | +$icon = array(); | ||
104 | +if(!$result["results"]) | ||
105 | +{exit;} | ||
106 | +foreach($result["results"] as $r) | ||
107 | +{ | ||
108 | + if(!in_array($r["from_user"],$icon)) | ||
109 | + { | ||
110 | + $kml .= '<Style id="'.$r["from_user"].'"><IconStyle><Icon>'; | ||
111 | + $kml .= '<href>'.$r["profile_image_url"].'</href>'; | ||
112 | + $kml .= '</Icon></IconStyle></Style>'; | ||
113 | + } | ||
114 | + $icon[] = $r["from_user"]; | ||
115 | +} | ||
116 | +$id = 0; | ||
117 | +foreach($result["results"] as $r) | ||
118 | +{ | ||
119 | + $xy = retornaXY($r["text"]); | ||
120 | + if($xy != "") | ||
121 | + { | ||
122 | + $html = "<table><tr>"; | ||
123 | + $html .= "<td><img src='".$r["profile_image_url"]."' /></td>"; | ||
124 | + $html .= "<td><span style=color:gray >".$r["from_user"]."<br>"; | ||
125 | + $html .= $r["created_at"]."</span><br>"; | ||
126 | + $html .= $r["text"]."<br></td></tr></table>"; | ||
127 | + $kml .='<Placemark><name>'.$id.'</name>'; | ||
128 | + $kml .= '<description><![CDATA['.$html.']]></description>'; | ||
129 | + $kml .= '<styleUrl>'.$r["from_user"].'</styleUrl>'; | ||
130 | + $kml .= '<Point id="'.$id.'"><coordinates>'.$xy.'</coordinates></Point></Placemark>'; | ||
131 | + $id++; | ||
132 | + } | ||
133 | +} | ||
134 | +$kml .= '</Folder></Document></kml>'; | ||
135 | +echo header("Content-type: application/xml"); | ||
136 | +echo $kml; | ||
137 | +function retornaXY($s) | ||
138 | +{ | ||
139 | + $x = preg_replace("/(.*#x)|(\s.*)/i","",$s); | ||
140 | + if($x == "") | ||
141 | + {$x = preg_replace("/(.*#long)|(\s.*)/i","",$s);} | ||
142 | + if($x == "") | ||
143 | + {$x = preg_replace("/(.*#lon)|(\s.*)/i","",$s);} | ||
144 | + $y = preg_replace("/(.*#y)|(\s.*)/i","",$s); | ||
145 | + if($y == "") | ||
146 | + {$y = preg_replace("/(.*#lat)|(\s.*)/i","",$s);} | ||
147 | + $x = str_replace(",",".",$x); | ||
148 | + $y = str_replace(",",".",$y); | ||
149 | + if(!is_numeric($x) || !is_numeric($y)) | ||
150 | + {return "";} | ||
151 | + else | ||
152 | + {return $x.",".$y;} | ||
153 | +} | ||
154 | +function fixEncoding($in_str) | ||
155 | +{ | ||
156 | + $cur_encoding = mb_detect_encoding($in_str) ; | ||
157 | + if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8")) | ||
158 | + {return $in_str;} | ||
159 | + else | ||
160 | + {return utf8_encode($in_str);} | ||
161 | +} | ||
162 | +?> | ||
0 | \ No newline at end of file | 163 | \ No newline at end of file |