Commits

Anonymous committed 8e81efe

Renomeei arquivos pra evitar confusao

Comments (0)

Files changed (3)

comofas.py

-#! /usr/bin/env python
-# -*- coding: utf-8 -*-
-
-# Comofas - everyone deserves an answer from Yahoo!Answers and Twitter
-# Yahoo!Answers and Twitter are trademarks from Yahoo! Inc and Twitter respectively.
-#
-#Copyright (C) 2008  Pedro Valente
-#
-#This program is free software; you can redistribute it and/or
-#modify it under the terms of the GNU General Public License
-#as published by the Free Software Foundation; either version 2
-#of the License, or (at your option) any later version.
-#
-#This program is distributed in the hope that it will be useful,
-#but WITHOUT ANY WARRANTY; without even the implied warranty of
-#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#GNU General Public License for more details.
-#
-#You should have received a copy of the GNU General Public License
-#along with this program; if not, write to the Free Software
-#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-
-from pynswers.Answers import Answers
-import sys
-import urllib
-import string
-import random
-import re
-import simplejson
-import time
-import cPickle
-from twitter import twitter
-
-twitter_username = 'fasassim'
-twitter_password = 'abc123'
-
-def get_friends_list(api, force = False):
-    try:
-        friends_list = cPickle.load(open('friends.pickle'))
-    except IOError:
-        force = True
-    if force:
-        friends = api.GetFriends()
-        friends_list = [f.screen_name for f in friends]
-        cPickle.dump(friends_list, open('friends.pickle','w'))
-    return friends_list
-
-def add_friend(api, screen_name):
-    try:
-        new_friend = api.CreateFriendship(screen_name)
-        print "twitter.fasassim.following %s" % new_friend.name
-    except:
-        print "twitter.fasassim.following %s - FAIL" % screen_name
-
-def get_full_feed():
-    feed = urllib.urlopen('http://search.twitter.com/search.json?q=comofas')
-    updates = simplejson.loads(feed.read())
-    cPickle.dump(updates, open('updates.pickle','w'))
-    return updates
-
-def get_last_feed():
-    updates = get_full_feed()
-    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 = pergunta.replace('comofas',' ')
-    s=string.find(pergunta,'http://')
-    if s != -1:
-        t = string.find(pergunta[s+7:], " ")
-        pergunta = string.replace(pergunta,pergunta[s:t+s+7],' ')
-
-    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 = re.sub('\n','', pergunta)
-    pergunta = re.sub('\s\s','', pergunta)
-
-    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
-    # Lista de palavras que não ficam bem no fim da frase.
-    dispensaveis = ['e','a','o','de','da','do','na','no','para','pra','mas','se','que','como','porque']
-    a = raw_answer[:last].split()
-    if a:
-        while a[-1] in dispensaveis:
-            a.pop(-1)
-    return "%s%s" % (msg, " ".join(a))
-
-
-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!')
-    
-    friends_list = get_friends_list(api)   
-    print "Sigo %d amigos" % len(friends_list)
-    
-    
-    ultima_pergunta=''
-    cycle_count = 0
-    while (True):
-        intervalo = random.randint(25,40)*2
-        print "Tempo: %d segundos..." % intervalo
-        time.sleep(intervalo)
-        feed = get_full_feed()
-        
-        if cycle_count >= 5:
-            #checa se no feed tem pessoas novas
-            new_people = False
-            for update in feed['results']:
-                if update['from_user'] not in friends_list:
-                    new_people = True
-                    add_friend(api, update['from_user'])
-            if new_people:
-                friends_list = get_friends_list(api, force=True)
-            cycle_count = 0
-        else:
-            cycle_count += 1
-        
-        #TODO Iterar do último item do feed para o primeiro, checando se já foi respondido.
-        #     Se não foi, responde e grava em algum lugar update_id e flag_respondida
-        #     Ignorar updates mais antigos do que 1 ou 2 horas
-        f = get_last_feed()
-        pergunta = f[0]
-        usuario = f[1]
-        follow_counter=0
-
-        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
-
-
-            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)
-
-                    except UnicodeEncodeError:
-                        continue
-            else:
-                #nao_entendi(usuario)
-                pass

comofas2.py

