def __init__(self, twitter_config, urlToken=None): """ Initialise an instance of the TwitterAuth class that will enable us to create an oauth access token with twitter """ # initialise the configuration self.config = twitter_config self.requestToken = urlToken self.accessToken = None # create the consumer self.consumer = oauth.OAuthConsumer(self.config.consumerKey, self.config.consumerSecret) # if the oauth token is not set, then prepare the request token and authorization steps if self.requestToken is None: # create the request token self.requestToken = self._initRequestToken(URL_REQUEST_TOKEN) # authorize the request token self._authorizeToken() # otherwise, create an oauth token from the string passed to the function else: # look for the twitter user from the database requestKey = OAuthAccessKey.findByRequestKey(self.requestToken) # if we found the user if requestKey is not None: self.accessToken = oauth.OAuthToken.from_string( requestKey.requestKeyEncoded)
def __init__(self, run_as_user, twitter_config=None, maxInterval=slicer.DEFAULT_MAX_INTERVAL): """ Initialise the new TwawlTask object """ # call the inherited constructor slicer.SlicedTask.__init__(self, maxInterval) # initialise private members self._runAsUser = run_as_user self._accessKey = None self._twitterConfig = twitter_config # initialise members self.ruleName = '' self.highTweetId = 0 self.searchFor = 'A String that we are very unlikely to find...' self.nextRequest = None self.processedCount = 0 self.currentHistory = None self.searchType = "search" # initialise function callbacks self.tweetInspectors = [] # if the run user has been specified, then find the access key for the user if run_as_user: accesskey_data = OAuthAccessKey.findByUserName(run_as_user) # if we have found some data, then update the access key if accesskey_data: self._accessKey = accesskey_data.accessKeyEncoded
def __init__(self, run_as_user, twitter_config = None, maxInterval = slicer.DEFAULT_MAX_INTERVAL): """ Initialise the new TwawlTask object """ # call the inherited constructor slicer.SlicedTask.__init__(self, maxInterval) # initialise private members self._runAsUser = run_as_user self._accessKey = None self._twitterConfig = twitter_config # initialise members self.ruleName = '' self.highTweetId = 0 self.searchFor = 'A String that we are very unlikely to find...' self.nextRequest = None self.processedCount = 0 self.currentHistory = None self.searchType = "search" # initialise function callbacks self.tweetInspectors = [] # if the run user has been specified, then find the access key for the user if run_as_user: accesskey_data = OAuthAccessKey.findByUserName(run_as_user) # if we have found some data, then update the access key if accesskey_data: self._accessKey = accesskey_data.accessKeyEncoded
def _initRequestToken(self, requestTokenUrl): """ This method is used to initialise the request token so we can progress obtaining an access token to access twitter. """ # create and sign the oauth request oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_url=URL_REQUEST_TOKEN) oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None) logging.debug("created auth request: %s", oauth_request) # send the request request_result = urlfetch.fetch(url = requestTokenUrl, headers = oauth_request.to_header()) # if the response was successful, then process the response fnresult = None if request_result.status_code == 200: fnresult = oauth.OAuthToken.from_string(request_result.content) # look for the twawl user in the database request = OAuthAccessKey.findOrCreate(fnresult.key, partnerId = 'twitter') # update the user request key details request.requestKeyEncoded = fnresult.to_string() # save the user to the database request.put() else: logging.warning("Unable to obtain request token, response was %s", request_result.content) logging.debug("received request token: %s", fnresult) return fnresult
def __init__(self, twitter_config, urlToken = None): """ Initialise an instance of the TwitterAuth class that will enable us to create an oauth access token with twitter """ # initialise the configuration self.config = twitter_config self.requestToken = urlToken self.accessToken = None # create the consumer self.consumer = oauth.OAuthConsumer(self.config.consumerKey, self.config.consumerSecret) # if the oauth token is not set, then prepare the request token and authorization steps if self.requestToken is None: # create the request token self.requestToken = self._initRequestToken(URL_REQUEST_TOKEN) # authorize the request token self._authorizeToken() # otherwise, create an oauth token from the string passed to the function else: # look for the twitter user from the database requestKey = OAuthAccessKey.findByRequestKey(self.requestToken) # if we found the user if requestKey is not None: self.accessToken = oauth.OAuthToken.from_string(requestKey.requestKeyEncoded)
def processResponse(self, content, responseCallback): """ This method is used to process the response from twitter in the case that our request has been successful @content - the content of the response returned from the request @responseCallback - a method callback that can be used to push details back to the calling method """ # decode the json response searchResults = simplejson.loads(content) # if we have have a url auth token, then update the oauth details if self.urlAuthToken: auth_data = OAuthAccessKey.findByRequestKey(self.urlAuthToken) auth_data.userId = searchResults.get('id') auth_data.userName = searchResults.get('screen_name') auth_data.put() # read the unique numeric id of the user from twitter self.twitterId = searchResults.get('id', self.twitterId) # read the current screen name (this can change) self.screenName = searchResults.get('screen_name', self.screenName) # read what they have specified as their contact name self.realName = searchResults.get('name', self.realName) # read the url for the current avatar image self.profileImageUrl = searchResults.get('profile_image_url', self.profileImageUrl) # now get the bigger image self.profileImageUrl = self.profileImageUrl.replace( "_normal.jpg", "_bigger.jpg") # read the location they have entered into their profile - may or may not be useful self.location = searchResults.get('location', self.location) # read the current number of followers that user has self.followersCount = searchResults.get('followersCount', self.followersCount) # read the date at which they signed up for twitter self.dateCreated = searchResults.get('created_at', self.followersCount) # read the utc offset they have specified for their account (in seconds) self.utcOffset = searchResults.get('utc_offset', self.utcOffset) logging.info("Successfully logged in %s (%d: %s from %s)", self.screenName, self.twitterId, self.realName, self.location)
def processResponse(self, content, responseCallback): """ This method is used to process the response from twitter in the case that our request has been successful @content - the content of the response returned from the request @responseCallback - a method callback that can be used to push details back to the calling method """ # decode the json response searchResults = simplejson.loads(content) # if we have have a url auth token, then update the oauth details if self.urlAuthToken: auth_data = OAuthAccessKey.findByRequestKey(self.urlAuthToken) auth_data.userId = searchResults.get('id') auth_data.userName = searchResults.get('screen_name') auth_data.put() # read the unique numeric id of the user from twitter self.twitterId = searchResults.get('id', self.twitterId) # read the current screen name (this can change) self.screenName = searchResults.get('screen_name', self.screenName) # read what they have specified as their contact name self.realName = searchResults.get('name', self.realName) # read the url for the current avatar image self.profileImageUrl = searchResults.get('profile_image_url', self.profileImageUrl) # now get the bigger image self.profileImageUrl = self.profileImageUrl.replace("_normal.jpg", "_bigger.jpg") # read the location they have entered into their profile - may or may not be useful self.location = searchResults.get('location', self.location) # read the current number of followers that user has self.followersCount = searchResults.get('followersCount', self.followersCount) # read the date at which they signed up for twitter self.dateCreated = searchResults.get('created_at', self.followersCount) # read the utc offset they have specified for their account (in seconds) self.utcOffset = searchResults.get('utc_offset', self.utcOffset) logging.info("Successfully logged in %s (%d: %s from %s)", self.screenName, self.twitterId, self.realName, self.location)
def buildAccessToken(self, accessTokenUrl): """ This static method is used to contact twitter and build the access token that will permit us to access the twitter information stream """ # if the request token has not been set, then don't attempt to get an access token if self.requestToken is None: logging.warning( "Unable to contact twitter, no request token available.") return None # initialise the oauth request logging.debug( "Attempting to build access token from request token '%s'", self.requestToken) oauth_request = oauth.OAuthRequest.from_consumer_and_token( self.consumer, token=self.accessToken, http_url=URL_ACCESS_TOKEN) oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, self.accessToken) # send the request request_result = urlfetch.fetch(url=accessTokenUrl, headers=oauth_request.to_header()) # if the response was successful, then process the response fnresult = None if request_result.status_code == 200: fnresult = oauth.OAuthToken.from_string(request_result.content) # update the user to store the access token request = OAuthAccessKey.findOrCreate(self.requestToken, partnerId='twitter') # update the user accesskey request.accessKeyEncoded = fnresult.to_string() request.put() else: logging.warning("Unable to obtain access token: %s", request_result.content) logging.debug("received access token: %s", fnresult) if fnresult: return fnresult.to_string()
def getAccessToken(twitter_config, allowInit=True, urlToken=None): """ This static method is used to wrap the operations of authenticating with twitter. In addition to prevent requerying twitter many times for the authentication token, this is stored in the mem-cache (TODO: investigate security concerns) to optimize performance. """ # initialise variables fnresult = None logging.debug( "Application requested Twitter Access Token: allowInit = %s, url auth token = %s", allowInit, urlToken) # if the oauth token is set, then we should regenerate the authentication token if urlToken is not None: # create the authenticator authenticator = TwitterAuth(twitter_config, urlToken) # build the access token fnresult = authenticator.buildAccessToken(URL_ACCESS_TOKEN) return fnresult # if the access key is still not known, then see if we can obtain it from the database if fnresult is None: fnresult = OAuthAccessKey.findByRequestKey(urlToken) # if we have a value, then return that value if fnresult is not None: logging.debug( "oauth key for request key '%s' retrieved from the cache or db", urlToken) return fnresult # seeing as we haven't used the cache, let's get stuck into this (If we are permitted) if allowInit: # TODO: make this work for more than just a default service user authenticator = TwitterAuth(twitter_config) else: logging.error("Unable to contact twitter, access token unknown") # return the access token return fnresult
def getAccessToken(twitter_config, allowInit = True, urlToken = None): """ This static method is used to wrap the operations of authenticating with twitter. In addition to prevent requerying twitter many times for the authentication token, this is stored in the mem-cache (TODO: investigate security concerns) to optimize performance. """ # initialise variables fnresult = None logging.debug("Application requested Twitter Access Token: allowInit = %s, url auth token = %s", allowInit, urlToken) # if the oauth token is set, then we should regenerate the authentication token if urlToken is not None: # create the authenticator authenticator = TwitterAuth(twitter_config, urlToken) # build the access token fnresult = authenticator.buildAccessToken(URL_ACCESS_TOKEN) return fnresult # if the access key is still not known, then see if we can obtain it from the database if fnresult is None: fnresult = OAuthAccessKey.findByRequestKey(urlToken) # if we have a value, then return that value if fnresult is not None: logging.debug("oauth key for request key '%s' retrieved from the cache or db", urlToken) return fnresult # seeing as we haven't used the cache, let's get stuck into this (If we are permitted) if allowInit: # TODO: make this work for more than just a default service user authenticator = TwitterAuth(twitter_config) else: logging.error("Unable to contact twitter, access token unknown") # return the access token return fnresult
def _initRequestToken(self, requestTokenUrl): """ This method is used to initialise the request token so we can progress obtaining an access token to access twitter. """ # create and sign the oauth request oauth_request = oauth.OAuthRequest.from_consumer_and_token( self.consumer, http_url=URL_REQUEST_TOKEN) oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None) logging.debug("created auth request: %s", oauth_request) # send the request request_result = urlfetch.fetch(url=requestTokenUrl, headers=oauth_request.to_header()) # if the response was successful, then process the response fnresult = None if request_result.status_code == 200: fnresult = oauth.OAuthToken.from_string(request_result.content) # look for the twawl user in the database request = OAuthAccessKey.findOrCreate(fnresult.key, partnerId='twitter') # update the user request key details request.requestKeyEncoded = fnresult.to_string() # save the user to the database request.put() else: logging.warning("Unable to obtain request token, response was %s", request_result.content) logging.debug("received request token: %s", fnresult) return fnresult
def buildAccessToken(self, accessTokenUrl): """ This static method is used to contact twitter and build the access token that will permit us to access the twitter information stream """ # if the request token has not been set, then don't attempt to get an access token if self.requestToken is None: logging.warning("Unable to contact twitter, no request token available.") return None # initialise the oauth request logging.debug("Attempting to build access token from request token '%s'", self.requestToken) oauth_request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, token=self.accessToken, http_url=URL_ACCESS_TOKEN) oauth_request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, self.accessToken) # send the request request_result = urlfetch.fetch(url = accessTokenUrl, headers = oauth_request.to_header()) # if the response was successful, then process the response fnresult = None if request_result.status_code == 200: fnresult = oauth.OAuthToken.from_string(request_result.content) # update the user to store the access token request = OAuthAccessKey.findOrCreate(self.requestToken, partnerId = 'twitter') # update the user accesskey request.accessKeyEncoded = fnresult.to_string() request.put() else: logging.warning("Unable to obtain access token: %s", request_result.content) logging.debug("received access token: %s", fnresult) if fnresult: return fnresult.to_string()