12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- def upload_video(self, video, thumbnail, caption=None, upload_id=None):
- if upload_id is None:
- upload_id = str(int(time.time() * 1000))
- data = {
- 'upload_id': upload_id,
- '_csrftoken': self.token,
- 'media_type': '2',
- '_uuid': self.uuid,
- }
- m = MultipartEncoder(data, boundary=self.uuid)
- self.session.headers.update({'X-IG-Capabilities': '3Q4=',
- 'X-IG-Connection-Type': 'WIFI',
- 'Host': 'i.instagram.com',
- 'Cookie2': '$Version=1',
- 'Accept-Language': 'en-US',
- 'Accept-Encoding': 'gzip, deflate',
- 'Content-type': m.content_type,
- 'Connection': 'keep-alive',
- 'User-Agent': self.user_agent})
- response = self.session.post(config.API_URL + "upload/video/", data=m.to_string())
- if response.status_code == 200:
- body = json.loads(response.text)
- upload_url = body['video_upload_urls'][3]['url']
- upload_job = body['video_upload_urls'][3]['job']
- with open(video, 'rb') as video_bytes:
- video_data = video_bytes.read()
- # solve issue #85 TypeError: slice indices must be integers or None or have an __index__ method
- request_size = len(video_data) // 4
- last_request_extra = len(video_data) - 3 * request_size
- headers = copy.deepcopy(self.session.headers)
- self.session.headers.update({
- 'X-IG-Capabilities': '3Q4=',
- 'X-IG-Connection-Type': 'WIFI',
- 'Cookie2': '$Version=1',
- 'Accept-Language': 'en-US',
- 'Accept-Encoding': 'gzip, deflate',
- 'Content-type': 'application/octet-stream',
- 'Session-ID': upload_id,
- 'Connection': 'keep-alive',
- 'Content-Disposition': 'attachment; filename="video.mov"',
- 'job': upload_job,
- 'Host': 'upload.instagram.com',
- 'User-Agent': self.user_agent
- })
- for i in range(4):
- start = i * request_size
- if i == 3:
- end = i * request_size + last_request_extra
- else:
- end = (i + 1) * request_size
- length = last_request_extra if i == 3 else request_size
- content_range = "bytes {start}-{end}/{len_video}".format(
- start=start, end=end - 1, len_video=len(video_data)).encode('utf-8')
- self.session.headers.update({'Content-Length': str(end - start), 'Content-Range': content_range})
- response = self.session.post(upload_url, data=video_data[start:start + length])
- self.session.headers = headers
- if response.status_code == 200:
- if self.configure_video(upload_id, video, thumbnail, caption):
- self.expose()
- return True
- return False
|