Commits

pval...@lookindustry  committed 03e5398

commit inicial mudando nome pra comofas

  • Participants

Comments (0)

Files changed (1)

+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from pynswers.Answers import Answers
+import sys
+import urllib
+import string
+import random
+import re
+import simplejson
+import time
+from twitter import twitter
+
+twitter_username = 'fasassim'
+twitter_password = 'abc123'
+
+def get_last_feed():
+    feed = urllib.urlopen('http://search.twitter.com/search.json?q=comofas')
+    updates = feed.read()
+    updates = simplejson.loads(updates)
+
+    return updates['results'][0]['text'].lower(), updates['results'][0]['from_user']
+
+def clean_text(txt):
+    pergunta = re.sub('#\w+','', txt)
+    pergunta = re.sub('@\w+','', pergunta)
+    pergunta = re.sub('\n','', pergunta)
+    pergunta = re.sub('\s\s','', pergunta)
+    s=string.find(pergunta,'http://')
+    if s != -1:
+        t = string.find(pergunta[s+7:], " ")
+        pergunta = string.replace(pergunta,pergunta[s:t+s+7],' ')
+    pergunta = pergunta.replace('comofas','')
+
+    replacements = [(u'é','e'),(u'á','a'),(u'í','i'),(u'ó','o'),(u'ú','u'),(u'â','a'),(u'ê','e'),(u'ô','o'),(u'ã','a'),(u'õ','o'),(u'ç','c')]
+
+    for a, b in replacements:
+        pergunta = pergunta.replace(a, b)
+    for punct in string.punctuation:
+        pergunta = pergunta.replace(punct, '')
+
+    pergunta = string.joinfields(pergunta.split(), ' OR ')
+
+    return pergunta
+
+def nao_entendi(usuario):
+    nao_entendi=[u"Não entendi mt bem, vc poderia me explicar?",u"Vc poderia explicar melhor?", u"Hã? Repete aí, mas fala diferente."]
+    mensagem = build_message(to_user=usuario, raw_answer=nao_entendi[random.randint(0,len(nao_entendi)-1)])
+    status = api.PostUpdate(mensagem)
+    print '-' * 50
+    print "twitter.fasassim(nao entendi) postUpdate: %s" % mensagem
+
+def build_message(to_user, raw_answer):
+    chars = 140
+    msg = "@%s " % to_user
+    chars = chars - len(msg)
+    if len(raw_answer) < chars:
+        return "%s%s" % (msg, raw_answer)
+    last = 0
+    for indice, c in enumerate(raw_answer):
+        if indice > chars:
+            break
+        if c == " ":
+            last = indice
+    return "%s%s" % (msg, raw_answer[:last])
+
+
+if __name__=='__main__':
+    app = Answers()
+    app.appid = 'GwZLVv32'
+
+    api = twitter.Api(username=twitter_username, password=twitter_password)
+    api.SetXTwitterHeaders('fasassim',"http://twitter.com/fasassim",'Ni!')
+
+    ultimos_usuarios={}
+    ultima_pergunta=''
+    while (True):
+        time.sleep(random.randint(15,30)*2)
+        f = get_last_feed()
+        pergunta = f[0]
+        usuario = f[1]
+        follow_counter=0
+
+        ultimos_usuarios[usuario]=1
+
+        if ultima_pergunta == pergunta:
+            continue
+        else:
+            ultima_pergunta = pergunta
+            pergunta = clean_text(pergunta)
+
+            try:
+                questions = app.questionSearch({'query':pergunta, 'search_in':'question', 'region': 'br', 'type':'resolved', 'results':'1'})
+            except:
+                #nao_entendi(usuario)
+                continue
+
+            follow_counter = follow_counter + 1
+
+            if follow_counter==5:
+                follow_counter = 0
+                added_friends = api.GetFriends()
+
+                for friend in added_friends:
+                    if not ultimos_usuarios.has_key(friend.name) and usuario != 'fasassim':
+                        api.CreateFriendship(friend)
+                        print "twitter.fasassim.following %s" % friend.name
+                        ultimos_usuarios.pop(friend.name)
+                ultimos_usuarios={}
+
+            if questions:
+                for question in questions:
+                    try:
+                        print '-'* 50
+                        print "Twitter: %s\nPergunta: %s \n\n" % (pergunta, question['Subject'])
+                        mensagem = build_message(to_user=usuario, raw_answer=question['ChosenAnswer'])
+                        meu_timeline = api.GetUserTimeline(twitter_username)
+                        if mensagem == meu_timeline[0].text:
+                            ultima_pergunta = pergunta
+                            continue
+
+                        print '-' * 50
+                        print "twitter.fasassim postUpdate: %s" % mensagem
+                        status = api.PostUpdate(mensagem)
+                        ultimos_usuarios[usuario]=1
+                    except UnicodeEncodeError:
+                        continue
+            else:
+                #nao_entendi(usuario)
+                pass