Created
June 13, 2013 23:40
-
-
Save chagan/5778360 to your computer and use it in GitHub Desktop.
Add oembed support to python twitter. Add class anywhere, add GetOembed to API Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Oembed(object): | |
'''A class representing an oembed for a tweet''' | |
def __init__(self, | |
html=None, | |
author_name=None, | |
provider_url=None, | |
url=None, | |
provider_name=None, | |
version=None, | |
type=None, | |
height=None, | |
cache_age=None, | |
author_url=None, | |
width=None): | |
self.html = html | |
self.author_name = author_name | |
self.provider_url = provider_url, | |
self.url = url, | |
self.provider_name = provider_name, | |
self.version = version, | |
self.type = type, | |
self.height = height, | |
self.cache_age = cache_age, | |
self.author_url = author_url, | |
self.width = width | |
def GetHTML(self): | |
return self._html | |
def GetAuthorName(self): | |
return self._author_name | |
def GetProviderUrl(self): | |
return self._provider_url | |
def GetUrl(self): | |
return self._url | |
def GetProviderName(self): | |
return self._provider_name | |
def GetVersion(self): | |
return self._version | |
def GetType(self): | |
return self._type | |
def GetHeight(self): | |
return self._height | |
def GetCacheAge(self): | |
return self._cache_age | |
def GetAuthorUrl(self): | |
return self._author_url | |
def GetWidth(self): | |
return self._width | |
def GetHeight(self): | |
return self._height | |
@staticmethod | |
def NewFromJsonDict(data): | |
'''Create a new instance based on a JSON dict. | |
Args: | |
data: | |
A JSON dict, as converted from the JSON in the twitter API | |
Returns: | |
A twitter.Oembed instance | |
''' | |
return Oembed(html=data.get('html', None), | |
author_name=data.get('author_name', None), | |
provider_url=data.get('provider_url', None), | |
url=data.get('url', None), | |
provider_name=data.get('provider_name', None), | |
version=data.get('version', None), | |
type=data.get('type', None), | |
height=data.get('height', None), | |
cache_age=data.get('cache_age', None), | |
author_url=data.get('author_url', None), | |
width=data.get('width', None)) | |
def GetOembed(self, | |
id=None, | |
url=None, | |
maxwidth=None, | |
hide_media=None, | |
hide_thread=None, | |
omit_script=None, | |
align=None, | |
related=None, | |
lang=None,): | |
'''Returns the oembed info for a given tweet | |
Args: | |
id: | |
The Tweet/status ID to return embed code for. | |
url: | |
The URL of the Tweet/status to be embedded. | |
maxwidth: | |
The maximum width in pixels that the embed should be rendered at. This value is constrained to be between 250 and 550 pixels. | |
hide_media: | |
Specifies whether the embedded Tweet should automatically expand images which were uploaded via POST statuses/update_with_media. When set to either true, t or 1 images will not be expanded. Defaults to false. | |
hide_thread: | |
Specifies whether the embedded Tweet should automatically expand images which were uploaded via POST statuses/update_with_media. When set to either true, t or 1 images will not be expanded. Defaults to false. | |
omit_script: | |
Specifies whether the embedded Tweet HTML should include a <script> element pointing to widgets.js. In cases where a page already includes widgets.js, setting this value to true will prevent a redundant script element from being included. When set to either true, t or 1 the <script> element will not be included in the embed HTML, meaning that pages must include a reference to widgets.js manually. Defaults to false. | |
align: | |
Specifies whether the embedded Tweet should be left aligned, right aligned, or centered in the page. Valid values are left, right, center, and none. Defaults to none, meaning no alignment styles are specified for the Tweet. | |
related: | |
A value for the TWT related parameter, as described in Web Intents. This value will be forwarded to all Web Intents calls. | |
lang: | |
Language code for the rendered embed. This will affect the text and localization of the rendered HTML. | |
Returns: | |
A twitter.Oembed instance. | |
''' | |
url = '%s/statuses/oembed.json' % self.base_url | |
if not self._oauth_consumer: | |
raise TwitterError("The twitter.Api instance must be authenticated.") | |
parameters = {} | |
if id: | |
parameters['id'] = id | |
if url: | |
parameters['url'] = url | |
if maxwidth: | |
parameters['maxwidth'] = maxwidth | |
if hide_media: | |
parameters['hide_media'] = hide_media | |
if omit_script: | |
parameters['omit_script'] = omit_script | |
if align: | |
parameters['align'] = align | |
if related: | |
parameters['related'] = related | |
if lang: | |
parameters['lang'] = lang | |
json = self._FetchUrl(url, parameters=parameters) | |
data = self._ParseAndCheckTwitter(json) | |
return Oembed.NewFromJsonDict(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment