Skip to content

Commit

Permalink
Add /media/subtitles/create and delete endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
danielthepope committed Nov 24, 2019
1 parent bbabc8f commit 4913e44
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tests/test_api_30.py
Expand Up @@ -1548,6 +1548,87 @@ def testPostUploadMediaChunkedFinalize(self):
self.assertEqual(len(responses.calls), 1)
self.assertTrue(resp)

@responses.activate
def testPostMediaSubtitlesCreateSuccess(self):
responses.add(
POST,
'https://upload.twitter.com/1.1/media/subtitles/create.json',
body=b'',
status=200)
expected_body = {
'media_id': '1234',
'media_category': 'TweetVideo',
'subtitle_info': {
'subtitles': [{
'media_id': '5678',
'language_code': 'en',
'display_name': 'English'
}]
}
}
resp = self.api.PostMediaSubtitlesCreate(video_media_id=1234,
subtitle_media_id=5678,
language_code='en',
display_name='English')

self.assertEqual(len(responses.calls), 1)
self.assertEqual(responses.calls[0].request.url,
'https://upload.twitter.com/1.1/media/subtitles/create.json')
request_body = json.loads(responses.calls[0].request.body.decode('utf-8'))
self.assertTrue(resp)
self.assertDictEqual(expected_body, request_body)

@responses.activate
def testPostMediaSubtitlesCreateFailure(self):
responses.add(
POST,
'https://upload.twitter.com/1.1/media/subtitles/create.json',
body=b'{"error":"Some error happened"}',
status=400)
self.assertRaises(
twitter.TwitterError,
lambda: self.api.PostMediaSubtitlesCreate(video_media_id=1234,
subtitle_media_id=5678,
language_code='en',
display_name='English'))

@responses.activate
def testPostMediaSubtitlesDeleteSuccess(self):
responses.add(
POST,
'https://upload.twitter.com/1.1/media/subtitles/delete.json',
body=b'',
status=200)
expected_body = {
'media_id': '1234',
'media_category': 'TweetVideo',
'subtitle_info': {
'subtitles': [{
'language_code': 'en'
}]
}
}
resp = self.api.PostMediaSubtitlesDelete(video_media_id=1234, language_code='en')

self.assertEqual(len(responses.calls), 1)
self.assertEqual(responses.calls[0].request.url,
'https://upload.twitter.com/1.1/media/subtitles/delete.json')
request_body = json.loads(responses.calls[0].request.body.decode('utf-8'))
self.assertTrue(resp)
self.assertDictEqual(expected_body, request_body)

@responses.activate
def testPostMediaSubtitlesDeleteFailure(self):
responses.add(
POST,
'https://upload.twitter.com/1.1/media/subtitles/delete.json',
body=b'{"error":"Some error happened"}',
status=400)
self.assertRaises(
twitter.TwitterError,
lambda: self.api.PostMediaSubtitlesDelete(video_media_id=1234,
language_code='en'))

@responses.activate
def testGetUserSuggestionCategories(self):
with open('testdata/get_user_suggestion_categories.json') as f:
Expand Down
75 changes: 75 additions & 0 deletions twitter/api.py
Expand Up @@ -1424,6 +1424,81 @@ def UploadMediaChunked(self,
except KeyError:
raise TwitterError('Media could not be uploaded.')

def PostMediaSubtitlesCreate(self,
video_media_id,
subtitle_media_id,
language_code,
display_name):
"""Associate uploaded subtitles to an uploaded video. You can associate
subtitles to a video before or after Tweeting.
Args:
video_media_id (int):
Media ID of the uploaded video to add the subtitles to. The
video must have been uploaded using the category 'TweetVideo'.
subtitle_media_id (int):
Media ID of the uploaded subtitle file. The subtitles myst have
been uploaded using the category 'Subtitles'.
language_code (str):
The language code that the subtitles are written in. The
language code should be a BCP47 code (e.g. 'en', 'sp')
display_name (str):
Language name (e.g. 'English', 'Spanish')
Returns:
True if successful. Raises otherwise.
"""
url = '%s/media/subtitles/create.json' % self.upload_url

subtitle = {}
subtitle['media_id'] = str(subtitle_media_id)
subtitle['language_code'] = language_code
subtitle['display_name'] = display_name
parameters = {}
parameters['media_id'] = str(video_media_id)
parameters['media_category'] = 'TweetVideo'
parameters['subtitle_info'] = {}
parameters['subtitle_info']['subtitles'] = [subtitle]

resp = self._RequestUrl(url, 'POST', json=parameters)
# Response body should be blank, so only do error checking if the response is not blank.
if resp.content.decode('utf-8'):
self._ParseAndCheckTwitter(resp.content.decode('utf-8'))

return True

def PostMediaSubtitlesDelete(self,
video_media_id,
language_code):
"""Remove subtitles from an uploaded video.
Args:
video_media_id (int):
Media ID of the video for which the subtitles will be removed.
language_code (str):
The language code of the subtitle file that should be deleted.
The language code should be a BCP47 code (e.g. 'en', 'sp')
Returns:
True if successful. Raises otherwise.
"""
url = '%s/media/subtitles/delete.json' % self.upload_url

subtitle = {}
subtitle['language_code'] = language_code
parameters = {}
parameters['media_id'] = str(video_media_id)
parameters['media_category'] = 'TweetVideo'
parameters['subtitle_info'] = {}
parameters['subtitle_info']['subtitles'] = [subtitle]

resp = self._RequestUrl(url, 'POST', json=parameters)
# Response body should be blank, so only do error checking if the response is not blank.
if resp.content.decode('utf-8'):
self._ParseAndCheckTwitter(resp.content.decode('utf-8'))

return True

def _TweetTextWrap(self,
status,
char_lim=CHARACTER_LIMIT):
Expand Down

0 comments on commit 4913e44

Please sign in to comment.