|
| 1 | +This guide will help you make your first requests to upload media using the X API v2 media upload endpoint(s). |
| 2 | + |
| 3 | +You can get more information for this at [docs](https://docs.x.com/x-api/media/quickstart/media-upload-chunked) |
| 4 | + |
| 5 | +For video or chunked uploads, you must: |
| 6 | + |
| 7 | +1. Initialize the upload using the `INIT` command |
| 8 | +2. Upload each chunk of bytes using the `APPEND` command |
| 9 | +3. Complete the upload using the `FINALIZE` command |
| 10 | + |
| 11 | +let's do it, Now we need to upload a big video with a filename `/path/to/video.mp4` |
| 12 | + |
| 13 | +### Step 1: Initialize the upload |
| 14 | + |
| 15 | +As first step, you need to initialize the upload. |
| 16 | + |
| 17 | +```python |
| 18 | + |
| 19 | +import os |
| 20 | + |
| 21 | +filename = "/path/to/video.mp4" |
| 22 | + |
| 23 | +init_resp = myapi.upload_media_chunked_init_v2( |
| 24 | + total_bytes=os.path.getsize(filename), |
| 25 | + media_type="video/mp4", |
| 26 | + media_category="tweet_video", |
| 27 | +) |
| 28 | +print(init_resp) |
| 29 | +# Response(data=MediaUpload(id='1912334964932374529', media_key='7_1912334964932374529', processing_info=None, image=None, video=None)) |
| 30 | +``` |
| 31 | + |
| 32 | +### Step 2: Append the file by chunks |
| 33 | + |
| 34 | +Once we have the media identifiers `id` from the `init_resp`, we can start uploading the file by chunks. |
| 35 | + |
| 36 | +```python |
| 37 | + |
| 38 | +media_id = init_resp.data.id |
| 39 | + |
| 40 | +chunk_size = 2 * 1024 * 1024 |
| 41 | +segment_index = 0 |
| 42 | +with open(filename, "rb") as f: |
| 43 | + while True: |
| 44 | + chunk = f.read(chunk_size) |
| 45 | + if not chunk: |
| 46 | + break |
| 47 | + |
| 48 | + chunk_resp = myapi.upload_media_chunked_append_v2( |
| 49 | + media_id=media_id, |
| 50 | + media=chunk, |
| 51 | + segment_index=segment_index, |
| 52 | + ) |
| 53 | + print(chunk_resp) |
| 54 | + segment_index += 1 |
| 55 | + |
| 56 | +# True |
| 57 | +``` |
| 58 | + |
| 59 | +### Step 3: Finalize the upload |
| 60 | + |
| 61 | +Everything is ok, we need finalize the upload. |
| 62 | + |
| 63 | +```python |
| 64 | +finalize_resp = myapi.upload_media_chunked_finalize_v2(media_id=media_id) |
| 65 | +print(finalize_resp) |
| 66 | +# Response(data=MediaUpload(id='1912090619981471744', media_key='7_1912090619981471744', processing_info=MediaUploadResponseProcessingInfo(state='succeeded', check_after_secs=None, progress_percent=None, error=None), image=None, video=None)) |
| 67 | +``` |
| 68 | + |
| 69 | +### Step 4 (Optional): Check the processing status |
| 70 | + |
| 71 | +Once you have finalized the upload, you can check the processing status. |
| 72 | + |
| 73 | +```python |
| 74 | +status_resp = myapi.upload_media_chunked_status_v2(media_id=media_id) |
| 75 | +print(status_resp) |
| 76 | +# Response(data=MediaUpload(id='1912090619981471744', media_key='7_1912090619981471744', processing_info=MediaUploadResponseProcessingInfo(state='succeeded', check_after_secs=None, progress_percent=100, error=None), image=None, video=None)) |
| 77 | +``` |
| 78 | + |
| 79 | +### Step 5: Create tweet with media |
| 80 | + |
| 81 | +Congratulations, you have uploaded a video using the X API v2 media upload endpoint(s). |
| 82 | + |
| 83 | +Now we can create a tweet with this video. |
| 84 | + |
| 85 | +```python |
| 86 | +tweet_resp = myapi.create_tweet(text="My first tweet with a video", media_media_ids=[media_id]) |
| 87 | + |
| 88 | +# Tweet(id=1912338879258194343, text=My first tweet with a video...) |
| 89 | +``` |
| 90 | + |
| 91 | +Enjoy it! |
0 commit comments