def _http(self, method, accessToken, path, priority='high', parse_json=True, **params): if params is None: params = {} num_retries = 0 max_retries = 5 if accessToken is not None: params['access_token'] = accessToken if method != 'get': params['method'] = method data = None baseurl = '' if path[:8] != 'https://': baseurl = 'https://graph.facebook.com/' url = "%s%s" % (baseurl, path) if method == 'get': response, content = service_request('facebook', method, url, query_params=params, priority=priority, timeout=DEFAULT_TIMEOUT) else: print('body: %s url: %s' % (params, url)) response, content = service_request('facebook', method, url, body=params, priority=priority, timeout=DEFAULT_TIMEOUT) if parse_json: result = json.loads(content) else: result = content if int(response.status) >= 400: result = json.loads(content) logs.info('result: %s' % result) msg = None code = None if 'error' in result: msg = result['error']['message'] if 'code' in result['error']: code = result['error']['code'] if code == 190: raise StampedFacebookTokenError('Invalid Facebook token') elif code == 200: raise StampedFacebookPermissionsError(msg) elif code == 3501: raise StampedFacebookUniqueActionAlreadyTakenOnObject('OG Action already exists for object') elif code == 1611118: raise StampedFacebookOGImageSizeError(msg) if 'type' in result['error'] and result['error']['type'] == 'OAuthException': # OAuth exception pass logs.info('Facebook API Error: status: %s code: %s message: %s' % (response.status, code, msg)) raise StampedThirdPartyError('Facebook API Error: %s' % msg) #logs.info("Retrying (%s)" % (num_retries)) #time.sleep(0.5) return result
def method(self, method, priority='low', timeout=None, **kwargs): for k,v in kwargs.items(): if isinstance(v,int) or isinstance(v,float): kwargs[k] = str(v) elif isinstance(v,unicode): kwargs[k] = v.encode('utf-8') kwargs['method'] = method oauthRequest = oauth.Request.from_consumer_and_token(self.__consumer, http_url='http://api.rdio.com/1/', http_method='POST', token = None, parameters=kwargs) oauthRequest.sign_request(self.__signature_method_hmac_sha1, self.__consumer, None) body = oauthRequest body.update(kwargs) headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept-encoding':'gzip' } response, content = service_request('rdio', 'POST', 'http://api.rdio.com/1/', header=headers, body=body, priority=priority, timeout=timeout) if response.status >= 400: raise StampedThirdPartyError('Rdio API Error: Status: %s Content: %s' % (response.status, content)) return json.loads(content)
def lookupIMDBRaw(self, imdb_id, priority="low", timeout=None): url = "http://www.thetvdb.com/api/GetSeriesByRemoteID.php" params = {"imdbid": imdb_id} try: response, xml = service_request("tvdb", "GET", url, query_params=params, priority=priority, timeout=timeout) except: return None # Putting results into the mongo cache will convert them to unicode, so in order to keep things parallel between # the case where this does go through the cache and the case where it doesn't, we decode here and encode on the # other side. return xml.decode("utf-8")
def _get(self, uri, priority="low", timeout=None, params=None): if params is not None: uri = "%s?%s" % (uri, urllib.urlencode(params)) # construct the signed url uri = "%s%sclient=%s" % (uri, "?" if params is None else "&", self._client_id) uri = uri.encode("utf-8") url = "%s%s&sig=%s" % (self.BASE_URL, uri, self._sign(uri)) header = {"Accept-encoding": "gzip", "Accept": "application/json"} response, content = service_request( "singleplatform", "GET", url, header=header, priority=priority, timeout=timeout ) logs.info(url) result = json.loads(content) return result
def __call__(self, timeout=None, **kwargs): kwargs["Timestamp"] = time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()) kwargs["Operation"] = self.Operation kwargs["Version"] = self.Version kwargs["AWSAccessKeyId"] = self.AWSAccessKeyId kwargs["Service"] = "AWSECommerceService" if self.Style: kwargs["Style"] = self.Style if self.AssociateTag: kwargs["AssociateTag"] = self.AssociateTag if "Style" in kwargs: service_domain = SERVICE_DOMAINS[self.Region][1] else: service_domain = SERVICE_DOMAINS[self.Region][0] keys = kwargs.keys() keys.sort() quoted_strings = "&".join( "%s=%s" % (k, urllib.quote(unicode(kwargs[k]).encode("utf-8"), safe="~")) for k in keys ) data = "GET\n" + service_domain + "\n/onca/xml\n" + quoted_strings digest = hmac.new(self.AWSSecretAccessKey, data, sha256).digest() signature = urllib.quote(b64encode(digest)) api_string = "http://" + service_domain + "/onca/xml?" + quoted_strings + "&Signature=%s" % signature priority = kwargs.get("priority", "low") header = {"Accept-Encoding": "gzip"} response, content = service_request( "amazon", "GET", api_string, header=header, priority=priority, timeout=timeout ) if response.get("Content-Encoding", None) == "gzip": gzipped_file = gzip.GzipFile(fileobj=StringIO.StringIO(content)) response_text = gzipped_file.read() else: response_text = content return response_text
def __instagram(self, service, priority='low', timeout=None, max_retries=3, verb='GET', **params): if 'client_id' not in params: params['client_id'] = self.__client_id if service.startswith('http'): url = service else: url = "%s/%s" % (HOST, service) response, content = service_request('instagram', 'GET', url, query_params=params, header={ 'Accept' : 'application/json' }, priority=priority, timeout=timeout) data = json.loads(content) return data
def lookup(self, thetvdb_id, priority="low", timeout=None): details_url = "http://www.thetvdb.com/api/%s/series/%s/all/" % (self.api_key, thetvdb_id) response, xml = service_request("tvdb", "GET", details_url, priority=priority, timeout=timeout) tree = objectify.fromstring(xml) items = tree.findall(".//Series") """# useful debugging aid f = open('thetvdb.%s.xml' % thetvdb_id, 'w') f.write(xml) f.close() """ if items is not None and 1 == len(items): item = items[0] return self._parse_entity(item) return
def _getAutocompleteResponse(self, latLng, query, apiKey, optionalParams=None, priority='low'): params = { 'input' : query, 'sensor' : 'false', 'types' : 'establishment', 'key' : apiKey, } if latLng is not None: params['location'] = self._geocoder.getEncodedLatLng(latLng) self._handleParams(params, optionalParams) # example URL: # https://maps.googleapis.com/maps/api/place/autocomplete/json?input=test&sensor=false&key=AIzaSyAxgU3LPU-m5PI7Jh7YTYYKAz6lV6bz2ok url = self._getAPIURL('autocomplete') utils.log('[GooglePlaces] ' + url) response, content = service_request('googleplaces', 'GET', url, query_params=params, priority=priority) return json.loads(content)
def __http(self, verb, service, user_token=None, user_secret=None, priority='high', **params): url = 'https://api.twitter.com/%s' % service # Generate the oauth token from the user_token and user_secret if user_token is not None and user_secret is not None: token = oauth.OAuthToken( user_token, user_secret, ) # Prepare the oauth request oauthRequest = oauth.OAuthRequest.from_consumer_and_token(self.__consumer, http_url=url, parameters=params, token=token, http_method=verb) oauthRequest.sign_request( self.__signature_method, self.__consumer, token) header = oauthRequest.to_header() else: header = None body = oauthRequest.to_postdata() if verb == 'POST' else None logs.debug(url) # Send the http request try: response, content = service_request('twitter', verb, url, query_params=params, body=body, header=header, priority=priority) result = json.loads(content) except Exception: logs.warning('Error connecting to Twitter') raise StampedThirdPartyError('There was an error connecting to Twitter') if 'error' in result: raise StampedInputError('Twitter API Fail: %s' % result['error']) elif 'errors' in result: errors = result['errors'] # Internal Error if len(errors) == 1 and 'code' in errors[0] and errors[0]['code'] == 131: raise StampedThirdPartyInternalError('Twitter returned an internal error') raise StampedThirdPartyError('There was an error connecting to Twitter') return result
def getPlaceDetailsResponse(self, reference, apiKey, optionalParams=None, priority='low'): params = { 'reference' : reference, 'sensor' : 'false', 'key' : apiKey, } self._handleParams(params, optionalParams) # example URL: # https://maps.googleapis.com/maps/api/place/details/json?reference=...&sensor=false&key=AIzaSyAxgU3LPU-m5PI7Jh7YTYYKAz6lV6bz2ok url = self._getAPIURL('details') utils.log('[GooglePlaces] ' + url) try: response, content = service_request('googleplaces', 'GET', url, query_params=params, priority=priority) return json.loads(content) except: utils.log('[GooglePlaces] unexpected error searching "' + url + '"') raise
def addPlaceReport(self, entity, priority='low'): params = { 'sensor' : 'false', 'key' : self._getAPIKey(0, 0), } post_params = { 'location' : { 'lat' : entity.lat, 'lng' : entity.lng, }, 'name' : entity.title, 'accuracy' : 50, 'types' : [ ], 'language' : 'en-US', } url = self._getAPIURL('add') #utils.log(url) response, content = service_request('googleplaces', 'POST', url, query_params=params, body=post_params, priority='low') return content
def _getSearchResponseByLatLng(self, latLng, apiKey, optionalParams=None, priority="low"): params = { 'location' : self._geocoder.getEncodedLatLng(latLng), 'radius' : self.DEFAULT_RADIUS, 'sensor' : 'false', 'key' : apiKey, } self._handleParams(params, optionalParams) # example URL: # https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name=harbour&sensor=false&key=AIzaSyAxgU3LPU-m5PI7Jh7YTYYKAz6lV6bz2ok url = self._getAPIURL('search') utils.log('[GooglePlaces] ' + url) try: # GET the data and parse the response as json response, content = service_request('googleplaces', 'GET', url, query_params=params, priority=priority) return json.loads(content) except: utils.log('[GooglePlaces] unexpected error searching "' + url + '"') raise
def __rawFactual(self, service, prefix='places', priority='low', **args): """ Helper method for making OAuth Factual API calls. This code is based on the recommended Python sample code available at: http://developer.factual.com/display/docs/Core+API+-+Oauth The custom beginning constructs the url based on input parameters. The custom end parses the JSON response and abstracts the data portion if successful. """ pairs = [ '%s=%s' % (k,v) for k,v in args.items() ] url = "http://api.v3.factual.com/%s/%s?%s" % (prefix,service,'&'.join(pairs)) params = parse_qsl(urlparse(url).query) consumer = oauth.OAuthConsumer(key=self.__v3_key, secret=self.__v3_secret) request = oauth.OAuthRequest.from_consumer_and_token(consumer, http_method='GET', http_url=url, parameters=params) request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), consumer, None) response, content = service_request('factual', 'GET', url, header=request.to_header(), priority=priority) return content
def method(self, method, priority='low', timeout=None, **params): url = 'http://itunes.apple.com/%s' % method response, content = service_request('itunes', 'GET', url, query_params=params, priority=priority, timeout=timeout) result = json.loads(content) return result