Skip to content

Commit 14ad8b8

Browse files
committed
feat(media): 📝 add docs for media upload api v2
1 parent bb17099 commit 14ad8b8

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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!
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
You can use media upload endpoint to upload simple media, images(gifs).
2+
3+
You can get more information for this at [docs](https://docs.x.com/x-api/media/media-upload)
4+
5+
## upload simple
6+
7+
```python
8+
9+
with open("path/to/image", "rb") as media:
10+
resp = my_api.upload_media_simple_v2(media=media)
11+
print(resp)
12+
```
13+

docs/mkdocs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ nav:
3737
- Media Upload:
3838
- Simple Upload: usage/media-upload/simple-upload.md
3939
- Chunked Upload: usage/media-upload/chunked-upload.md
40+
41+
- Media Upload V2:
42+
- Simple Upload: usage/media-upload-v2/simple-upload.md
43+
- Chunked Upload: usage/media-upload-v2/chunked-upload.md
4044
- Tweets:
4145
- Tweet Lookup: usage/tweets/tweet-lookup.md
4246
- Manage Tweets: usage/tweets/tweet-manage.md

0 commit comments

Comments
 (0)