<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>coding &#187; twitter</title>
	<atom:link href="http://www.coding.com.br/tag/twitter/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.coding.com.br</link>
	<description>have you coded today?</description>
	<lastBuildDate>Thu, 18 Aug 2011 17:29:43 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Twitter @hashunifei</title>
		<link>http://www.coding.com.br/programacao/twitter-hashunifei/</link>
		<comments>http://www.coding.com.br/programacao/twitter-hashunifei/#comments</comments>
		<pubDate>Sat, 13 Feb 2010 04:37:12 +0000</pubDate>
		<dc:creator>Tiago Maluta</dc:creator>
				<category><![CDATA[programação]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[unifei]]></category>

		<guid isPermaLink="false">http://www.coding.com.br/?p=570</guid>
		<description><![CDATA[
Sempre tive curiosidade de fazer um agregador para o Twitter, ou seja, criar um usuário que fizesse o retwitt de todos os termos que aparecem sobre determinada palavra. Para fazer um teste resolvi criar um usuário chamado @hashunifei que irá agregar o que pessoal escrever sobre a UNIFEI (Universidade Federal de Itajubá) incluindo o nome antigo [...]]]></description>
			<content:encoded><![CDATA[<p align="justify">
Sempre tive curiosidade de fazer um agregador para o Twitter, ou seja, criar um usuário que fizesse o <em>retwitt</em> de todos os termos que aparecem sobre determinada palavra. Para fazer um teste resolvi criar um usuário chamado <a href="http://www.twitter.com/hashunifei">@hashunifei</a> que irá agregar o que pessoal escrever sobre a <a href="http://www.unifei.edu.br">UNIFEI</a> (Universidade Federal de Itajubá) incluindo o nome antigo <img src='http://www.coding.com.br/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Seguindo a filosofia <em>code less, create more</em> decidi fazer um <em>script</em> em Python que acessa a <a href="http://apiwiki.twitter.com/">API</a> do Twitter. Depois só configurar algum agendador de tarefas (ex.: <a href="http://en.wikipedia.org/wiki/Cron">cron</a>) para executar o programa de tempos em tempos. Para evitar que a cada busca os mesmos <em>twitts</em> sejam publicados há um arquivo chamado <em>.hashunifei</em> que grava o número de identificação (id) das mensagens publicadas.
</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #808080; font-style: italic;"># Tiago Maluta &lt;maluta@unifei.edu.br&gt; </span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> simplejson, <span style="color: #dc143c;">urllib</span>
<span style="color: #ff7700;font-weight:bold;">import</span> twitter 
&nbsp;
list_id = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
api = twitter.<span style="color: black;">Api</span><span style="color: black;">&#40;</span>username=<span style="color: #483d8b;">'USUARIO'</span>, password=<span style="color: #483d8b;">'SENHA'</span><span style="color: black;">&#41;</span>
&nbsp;
SEARCH_BASE = <span style="color: #483d8b;">'http://search.twitter.com/'</span>
url = SEARCH_BASE + <span style="color: #483d8b;">&quot;search.json?q=UNIFEI+OR+efei&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> publish<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>,text<span style="color: black;">&#41;</span>:
	twitt = <span style="color: #483d8b;">&quot;RT @&quot;</span>+<span style="color: #dc143c;">user</span>+<span style="color: #483d8b;">&quot; &quot;</span>+text
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>twitt<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">140</span>:
		twitt = twitt<span style="color: black;">&#91;</span>:<span style="color: #ff4500;">140</span><span style="color: black;">&#93;</span>
	api.<span style="color: black;">PostUpdates</span><span style="color: black;">&#40;</span>twitt<span style="color: black;">&#41;</span>
&nbsp;
result = simplejson.<span style="color: black;">load</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">urllib</span>.<span style="color: black;">urlopen</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'results'</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">with</span> <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.hashunifei&quot;</span>,<span style="color: #483d8b;">&quot;r+&quot;</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">as</span> f:
    data = f.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    list_id = data.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;,&quot;</span><span style="color: black;">&#41;</span> 
&nbsp;
f = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.hashunifei&quot;</span>,<span style="color: #483d8b;">&quot;a&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> twitt <span style="color: #ff7700;font-weight:bold;">in</span> result:
&nbsp;
	text = twitt<span style="color: black;">&#91;</span><span style="color: #483d8b;">'text'</span><span style="color: black;">&#93;</span>
	<span style="color: #dc143c;">user</span> = twitt<span style="color: black;">&#91;</span><span style="color: #483d8b;">'from_user'</span><span style="color: black;">&#93;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">user</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;hashunifei&quot;</span>:
		<span style="color: #008000;">id</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>twitt<span style="color: black;">&#91;</span><span style="color: #483d8b;">'id'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">id</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #ff7700;font-weight:bold;">in</span> list_id:	
			list_id.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span><span style="color: black;">&#41;</span>
			f.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #008000;">id</span>+<span style="color: #483d8b;">','</span><span style="color: black;">&#41;</span>
			publish<span style="color: black;">&#40;</span><span style="color: #dc143c;">user</span>,text<span style="color: black;">&#41;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Se quiser testar o código-fonte (lembre-se de mudar o usuário e senha e a palavra da busca)</p>
<blockquote><p>
$ touch .hashunifei<br />
$ wget <a href="http://github.com/maluta/junk/raw/master/hashunifei.py">http://github.com/maluta/junk/raw/master/hashunifei.py</a>
</p></blockquote>
<p>Limitações: A mensagem é truncada em 140 caracteres (limite do twitter). </p>
<p>Eu sinceramente não sei se é assim que o pessoal faz, foi a primeira idéia que tive&#8230;, se o pessoal que entende de <em>web</em> quiser colaborar seria ótimo.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.coding.com.br/programacao/twitter-hashunifei/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
