# -*- coding: utf-8 -*- # http://github.com/sixohsix/twitter/blob/7985672c869426c961ea96565aa0d8695a63513d/twitter/api.py import sys import pprint import twitter.api from PyQt4 import QtCore, QtGui import urllib import os.path import hashlib import KTUtil class Twitter ( QtCore.QThread ): def __init__ ( self, parent=None, source="TwitterThread" ): QtCore.QThread.__init__( self ) self.parent = parent self._mutex = QtCore.QMutex() self._action = None self._argument = None self.twitter = None; self.source = source; self.user_id = -1; def __del__ ( self ): self.action( "quit" ); def action ( self, action, argument=None ): self._mutex.lock(); self._action = action; self._argument = argument; self._mutex.unlock(); def run ( self ): while True: self._mutex.lock(); if self._action: #print "Thread: Starting %s" % ( self._action ); if self._action == "login": try: self.twitter = twitter.api.Twitter( self._argument[0], self._argument[1] ); verified = self.twitter.account.verify_credentials( source=self.source ); self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), True, [ self._action, verified ] ); except twitter.api.TwitterError, e: print e self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), False, [ self._action, e ] ); except Exception, e: print e self.twitter = None; self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), False, [ self._action, e ] ); elif self._action == "logout": self.twitter = None; self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), True, [ self._action ] ); elif self._action == "tweet": try: if 2 <= len( self._argument ): replyTo = self._argument[1]; else: replyTo = -1; status = self.twitter.statuses.update( source=self.source, status=self._argument[0], in_reply_to_status_id=replyTo ); self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), True, [ self._action, status ] ); except twitter.api.TwitterError, e: print e self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), False, [ self._action, e ] ); except Exception, e: print e self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), False, [ self._action, e ] ); elif self._action == "timeline": try: since = self._argument[0]; if since <= 0: since = 1; timeline = self.twitter.statuses.friends_timeline( source=self.source, since_id=since ); self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), True, [ self._action, timeline ] ); except twitter.api.TwitterError, e: print e self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), False, [ self._action, e ] ); except Exception, e: print e self.emit( QtCore.SIGNAL( "done( bool, PyQt_PyObject )" ), False, [ self._action, e ] ); elif action == "quit": self._action = None; self._argument = None; self._mutex.unlock(); self.quit(); break; #print "Thread: Completed %s" % ( self._action ); self._action = None; self._argument = None; self._mutex.unlock(); QtGui.qApp.processEvents(); self.usleep(300); class ImageFetch ( QtCore.QThread ): def __init__ ( self, url, parent=None): QtCore.QThread.__init__( self ); self.url = url; def run ( self ): try: # TODO: This is an iffy split... filename = KTUtil.path( 'cache/' + hashlib.sha256( self.url.split('twimg.com/')[-1] ).hexdigest() ); if False == os.path.isfile( filename ): urllib.urlretrieve( self.url, filename ); except: filename = KTUtil.path( 'resource/missing.png' ); self.emit( QtCore.SIGNAL( "done( PyQt_PyObject )" ), filename );