示例#1
0
文件: client.py 项目: mhameed/acctwit
    def __init__(self): 
        # consumer keys
        twToken = base64.b64decode('cXFWZTh1dHl2MzkyN1JMS3NxMEIyUQ==')
        twSec = base64.b64decode('MWo5NGxUalZ2T3BXWFFndlR0YXVkSkluRE1yVmpmU1lBYWNaOThXNmsySQ==')

        # load our configs
        fname = os.path.expandvars(os.path.expanduser('.acctwitrc'))
        self.conf = ConfigObj(fname)
        logging.debug("just read the folowing prefs:\n%s\n" %pformat(self.conf))

        # check if we have access keys
        oaToken = self.conf.get('oauth_token', '')
        oaSec = self.conf.get('oauth_token_secret', '')
        if not oaToken or not oaSec:
            # first time usage, so get the pin.
            tmpClient = Twython(twitter_token = twToken, twitter_secret = twSec)
            data = tmpClient.get_authentication_tokens()
            print "You need to enter a pin code from twitter to authorize accTwit-cli:"
            print "Please visit this link to obtain the pin: %s" % tmpClient.shortenURL(data['auth_url'][7:])
            pin = raw_input("Enter pin, or type exit? ")
            if pin == 'exit': sys.exit()
            consumer = oauth.Consumer(twToken, twSec)
            request_token = oauth.Token(data['oauth_token'], data['oauth_token_secret'])
            request_token.set_verifier(pin)
            tmpClient.client = oauth.Client(consumer, request_token)
            accessData = tmpClient.get_authorized_tokens()
            if accessData.has_key('screen_name'):
                oaToken = self.conf['oauth_token'] = accessData['oauth_token']
                oaSec = self.conf['oauth_token_secret'] = accessData['oauth_token_secret']
                self._saveConf()
                print "Authenticated successfully."
            else:
                print "Sorry, something went wrong, could not get access tokens."
                sys.exit()
        # connect to twitter
        self.t = Twython( \
        twitter_token = twToken, twitter_secret = twSec, \
        oauth_token = oaToken, oauth_token_secret = oaSec)
        self.tg = TweetGrabber(self.t, self.conf)
        self.tg.start()
        self.localData = []
        self.page = 0
        # connect to our db
        self.dbConn = sqlite3.connect(self.conf['db_name'])
示例#2
0
文件: client.py 项目: mhameed/acctwit
class Client:

    def __init__(self): 
        # consumer keys
        twToken = base64.b64decode('cXFWZTh1dHl2MzkyN1JMS3NxMEIyUQ==')
        twSec = base64.b64decode('MWo5NGxUalZ2T3BXWFFndlR0YXVkSkluRE1yVmpmU1lBYWNaOThXNmsySQ==')

        # load our configs
        fname = os.path.expandvars(os.path.expanduser('.acctwitrc'))
        self.conf = ConfigObj(fname)
        logging.debug("just read the folowing prefs:\n%s\n" %pformat(self.conf))

        # check if we have access keys
        oaToken = self.conf.get('oauth_token', '')
        oaSec = self.conf.get('oauth_token_secret', '')
        if not oaToken or not oaSec:
            # first time usage, so get the pin.
            tmpClient = Twython(twitter_token = twToken, twitter_secret = twSec)
            data = tmpClient.get_authentication_tokens()
            print "You need to enter a pin code from twitter to authorize accTwit-cli:"
            print "Please visit this link to obtain the pin: %s" % tmpClient.shortenURL(data['auth_url'][7:])
            pin = raw_input("Enter pin, or type exit? ")
            if pin == 'exit': sys.exit()
            consumer = oauth.Consumer(twToken, twSec)
            request_token = oauth.Token(data['oauth_token'], data['oauth_token_secret'])
            request_token.set_verifier(pin)
            tmpClient.client = oauth.Client(consumer, request_token)
            accessData = tmpClient.get_authorized_tokens()
            if accessData.has_key('screen_name'):
                oaToken = self.conf['oauth_token'] = accessData['oauth_token']
                oaSec = self.conf['oauth_token_secret'] = accessData['oauth_token_secret']
                self._saveConf()
                print "Authenticated successfully."
            else:
                print "Sorry, something went wrong, could not get access tokens."
                sys.exit()
        # connect to twitter
        self.t = Twython( \
        twitter_token = twToken, twitter_secret = twSec, \
        oauth_token = oaToken, oauth_token_secret = oaSec)
        self.tg = TweetGrabber(self.t, self.conf)
        self.tg.start()
        self.localData = []
        self.page = 0
        # connect to our db
        self.dbConn = sqlite3.connect(self.conf['db_name'])


    def _saveConf(self):
        logging.debug("writing out the folowing prefs:\n%s\n" %pformat(self.conf))
        self.conf.write()

    def quit(self):
        self.tg.stop()
        self.tg.join()
        self._saveConf()

    def post(self, msg, replyTo=None):
        res = None
        if len(msg) > 140:
            return "sorry cant do, message longer than 140."
        if replyTo:
            res = self.t.updateStatus(status=msg, in_reply_to_status_id=replyTo)
        else:
            res = self.t.updateStatus(status=msg)
        try:
            if res.has_key('id'):
                return "status posted."
        except:
            pass
        logging.debug("tried to post, but got res='%s'" % res.__str__())
        return "there was an error posting."

    def rt(self,id):
        res = self.t.reTweet(id=id)
        logging.debug("retweet returned %s" %res.__str__())
        try:
            return res['errors'].splitlines()[0]
        except:
            logging.exception("no errors found when retweeting, so probably everything is ok. res='%s', traceback follows:" %res.__str__())
            return "status retweeted."

    def updateCache(self):
        c = self.dbConn.cursor()
        limit = int(self.conf['tweets_per_page'])
        offset = (limit, limit*(self.page),)
        res = c.execute("select * from tweets order by id desc limit ? offset ?", offset)
        self.localData = res.fetchall()
        c.close()

    def refresh(self):
        self.updateCache()

    def prevPage(self):
        self.page += 1
        logging.debug("prevPage:page=%d" %self.page)
        self.updateCache()

    def nextPage(self):
        if self.page > 0:
            self.page -= 1
        logging.debug("nextPage:page=%d" %self.page)
        self.updateCache()


    def topPage(self):
        self.page = 0
        logging.debug("topPage")
        self.updateCache()