8000 Updates Media object with new methods, adds id param, adds tests by jeremylow · Pull Request #264 · bear/python-twitter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
This repository was archived by the owner on Aug 7, 2024. It is now read-only.

Updates Media object with new methods, adds id param, adds tests #264

Merged
merged 1 commit into from
Dec 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
100 changes: 100 additions & 0 deletions tests/test_media.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import twitter
import json
import unittest


class MediaTest(unittest.TestCase):

RAW_JSON = '''{"display_url": "pic.twitter.com/lX5LVZO", "expanded_url": "http://twitter.com/fakekurrik/status/244204973972410368/photo/1", "id": 244204973989187584, "id_str": "244204973989187584", "indices": [44,63], "media_url": "http://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png", "media_url_https": "https://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png", "sizes": {"large": {"h": 175, "resize": "fit", "w": 333}, "medium": {"h": 175, "resize": "fit", "w": 333}, "small": {"h": 175, "resize": "fit", "w": 333}, "thumb": {"h": 150, "resize": "crop", "w": 150}}, "type": "photo", "url": "http://t.co/lX5LVZO"}'''
SAMPLE_JSON = '''{"display_url": "pic.twitter.com/lX5LVZO", "expanded_url": "http://twitter.com/fakekurrik/status/244204973972410368/photo/1", "id": 244204973989187584, "media_url": "http://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png", "media_url_https": "https://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png", "type": "photo", "url": "http://t.co/lX5LVZO"}'''

def _GetSampleMedia(self):
return twitter.Media(
id=244204973989187584,
expanded_url='http://twitter.com/fakekurrik/status/244204973972410368/photo/1',
display_url='pic.twitter.com/lX5LVZO',
url='http://t.co/lX5LVZO',
media_url_https='https://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png',
media_url='http://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png',
type='photo')

def testInit(self):
'''Test the twitter.Media constructor'''
media = twitter.Media(
id=244204973989187584,
display_url='pic.twitter.com/7a2z7S8tKL',
expanded_url='http://twitter.com/NASAJPL/status/672830989895254016/photo/1',
url='https://t.co/7a2z7S8tKL',
media_url_https='https://pbs.twimg.com/media/CVZgOC3UEAELUcL.jpg',
media_url='http://pbs.twimg.com/media/CVZgOC3UEAELUcL.jpg',
type='photo')

def testProperties(self):
'''Test all of the twitter.Media properties'''
media = twitter.Media()

media.id = 244204973989187584
media.display_url = 'pic.twitter.com/7a2z7S8tKL'
media.expanded_url = 'http://twitter.com/NASAJPL/status/672830989895254016/photo/1'
media.url = 'https://t.co/7a2z7S8tKL'
media.media_url_https = 'https://pbs.twimg.com/media/CVZgOC3UEAELUcL.jpg'
media.media_url = 'http://pbs.twimg.com/media/CVZgOC3UEAELUcL.jpg'
media.type = 'photo'

self.assertEqual('pic.twitter.com/7a2z7S8tKL', media.display_url)
self.assertEqual(
'http://twitter.com/NASAJPL/status/672830989895254016/photo/1',
media.expanded_url)
self.assertEqual('https://t.co/7a2z7S8tKL', media.url)
self.assertEqual(
'https://pbs.twimg.com/media/CVZgOC3UEAELUcL.jpg',
media.media_url_https)
self.assertEqual(
'http://pbs.twimg.com/media/CVZgOC3UEAELUcL.jpg',
media.media_url)
self.assertEqual('photo', media.type)

def testAsJsonString(self):
'''Test the twitter.User AsJsonString method'''
self.assertEqual(MediaTest.SAMPLE_JSON,
self._GetSampleMedia().AsJsonString())

def testAsDict(self):
'''Test the twitter.Media AsDict method'''
media = self._GetSampleMedia()
data = media.AsDict()

