in facebook_business/video_uploader.py [0:0]
def send_request(self, context):
"""
send transfer request with the given context
"""
# Init a VideoUploadRequest
request = VideoUploadRequest(self._api)
self._start_offset = context.start_offset
self._end_offset = context.end_offset
filepath = context.file_path
file_size = os.path.getsize(filepath)
# Give a chance to retry every 10M, or at least twice
retry = max(file_size / (1024 * 1024 * 10), 2)
f = open(filepath, 'rb')
# While the there are still more chunks to send
while self._start_offset != self._end_offset:
# Read a chunk of file
f.seek(self._start_offset)
chunk = f.read(self._end_offset - self._start_offset)
context.start_offset = self._start_offset
context.end_offset = self._end_offset
# Parse the context
request.setParams(
self.getParamsFromContext(context),
{'video_file_chunk': (
os.path.basename(context.file_path),
chunk,
'multipart/form-data',
)},
)
# send the request
try:
response = request.send(
(context.account_id, 'advideos'),
).json()
self._start_offset = int(response['start_offset'])
self._end_offset = int(response['end_offset'])
except FacebookRequestError as e:
subcode = e.api_error_subcode()
body = e.body()
if subcode == 1363037:
# existing issue, try again immedidately
if (body and 'error' in body and
'error_data' in body['error'] and
'start_offset' in body['error']['error_data'] and
retry > 0):
self._start_offset = int(
body['error']['error_data']['start_offset'],
)
self._end_offset = int(
body['error']['error_data']['end_offset'],
)
retry = max(retry - 1, 0)
continue
elif ('error' in body and
'is_transient' in body['error']):
if body['error']['is_transient']:
time.sleep(1)
continue
f.close()
raise e
f.close()
return response