Source

comofas / comofas_questions.py

# -*- 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.

import urllib
import re
import simplejson
import sys
import logging
import cPickle as pickle
import os
import traceback

from twitter import twitter
from comofas_credentials import *
from comofas_settings import *


class ComofasQuestions(object):
    """
    Esta classe cuida apenas de perguntas
    """
    def __init__(self,twitter_connection=None):
        """
        o twitter_connection pode ser substituido por outra classe
        para executar testes offline.
        """
        self.twitter_connection = twitter_connection
        self.urllib = urllib
        
        # Esta linha não funciona, não descobrimos pq
        # self.twitter_connection.SetXTwitterHeaders('fasassim',"http://twitter.com/fasassim",'Ni!')
        self.search_url = 'http://search.twitter.com/search.json?q=' + KEYWORD

        logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG,)

        if PICKLE_FILENAME in os.listdir('.'):
            self.picklefd = open(PICKLE_FILENAME,"rb")
            try:
                self.persisted_questions = pickle.load(self.picklefd)
            except EOFError:
                self.persisted_questions = []
            finally:
                self.picklefd.close()
        else:
            self.picklefd =open(PICKLE_FILENAME,'wb')
            pickle.dump(self.persisted_questions,self.picklefd)
            self.picklefd.close()
            self.picklefd =open(PICKLE_FILENAME,'wb')
            self.persisted_questions = pickle.load(self.picklefd)
            self.picklefd.close()

    def persist_questions(self):
        self.picklefd =open(PICKLE_FILENAME,"wb")
        pickle.dump(self.persisted_questions,self.picklefd)
        self.picklefd.close()


    def get_questions(self):
        """
        Este metodo retorna todos os resultados, sem filtrar por text e from_user
        """
        feed = self.urllib.urlopen(self.search_url)
        updates = simplejson.loads(feed.read())
        return updates['results'] 

            
    def update_questions(self):
        """
            Este método vai ao twitter UMA vez, traz as perguntas novas e persiste no arquivo.
        """
        
        #traz questions do twitter
        questions = self.get_questions() 
                 
        
        #descobre as não gravadas, ou seja, as novas
        question_ids = []        
        for q in questions:
             question_ids.append(q['id'])
        
        persisted_ids = []
           
        
        for p in self.persisted_questions:
            persisted_ids.append(p['id'])
        
        new_ids = set(question_ids) - set(persisted_ids)
        
        if new_ids:
            #persiste a lista incluindo as novas
            for q in questions:
                if q['id'] in new_ids:    
                    self.persisted_questions.append(q)
                    
                    if DEBUG: print q
            
            self.persist_questions()

class Question(object):
    #in progress, not used yet
    def __init__(self, json):
        self.id = json['id']
        self.from_user = json['from_user']
        self.from_user_id = json['from_user_id']
        self.iso_language_code = json['iso_language_code']
        self.text = json['text']
    


if __name__=='__main__':
    twitter_api = twitter.Api(username=TWITTER_USERNAME, password=TWITTER_PASSWORD)

    comofas = ComofasQuestions(twitter_connection = twitter_api)
    comofas.update_questions()