Skip to content

Commit

Permalink
Added is_long_video argument to upload_media
Browse files Browse the repository at this point in the history
  • Loading branch information
d60 committed May 1, 2024
1 parent c929dac commit 40d7edb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 13 deletions.
29 changes: 22 additions & 7 deletions twikit/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,8 @@ def upload_media(
wait_for_completion: bool = False,
status_check_interval: float = 1.0,
media_type: str | None = None,
media_category: str | None = None
media_category: str | None = None,
is_long_video: bool = False
) -> str:
"""
Uploads media to twitter.
Expand All @@ -635,6 +636,9 @@ def upload_media(
If not specified, it will be guessed from the source.
media_category : :class:`str`, default=None
The media category.
is_long_video : :class:`bool`, default=False
If this is True, videos longer than 2:20 can be uploaded.
(Twitter Premium only)
Returns
-------
Expand Down Expand Up @@ -689,6 +693,11 @@ def upload_media(
# Checking the upload status of an image is impossible.
wait_for_completion = False

if is_long_video:
endpoint = Endpoint.UPLOAD_MEDIA_2
else:
endpoint = Endpoint.UPLOAD_MEDIA

total_bytes = len(binary)

# ============ INIT =============
Expand All @@ -700,7 +709,7 @@ def upload_media(
if media_category is not None:
params['media_category'] = media_category
response = self.http.post(
Endpoint.UPLOAD_MEDIA,
endpoint,
params=params,
headers=self._base_headers
).json()
Expand Down Expand Up @@ -728,7 +737,7 @@ def upload_media(
)
}
self.http.post(
Endpoint.UPLOAD_MEDIA,
endpoint,
params=params,
headers=headers,
files=files
Expand All @@ -744,14 +753,14 @@ def upload_media(
'media_id': media_id,
}
self.http.post(
Endpoint.UPLOAD_MEDIA,
endpoint,
params=params,
headers=self._base_headers,
)

if wait_for_completion:
while True:
state = self.check_media_status(media_id)
state = self.check_media_status(media_id, is_long_video)
processing_info = state['processing_info']
if 'error' in processing_info:
raise InvalidMedia(processing_info['error'].get('message'))
Expand All @@ -761,7 +770,9 @@ def upload_media(

return media_id

def check_media_status(self, media_id: str) -> dict:
def check_media_status(
self, media_id: str, is_long_video: bool = False
) -> dict:
"""
Check the status of uploaded media.
Expand All @@ -780,8 +791,12 @@ def check_media_status(self, media_id: str) -> dict:
'command': 'STATUS',
'media_id': media_id
}
if is_long_video:
endpoint = Endpoint.UPLOAD_MEDIA_2
else:
endpoint = Endpoint.UPLOAD_MEDIA
response = self.http.get(
Endpoint.UPLOAD_MEDIA,
endpoint,
params=params,
headers=self._base_headers
).json()
Expand Down
27 changes: 21 additions & 6 deletions twikit/twikit_async/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,8 @@ async def upload_media(
wait_for_completion: bool = False,
status_check_interval: float = 1.0,
media_type: str | None = None,
media_category: str | None = None
media_category: str | None = None,
is_long_video: bool = False
) -> str:
"""
Uploads media to twitter.
Expand All @@ -640,6 +641,9 @@ async def upload_media(
If not specified, it will be guessed from the source.
media_category : :class:`str`, default=None
The media category.
is_long_video : :class:`bool`, default=False
If this is True, videos longer than 2:20 can be uploaded.
(Twitter Premium only)
Returns
-------
Expand Down Expand Up @@ -694,6 +698,11 @@ async def upload_media(
# Checking the upload status of an image is impossible.
wait_for_completion = False

if is_long_video:
endpoint = Endpoint.UPLOAD_MEDIA_2
else:
endpoint = Endpoint.UPLOAD_MEDIA

total_bytes = len(binary)

# ============ INIT =============
Expand All @@ -705,7 +714,7 @@ async def upload_media(
if media_category is not None:
params['media_category'] = media_category
response = (await self.http.post(
Endpoint.UPLOAD_MEDIA,
endpoint,
params=params,
headers=self._base_headers
)).json()
Expand Down Expand Up @@ -736,7 +745,7 @@ async def upload_media(
}

coro = self.http.post(
Endpoint.UPLOAD_MEDIA,
endpoint,
params=params,
headers=headers,
files=files
Expand All @@ -760,7 +769,7 @@ async def upload_media(
'media_id': media_id,
}
await self.http.post(
Endpoint.UPLOAD_MEDIA,
endpoint,
params=params,
headers=self._base_headers,
)
Expand All @@ -777,7 +786,9 @@ async def upload_media(

return media_id

async def check_media_status(self, media_id: str) -> dict:
async def check_media_status(
self, media_id: str, is_long_video: bool = False
) -> dict:
"""
Check the status of uploaded media.
Expand All @@ -796,8 +807,12 @@ async def check_media_status(self, media_id: str) -> dict:
'command': 'STATUS',
'media_id': media_id
}
if is_long_video:
endpoint = Endpoint.UPLOAD_MEDIA_2
else:
endpoint = Endpoint.UPLOAD_MEDIA
response = (await self.http.get(
Endpoint.UPLOAD_MEDIA,
endpoint,
params=params,
headers=self._base_headers
)).json()
Expand Down
1 change: 1 addition & 0 deletions twikit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ class Endpoint:
DELETE_TWEET = 'https://twitter.com/i/api/graphql/VaenaVgh5q5ih7kvyVjgtg/DeleteTweet'
SEARCH_TIMELINE = 'https://twitter.com/i/api/graphql/flaR-PUMshxFWZWPNpq4zA/SearchTimeline'
UPLOAD_MEDIA = 'https://upload.twitter.com/i/media/upload.json'
UPLOAD_MEDIA_2 = 'https://upload.twitter.com/i/media/upload2.json'
CREATE_MEDIA_METADATA = 'https://api.twitter.com/1.1/media/metadata/create.json'
GUEST_TOKEN = 'https://api.twitter.com/1.1/guest/activate.json'
CREATE_CARD = 'https://caps.twitter.com/v2/cards/create.json'
Expand Down

0 comments on commit 40d7edb

Please sign in to comment.