def __handle_oauth(self, callback): if self.has_oauth_support(): self.client = TurpialAuthClient() else: self.client = TurpialAuthClient(api_url=self.apiurl) self.consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET) self.sign_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1() self.access_url = 'https://api.twitter.com/oauth/access_token' try: request = oauth.OAuthRequest.from_consumer_and_token( oauth_consumer=self.consumer, http_method='POST', http_url=self.access_url, parameters={ 'x_auth_mode': 'client_auth', 'x_auth_username': self.username, 'x_auth_password': self.password }) request.sign_request(self.sign_method_hmac_sha1, self.consumer, None) req = urllib2.Request(self.access_url, data=request.to_postdata()) response = urllib2.urlopen(req) self.token = oauth.OAuthToken.from_string(response.read()) callback({'auth': True}) except Exception, error: print "Error: %s\n%s" % (error, traceback.print_exc()) callback({'auth': False, 'error': 'Invalid Credentials'})
def __handle_oauth(self, args, callback): if args['cmd'] == 'start': self.client = TurpialAuthClient() self.consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET) self.signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1() auth = args['auth'] if auth['oauth-key'] != '' and auth['oauth-secret'] != '' and auth['oauth-verifier'] != '': self.token = oauth.OAuthToken(auth['oauth-key'], auth['oauth-secret']) self.token.set_verifier(auth['oauth-verifier']) self.is_oauth = True args['done'](self.token.key, self.token.secret, self.token.verifier) else: self.log.debug('Obtain a request token') oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url=self.client.request_token_url) oauth_request.sign_request(self.signature_method_hmac_sha1, self.consumer, None) self.log.debug('REQUEST (via headers)') self.log.debug('parameters: %s' % str(oauth_request.parameters)) try: self.token = self.client.fetch_request_token(oauth_request) except Exception, e: print "Error: %s\n%s" % (e, traceback.print_exc()) raise Exception self.log.debug('GOT') self.log.debug('key: %s' % str(self.token.key)) self.log.debug('secret: %s' % str(self.token.secret)) self.log.debug('callback confirmed? %s' % str(self.token.callback_confirmed)) self.log.debug('Authorize the request token') oauth_request = oauth.OAuthRequest.from_token_and_callback(token=self.token, http_url=self.client.authorization_url) self.log.debug('REQUEST (via url query string)') self.log.debug('parameters: %s' % str(oauth_request.parameters)) callback(oauth_request.to_url())
class TurpialAPI(threading.Thread): '''API basica de turpial basada en hilos''' def __init__(self): threading.Thread.__init__(self) self.setDaemon(False) self.log = logging.getLogger('API') self.queue = Queue.Queue() self.exit = False # OAuth stuffs self.client = None self.consumer = None self.is_oauth = False self.token = None self.signature_method_hmac_sha1 = None self.format = 'json' self.username = None self.password = None self.profile = None self.tweets = [] self.replies = [] self.directs = [] self.favorites = [] self.muted_users = [] self.friends = [] self.friendsloaded = False self.conversation = [] self.apiurl = 'http://api.twitter.com/1' self.to_fav = [] self.to_unfav = [] self.to_del = [] self.log.debug('Iniciado') def __register(self, args, callback): self.queue.put((args, callback)) def __del_tweet_from(self, tweets, id): item = None for twt in tweets: if id == twt['id']: item = twt break if item: tweets.remove(item) return tweets def __change_tweet_from(self, tweets, id, key, value): index = None for twt in tweets: if id == twt['id']: index = tweets.index(twt) break if index: tweets[index][key] = value return tweets def __handle_oauth(self, args, callback): if args['cmd'] == 'start': if self.has_oauth_support(): self.client = TurpialAuthClient() else: self.client = TurpialAuthClient(api_url=self.apiurl) self.consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET) self.signature_method_hmac_sha1 = oauth.OAuthSignatureMethod_HMAC_SHA1() auth = args['auth'] if auth['oauth-key'] != '' and auth['oauth-secret'] != '' and \ auth['oauth-verifier'] != '': self.token = oauth.OAuthToken(auth['oauth-key'], auth['oauth-secret']) self.token.set_verifier(auth['oauth-verifier']) self.is_oauth = True args['done'](self.token.key, self.token.secret, self.token.verifier) else: self.log.debug('Obtain a request token') oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url=self.client.request_token_url) oauth_request.sign_request(self.signature_method_hmac_sha1, self.consumer, None) self.log.debug('REQUEST (via headers)') self.log.debug('parameters: %s' % str(oauth_request.parameters)) try: self.token = self.client.fetch_request_token(oauth_request) except Exception, error: print "Error: %s\n%s" % (error, traceback.print_exc()) raise Exception self.log.debug('GOT') self.log.debug('key: %s' % str(self.token.key)) self.log.debug('secret: %s' % str(self.token.secret)) self.log.debug('callback confirmed? %s' % str(self.token.callback_confirmed)) self.log.debug('Authorize the request token') oauth_request = oauth.OAuthRequest.from_token_and_callback(token=self.token, http_url=self.client.authorization_url) self.log.debug('REQUEST (via url query string)') self.log.debug('parameters: %s' % str(oauth_request.parameters)) callback(oauth_request.to_url()) elif args['cmd'] == 'authorize': pin = args['pin'] self.log.debug('Obtain an access token') oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.token, verifier=pin, http_url=self.client.access_token_url) oauth_request.sign_request(self.signature_method_hmac_sha1, self.consumer, self.token) self.log.debug('REQUEST (via headers)') self.log.debug('parameters: %s' % str(oauth_request.parameters)) self.token = self.client.fetch_access_token(oauth_request) self.log.debug('GOT') self.log.debug('key: %s' % str(self.token.key)) self.log.debug('secret: %s' % str(self.token.secret)) self.is_oauth = True callback(self.token.key, self.token.secret, pin)