-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmedia_upload_v2.py
More file actions
228 lines (165 loc) · 6.89 KB
/
media_upload_v2.py
File metadata and controls
228 lines (165 loc) · 6.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import os
import sys
import base64
import hashlib
import re
import time
import requests
from requests_oauthlib import OAuth2Session
MEDIA_INIT_ENDPOINT_URL = 'https://api.x.com/2/media/upload/initialize'
MEDIA_ENDPOINT_URL = 'https://api.x.com/2/media/upload'
POST_TO_X_URL = 'https://api.x.com/2/tweets'
# Replace with path to file
VIDEO_FILENAME = 'REPLACE_ME'
# You will need to enable OAuth 2.0 in your App’s auth settings in the Developer Portal to get your client ID.
# Inside your terminal you will need to set an enviornment variable
# export CLIENT_ID='your-client-id'
client_id = os.environ.get("CLIENT_ID")
# If you have selected a type of App that is a confidential client you will need to set a client secret.
# Confidential Clients securely authenticate with the authorization server.
# Inside your terminal you will need to set an enviornment variable
# export CLIENT_SECRET='your-client-secret'
# Remove the comment on the following line if you are using a confidential client
# client_secret = os.environ.get("CLIENT_SECRET")
# Replace the following URL with your callback URL, which can be obtained from your App's auth settings.
redirect_uri = "https://www.example.com"
# Set the scopes
scopes = ["media.write", "users.read", "tweet.read", "tweet.write", "offline.access"]
# Create a code verifier
code_verifier = base64.urlsafe_b64encode(os.urandom(30)).decode("utf-8")
code_verifier = re.sub("[^a-zA-Z0-9]+", "", code_verifier)
# Create a code challenge
code_challenge = hashlib.sha256(code_verifier.encode("utf-8")).digest()
code_challenge = base64.urlsafe_b64encode(code_challenge).decode("utf-8")
code_challenge = code_challenge.replace("=", "")
# Start and OAuth 2.0 session
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri, scope=scopes)
# Create an authorize URL
auth_url = "https://x.com/i/oauth2/authorize"
authorization_url, state = oauth.authorization_url(
auth_url, code_challenge=code_challenge, code_challenge_method="S256"
)
# Visit the URL to authorize your App to make requests on behalf of a user
print(
"Visit the following URL to authorize your App on behalf of your X handle in a browser:"
)
print(authorization_url)
# Paste in your authorize URL to complete the request
authorization_response = input(
"Paste in the full URL after you've authorized your App:\n"
)
# Fetch your access token
token_url = "https://api.x.com/2/oauth2/token"
# The following line of code will only work if you are using a type of App that is a public client
auth = False
# If you are using a confidential client you will need to pass in basic encoding of your client ID and client secret.
# Please remove the comment on the following line if you are using a type of App that is a confidential client
# auth = HTTPBasicAuth(client_id, client_secret)
token = oauth.fetch_token(
token_url=token_url,
authorization_response=authorization_response,
auth=auth,
client_id=client_id,
include_client_id=True,
code_verifier=code_verifier,
)
# Your access token
access = token["access_token"]
headers = {
"Authorization": "Bearer {}".format(access),
"Content-Type": "application/json",
"User-Agent": "MediaUploadSampleCode",
}
class VideoPost(object):
def __init__(self, file_name):
# Defines video Post properties
self.video_filename = file_name
self.total_bytes = os.path.getsize(self.video_filename)
self.media_id = None
self.processing_info = None
def _create_append_url(self):
return f"https://api.x.com/2/media/upload/{self.media_id}/append"
def _create_finalize_url(self):
return f"https://api.x.com/2/media/upload/{self.media_id}/finalize"
def upload_init(self):
# Initializes Upload
print('INIT')
request_data = {
'media_type': 'video/mp4',
'total_bytes': self.total_bytes,
'media_category': 'tweet_video'
}
req = requests.post(url=MEDIA_ENDPOINT_URL, json=request_data, headers=headers)
print(req.status_code)
print(req.text)
media_id = req.json()['data']['id']
self.media_id = media_id
print('Media ID: %s' % str(media_id))
def upload_append(self):
segment_id = 0
bytes_sent = 0
with open(self.video_filename, 'rb') as file:
while bytes_sent < self.total_bytes:
chunk = file.read(4 * 1024 * 1024) # 4MB chunk size
print('APPEND')
files = {'media': ('chunk', chunk, 'application/octet-stream')}
data = {
'segment_index': segment_id
}
headers = {
"Authorization": f"Bearer {access}",
"User-Agent": "MediaUploadSampleCode",
}
req = requests.post(url=self._create_append_url(), data=data, files=files, headers=headers)
if req.status_code < 200 or req.status_code > 299:
print(req.status_code)
print(req.text)
sys.exit(0)
segment_id += 1
bytes_sent = file.tell()
print(f'{bytes_sent} of {self.total_bytes} bytes uploaded')
print('Upload chunks complete.')
def upload_finalize(self):
# Finalizes uploads and starts video processing
print('FINALIZE')
req = requests.post(url=self._create_finalize_url(), headers=headers)
print(req.json())
self.processing_info = req.json()['data'].get('processing_info', None)
self.check_status()
def check_status(self):
# Checks video processing status
if self.processing_info is None:
return
state = self.processing_info['state']
print('Media processing status is %s ' % state)
if state == u'succeeded':
return
if state == u'failed':
sys.exit(0)
check_after_secs = self.processing_info['check_after_secs']
print('Checking after %s seconds' % str(check_after_secs))
time.sleep(check_after_secs)
print('STATUS')
request_params = {
'command': 'STATUS',
'media_id': self.media_id
}
req = requests.get(url=MEDIA_ENDPOINT_URL, params=request_params, headers=headers)
self.processing_info = req.json()['data'].get('processing_info', None)
self.check_status()
def post(self):
# Publishes Post with attached video
payload = {
'text': 'I just uploaded a video with the media upload v2 @XDevelopers API.',
'media': {
'media_ids': [self.media_id]
}
}
req = requests.post(url=POST_TO_X_URL, json=payload, headers=headers)
print(req.json())
if __name__ == '__main__':
videoPost = VideoPost(VIDEO_FILENAME)
videoPost.upload_init()
videoPost.upload_append()
videoPost.upload_finalize()
videoPost.post()