MCPcopy Index your code
hub / github.com/ryanmcgrath/twython / upload_video

Method upload_video

twython/endpoints.py:174–261  ·  view source on GitHub ↗

Uploads video file to Twitter servers in chunks. The file will be available to be attached to a status for 60 minutes. To attach to a update, pass a list of returned media ids to the :meth:`update_status` method using the ``media_ids`` param. Upload happens in 3 stages:

(self, media, media_type, media_category=None, size=None, check_progress=False)

Source from the content-addressed store, hash-verified

172 return self.post("https://upload.twitter.com/1.1/media/metadata/create.json", params=params)
173
174 def upload_video(self, media, media_type, media_category=None, size=None, check_progress=False):
175 """Uploads video file to Twitter servers in chunks. The file will be available to be attached
176 to a status for 60 minutes. To attach to a update, pass a list of returned media ids
177 to the :meth:`update_status` method using the ``media_ids`` param.
178
179 Upload happens in 3 stages:
180 - INIT call with size of media to be uploaded(in bytes). If this is more than 15mb, twitter will return error.
181 - APPEND calls each with media chunk. This returns a 204(No Content) if chunk is received.
182 - FINALIZE call to complete media upload. This returns media_id to be used with status update.
183
184 Twitter media upload api expects each chunk to be not more than 5mb. We are sending chunk of 1mb each.
185
186 Docs:
187 https://developer.twitter.com/en/docs/media/upload-media/uploading-media/chunked-media-upload
188
189 """
190 upload_url = 'https://upload.twitter.com/1.1/media/upload.json'
191 if not size:
192 media.seek(0, os.SEEK_END)
193 size = media.tell()
194 media.seek(0)
195
196 # Stage 1: INIT call
197 params = {
198 'command': 'INIT',
199 'media_type': media_type,
200 'total_bytes': size,
201 'media_category': media_category
202 }
203 response_init = self.post(upload_url, params=params)
204 media_id = response_init['media_id']
205
206 # Stage 2: APPEND calls with 1mb chunks
207 segment_index = 0
208 while True:
209 data = media.read(1*1024*1024)
210 if not data:
211 break
212 media_chunk = BytesIO()
213 media_chunk.write(data)
214 media_chunk.seek(0)
215
216 params = {
217 'command': 'APPEND',
218 'media_id': media_id,
219 'segment_index': segment_index,
220 'media': media_chunk,
221 }
222 self.post(upload_url, params=params)
223 segment_index += 1
224
225 # Stage 3: FINALIZE call to complete upload
226 params = {
227 'command': 'FINALIZE',
228 'media_id': media_id
229 }
230
231 response = self.post(upload_url, params=params)

Callers

nothing calls this directly

Calls 2

postMethod · 0.80
getMethod · 0.80

Tested by

no test coverage detected