self.assertEqual(
'pic.twitter.com/lX5LVZO',
data['display_url'])
self.assertEqual(
'http://twitter.com/fakekurrik/status/244204973972410368/photo/1',
data['expanded_url'])
self.assertEqual('http://t.co/lX5LVZO', data['url'])
self.assertEqual(
'https://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png',
data['media_url_https'])
self.assertEqual(
'http://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png',
data['media_url'])

self.assertEqual('photo', data['type'])

def testEq(self):
'''Test the twitter.Media __eq__ method'''
media = twitter.Media()
media.id = 244204973989187584
media.display_url = 'pic.twitter.com/lX5LVZO'
media.expanded_url = 'http://twitter.com/fakekurrik/status/244204973972410368/photo/1'
media.url = 'http://t.co/lX5LVZO'
media.media_url_https = 'https://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png'
media.media_url = 'http://pbs.twimg.com/media/A2OXIUcCUAAXj9k.png'
media.type = 'photo'

self.assertEqual(media, self._GetSampleMedia())

def testNewFromJsonDict(self):
'''Test the twitter.Media NewFromJsonDict method'''
data = json.loads(MediaTest.RAW_JSON)
media = twitter.Media.NewFromJsonDict(data)
self.assertEqual(self._GetSampleMedia(), media)
53 changes: 48 additions & 5 deletions twitter/media.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python
import json


class Media(object):

"""A class representing the Media component of a tweet.

The Media structure exposes the following properties:
Expand All @@ -20,6 +22,7 @@ def __init__(self, **kwargs):
returned in a sequence.
"""
param_defaults = {
'id': None,
'expanded_url': None,
'display_url': None,
'url': None,
Expand All @@ -29,9 +32,13 @@ def __init__(self, **kwargs):
'variants': None
}

for (param, default) in param_defaults.iteritems():
for (param, default) in param_defaults.items():
setattr(self, param, kwargs.get(param, default))

@property
def Id(self):
return self.id or None

@property
def Expanded_url(self):
return self.expanded_url or False
Expand Down Expand Up @@ -59,9 +66,36 @@ def Variants(self):
def __eq__(self, other):
return other.Media_url == self.Media_url and other.Type == self.Type

def __ne__(self, other):
return not self.__eq__(other)

def __hash__(self):
return hash((self.Media_url, self.Type))

def __str__(self):
"""A string representation of this twitter.Media instance.

The return value is the same as the JSON string representation.

Returns:
A string representation of this twitter.Media instance.
"""
return self.AsJsonString()

def __repr__(self):
"""
A string representation of this twitter.Media instance.

The return value is the ID of status, username and datetime.

Returns:
Media(ID=244204973989187584, type=photo, display_url='pic.twitter.com/lX5LVZO')
"""
return "Media(Id={id}, type={type}, display_url='{url}')".format(
id=self.id,
type=self.type,
url=self.display_url)

def AsDict(self):
"""A dict representation of this twitter.Media instance.

Expand All @@ -71,6 +105,8 @@ def AsDict(self):
A dict representing this twitter.Media instance
"""
data = {}
if self.id:
data['id'] = self.id
if self.expanded_url:
data['expanded_url'] = self.expanded_url
if self.display_url:
Expand All @@ -87,7 +123,6 @@ def AsDict(self):
data['variants'] = self.variants
return data


@staticmethod
def NewFromJsonDict(data):
"""Create a new instance based on a JSON dict.
Expand All @@ -101,11 +136,19 @@ def NewFromJsonDict(data):
if 'video_info' in data:
variants = data['video_info']['variants']

return Media(expanded_url=data.get('expanded_url', None),
return Media(id=data.get('id', None),
expanded_url=data.get('expanded_url', None),
display_url=data.get('display_url', None),
url=data.get('url', None),
media_url_https=data.get('media_url_https', None),
media_url=data.get('media_url', None),
type=data.get('type', None),
variants=variants
)
variants=variants)

def AsJsonString(self):
"""A JSON string representation of this twitter.Media instance.

Returns:
A JSON string representation of this twitter.Media instance
"""
return json.dumps(self.AsDict(), sort_keys=True)
0