def waitUntilReady(api, video_id, interval, timeout): start_time = time.time() while True: status = VideoEncodingStatusChecker.getStatus(api, video_id) status = status['video_status'] if status != 'processing': break if start_time + timeout <= time.time(): raise FacebookError('video encoding timeout: ' + str(timeout)) time.sleep(interval) if status != 'ready': raise FacebookError('video encoding status: ' + status, ) return
def __build_param(self, etype, tb): if not etype: return None fb_request_errors = [ cls.__name__ for cls in FacebookError.__subclasses__() ] reason = None if etype.__name__ == FacebookRequestError.__name__: reason = Reasons.API elif etype.__name__ in fb_request_errors: reason = Reasons.SDK if reason is None: extracted_tb = traceback.extract_tb(tb, limit=100) for ii, (filename, line, funcname, code) in enumerate(extracted_tb): if filename.find('facebook_business') != -1: reason = Reasons.SDK if reason is None: return None return { 'reason': "{} : {}".format(reason.value, etype.__name__), 'callstack': traceback.format_tb(tb), 'platform': sys.version }
def auth(cls): """ Prepare for Ads API calls and return a tuple with act_id and page_id. page_id can be None but act_id is always set. """ config = cls.load_config() if cls._is_authenticated: return config['act_id'], config.get('page_id', None) if config['app_id'] and config['app_secret'] \ and config['act_id'] and config['access_token']: FacebookAdsApi.init( config['app_id'], config['app_secret'], config['access_token'], config['act_id'], ) cls._is_authenticated = True return config['act_id'], config.get('page_id', None) else: required_fields = set( ('app_id', 'app_secret', 'act_id', 'access_token')) missing_fields = required_fields - set(config.keys()) raise FacebookError( '\n\tFile config.json needs to have the following fields: {}\n' '\tMissing fields: {}\n'.format( ', '.join(required_fields), ', '.join(missing_fields), ))
def waitUntilEncodingReady(self, interval=30, timeout=600): if 'id' not in self: raise FacebookError('Invalid Video ID', ) VideoEncodingStatusChecker.waitUntilReady( self.get_api_assured(), self['id'], interval, timeout, )
def waitUntilEncodingReady(self, interval=30, timeout=600): from facebook_business.video_uploader import VideoEncodingStatusChecker from facebook_business.exceptions import FacebookError if 'id' not in self: raise FacebookError('Invalid Video ID', ) VideoEncodingStatusChecker.waitUntilReady( self.get_api_assured(), self['id'], interval, timeout, )
def upload(self, video, wait_for_encoding=False): """ Upload the given video file. Args: video(required): The AdVideo object that will be uploaded wait_for_encoding: Whether to wait until encoding is finished. """ # Check there is no existing session if self._session: raise FacebookError( "There is already an upload session for this video uploader", ) # Initiate an upload session self._session = VideoUploadSession(video, wait_for_encoding) result = self._session.start() self._session = None return result
def test_clear_secrets_match(): message = """ Message: Call was not successful Method: GET Path: https://graph.facebook.com/v3.1/12345/insights?access_token=sdlkandlk12j4lkhjrlk1nklfalknfslai1oi2j341lknralkdhop1i2&limit=25&after=NTE5OQZDZD Params: {} Status: 500 Response: { "error": { "code": 1, "message": "An unknown error occurred", "error_subcode": 99 } } """ # noqa result = redact_access_token(FacebookError(message)) assert ( str(result) == """ Message: Call was not successful Method: GET Path: https://graph.facebook.com/v3.1/12345/insights?access_token=********************************************************&limit=25&after=NTE5OQZDZD Params: {} Status: 500 Response: { "error": { "code": 1, "message": "An unknown error occurred", "error_subcode": 99 } } """ ) # noqa
def test_clear_secrets_no_match(): message = """ Message: Call was not successful Method: GET Path: https://graph.facebook.com/v3.1/12345/insights?limit=25&after=NTE5OQZDZD¶ms=test1,test2 Params: {} Status: 500 Response: { "error": { "code": 1, "message": "An unknown error occurred", "error_subcode": 99 } } """ # noqa result = redact_access_token(FacebookError(message)) assert ( str(result) == """ Message: Call was not successful Method: GET Path: https://graph.facebook.com/v3.1/12345/insights?limit=25&after=NTE5OQZDZD¶ms=test1,test2 Params: {} Status: 500 Response: { "error": { "code": 1, "message": "An unknown error occurred", "error_subcode": 99 } } """ ) # noqa
def update_product(self, retailer_id, **kwargs): """Updates a product stored in a product catalog Args: retailer_id: product id from product feed. g:price tag in Google Shopping feed kwargs: key-value pairs to update on the object, being key the field name and value the updated value Returns: The FacebookResponse object. """ if not kwargs: raise FacebookError( """No fields to update provided. Example: catalog = ProductCatalog('catalog_id') catalog.update_product( retailer_id, price=100, availability=Product.Availability.out_of_stock ) """, ) product_endpoint = ':'.join(( 'catalog', self.get_id_assured(), self.b64_encoded_id(retailer_id), )) url = '/'.join(( FacebookSession.GRAPH, FacebookAdsApi.API_VERSION, product_endpoint, )) return self.get_api_assured().call( 'POST', url, params=kwargs, )