-#! /usr/bin/env python
-# -*- coding: utf-8 -*-
-
-# Comofas - everyone deserves an answer from Yahoo!Answers and Twitter
-# Yahoo!Answers and Twitter are trademarks from Yahoo! Inc and Twitter respectively.
-#
-#Copyright (C) 2008  Pedro Valente
-#
-#This program is free software; you can redistribute it and/or
-#modify it under the terms of the GNU General Public License
-#as published by the Free Software Foundation; either version 2
-#of the License, or (at your option) any later version.
-#
-#This program is distributed in the hope that it will be useful,
-#but WITHOUT ANY WARRANTY; without even the implied warranty of
-#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#GNU General Public License for more details.
-#
-#You should have received a copy of the GNU General Public License
-#along with this program; if not, write to the Free Software
-#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
-
-from pynswers.Answers import Answers
-
-import urllib
-import urllib2
-import string
-import random
-import re
-import simplejson
-import time
-import sys
-import logging
-import pickle
-import os
-import traceback
-
-from twitter import twitter
-from comofas_credentials import *
-from comofas_settings import *
-
-class Comofas(object):
-    """
-    Esta classe pretende cumprir a nobre missao de responder
-    a todos os usuarios
-    """
-    def __init__(self,twitter_connection=None,answers_connection=None):
-        """
-        o twitter_connection pode ser substituido por outra classe
-        para executar testes offline.
-        """
-        self.twitter_connection = twitter_connection
-        self.twitter_connection.SetXTwitterHeaders('fasassim',"http://twitter.com/fasassim",'Ni!')
-        self.answers_connection = answers_connection
-        self.search_url = 'http://search.twitter.com/search.json?q=' + KEYWORD
-        self.last_feeds = dict()
-        self.last_users = dict()
-
-        logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)
-
-        if PICKLE_FILENAME in os.listdir('.'):
-            self.picklefd = open(PICKLE_FILENAME,"rb")
-            try:
-                self.last_feeds = pickle.load(self.picklefd)
-            except EOFError:
-                self.last_feeds = dict()
-            finally:
-                self.picklefd.close()
-        else:
-            self.picklefd =open(PICKLE_FILENAME,'wb')
-            pickle.dump(self.last_feeds,self.picklefd)
-            self.picklefd.close()
-            self.picklefd =open(PICKLE_FILENAME,'wb')
-            self.last_feeds = pickle.load(self.picklefd)
-            self.picklefd.close()
-
-    def persist_last_feeds(self):
-        self.picklefd =open(PICKLE_FILENAME,"wb")
-        pickle.dump(self.last_feeds,self.picklefd)
-        self.picklefd.close()
-
-    def get_last_feed(self):
-        """
-        este metodo retorna o ultimo resultado, com os
-        campos text e from_user apenas
-        """
-        feed = urllib.urlopen(self.search_url)
-        updates = feed.read()
-        updates = simplejson.loads(updates)
-
-        return updates['results'][0]['text'].lower(), updates['results'][0]['from_user']
-
-    def get_feeds(self):
-        """
-        Este metodo retorna todos os resultados, sem filtrar por text e from_user
-        """
-        feed = urllib.urlopen('http://search.twitter.com/search.json?q=' + KEYWORD)
-        updates = feed.read()
-        updates = simplejson.loads(updates)
-
-        return updates['results']
-
-    def clean_text(self,txt):
-        """
-        vou falar o obvio: este metodo limpa o texto
-        """
-        pergunta = re.sub('#\w+',' ', txt)
-        pergunta = re.sub('@\w+',' ', pergunta)
-
-        pergunta = pergunta.replace(KEYWORD,' ')
-        s=string.find(pergunta,'http://')
-        if s != -1:
-            t = string.find(pergunta[s+7:], " ")
-            pergunta = string.replace(pergunta,pergunta[s:t+s+7],' ')
-
-        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 = re.sub('\n','', pergunta)
-        pergunta = re.sub('\s\s','', pergunta)
-
-        pergunta = string.joinfields(pergunta.split(), ' OR ')
-
-        return pergunta
-
-    def nao_entendi(self,usuario):
-        """
-        este metodo pede que o usuario pergunte de outra forma
-        """
-        nao_entendi_lista=[u"Não entendi mt bem, vc poderia me explicar?",u"Vc poderia explicar melhor?", u"Hã? Repete aí, mas fala diferente."]
-        mensagem = self.build_message(to_user=usuario, raw_answer=nao_entendi_lista[random.randint(0,len(nao_entendi_lista)-1)])
-        status = "sem status"
-        try:
-            status = self.twitter_connection.PostUpdate(mensagem)
-        except urllib2.HTTPError:
-            logging.debug("answer exception:" + str(traceback.print_exc()))
-        print '-' * 50
-        print "twitter.fasassim(nao entendi) postUpdate: %s - status:%s" % (mensagem,status)
-
-    def build_message(self,to_user, raw_answer):
-        """
-        este metodo:
-        remove palavras que nao ficam bem no final da frase.
-        limita o tamanho da frase
-        """
-        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
-        # Lista de palavras que não ficam bem no fim da frase.
-        dispensaveis = ['e','a','o','de','da','do','na','no','para','pra','mas','se','que','como','porque']
-        a = raw_answer[:last].split()
-        while a[-1] in dispensaveis:
-            a.pop(-1)
-        return "%s%s" % (msg, " ".join(a))
-
-    def add_new_friends(self):
-        added_friends = list()
-        try:
-            added_friends = self.twitter_connection.GetFriends()
-            for friend in added_friends:
-                if not self.last_users.has_key(friend.name) and friend.name != 'fasassim':
-                    try:
-                        self.twitter_connection.CreateFriendship(friend)
-                        print "twitter.fasassim.following %s" % friend.name
-                        self.last_users.pop(friend.name)
-                    except:
-                        print "cannot create friendship for " + friend.name
-        except urllib2.HTTPError:
-            logging.debug("answer exception:" + str(traceback.print_exc()))
-        finally:
-            self.last_users = dict()
-
-    def answer_tweets(self):
-        """
-        este metodo busca os feeds do twitter, limpa o texto e processa a resposta para cada feed
-        ele nao foi testado e deve ser refatorado p/ dividir as responsabilidades
-
-        twitter keys:
-        [u'iso_language_code',
-     u'text',
-     u'created_at',
-     u'profile_image_url',
-     u'from_user',
-     u'from_user_id',
-     u'to_user_id',
-     u'id']
-
-        twitter user methods:
-        AsDict
-    AsJsonString
-    GetDescription
-    GetId
-    GetLocation
-    GetName
-    GetProfileImageUrl
-    GetScreenName
-    GetStatus
-    GetUrl
-    NewFromJsonDict
-    SetDescription
-    SetId
-    SetLocation
-    SetName
-    SetProfileImageUrl
-    SetScreenName
-    SetStatus
-    SetUrl
-        """
-        try:
-            while (True):
-                
-                time.sleep(random.randint(15,30)*2)
-                feeds = self.get_feeds()
-                for feed in feeds:
-                    if feed.get('id') in self.last_feeds:
-                        continue
-                    else:
-
-                        usuario = feed.get('from_user')
-                        pergunta = feed.get('text')
-                        self.last_users[usuario] = 1
-
-                        pergunta = self.clean_text(pergunta)
-
-                        try:
-                            answers_for_questions = answers.questionSearch({'query':pergunta, 'search_in':'question', 'region': REGION, 'type':'resolved', 'results':'1'})
-                        except:
-                            logging.debug("answer exception:" + str(traceback.print_exc()))
-                            continue
-
-                        #Adição de amigos desativada pro Twitter não achar que o bot é spammer
-                        #if len(self.last_users) == LAST_USERS_SIZE:
-                        #   self.add_new_friends()
-
-                        if answers_for_questions:
-                            for question in answers_for_questions:
-                                try:
-                                    print '-'* 50
-                                    print "Twitter: %s\nPergunta: %s \n\n" % (pergunta, question['Subject'])
-
-                                    mensagem = self.build_message(to_user=usuario, raw_answer=question['ChosenAnswer'])
-                                    meu_timeline =self.twitter_connection.GetUserTimeline(TWITTER_USERNAME)
-
-                                    for msg in meu_timeline:
-                                        if mensagem == msg.text:
-                                            continue
-
-                                    print '-' * 50
-                                    print "twitter.fasassim postUpdate: %s" % mensagem
-
-                                    status = self.twitter_connection.PostUpdate(mensagem)
-                                    self.last_feeds[feed.get('id')] = feed
-                                except (UnicodeEncodeError,urllib2.HTTPError):
-                                    logging.debug("Error:" + str(traceback.print_exc()))
-                                    continue
-                        else:
-                            logging.debug("Nao entendi:" + pergunta)
-                            #self.nao_entendi(usuario)
-        except KeyboardInterrupt:
-            self.persist_last_feeds()
-            sys.exit(1)
-
-
-
-if __name__=='__main__':
-    answers = Answers()
-    answers.appid = answers_appid
-
-    twitter_api = twitter.Api(username=TWITTER_USERNAME, password=TWITTER_PASSWORD)
-    twitter_api.SetXTwitterHeaders('fasassim',"http://twitter.com/fasassim",'Ni!')
-
-    comofas2 = Comofas(twitter_connection = twitter_api,answers_connection = answers)
-    comofas2.answer_tweets()
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Comofas - everyone deserves an answer from Yahoo!Answers and Twitter
+# Yahoo!Answers and Twitter are trademarks from Yahoo! Inc and Twitter respectively.
+#
+#Copyright (C) 2008  Pedro Valente
+#
+#This program is free software; you can redistribute it and/or
+#modify it under the terms of the GNU General Public License
+#as published by the Free Software Foundation; either version 2
+#of the License, or (at your option) any later version.
+#
+#This program is distributed in the hope that it will be useful,
+#but WITHOUT ANY WARRANTY; without even the implied warranty of
+#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#GNU General Public License for more details.
+#
+#You should have received a copy of the GNU General Public License
+#along with this program; if not, write to the Free Software
+#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
+from pynswers.Answers import Answers
+import sys
+import urllib
+import string
+import random
+import re
+import simplejson
+import time
+import cPickle
+from twitter import twitter
+
+twitter_username = 'fasassim'
+twitter_password = 'abc123'
+
+def get_friends_list(api, force = False):
+    try:
+        friends_list = cPickle.load(open('friends.pickle'))
+    except IOError:
+        force = True
+    if force:
+        friends = api.GetFriends()
+        friends_list = [f.screen_name for f in friends]
+        cPickle.dump(friends_list, open('friends.pickle','w'))
+    return friends_list
+
+def add_friend(api, screen_name):
+    try:
+        new_friend = api.CreateFriendship(screen_name)
+        print "twitter.fasassim.following %s" % new_friend.name
+    except:
+        print "twitter.fasassim.following %s - FAIL" % screen_name
+
+def get_full_feed():
+    feed = urllib.urlopen('http://search.twitter.com/search.json?q=comofas')
+    updates = simplejson.loads(feed.read())
+    cPickle.dump(updates, open('updates.pickle','w'))
+    return updates
+
+def get_last_feed():
+    updates = get_full_feed()
+    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 = pergunta.replace('comofas',' ')
+    s=string.find(pergunta,'http://')
+    if s != -1:
+        t = string.find(pergunta[s+7:], " ")
+        pergunta = string.replace(pergunta,pergunta[s:t+s+7],' ')
+
+    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 = re.sub('\n','', pergunta)
+    pergunta = re.sub('\s\s','', pergunta)
+
+    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
+    # Lista de palavras que não ficam bem no fim da frase.
+    dispensaveis = ['e','a','o','de','da','do','na','no','para','pra','mas','se','que','como','porque']
+    a = raw_answer[:last].split()
+    if a:
+        while a[-1] in dispensaveis:
+            a.pop(-1)
+    return "%s%s" % (msg, " ".join(a))
+
+
+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!')
+    
+    friends_list = get_friends_list(api)   
+    print "Sigo %d amigos" % len(friends_list)
+    
+    
+    ultima_pergunta=''
+    cycle_count = 0
+    while (True):
+        intervalo = random.randint(25,40)*2
+        print "Tempo: %d segundos..." % intervalo
+        time.sleep(intervalo)
+        feed = get_full_feed()
+        
+        if cycle_count >= 5:
+            #checa se no feed tem pessoas novas
+            new_people = False
+            for update in feed['results']:
+                if update['from_user'] not in friends_list:
+                    new_people = True
+                    add_friend(api, update['from_user'])
+            if new_people:
+                friends_list = get_friends_list(api, force=True)
+            cycle_count = 0
+        else:
+            cycle_count += 1
+        
+        #TODO Iterar do último item do feed para o primeiro, checando se já foi respondido.
+        #     Se não foi, responde e grava em algum lugar update_id e flag_respondida
+        #     Ignorar updates mais antigos do que 1 ou 2 horas
+        f = get_last_feed()
+        pergunta = f[0]
+        usuario = f[1]
+        follow_counter=0
+
+        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
+
+
+            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)
+
+                    except UnicodeEncodeError:
+                        continue
+            else:
+                #nao_entendi(usuario)
+                pass
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.