X Tutup
Skip to content

Commit 4050a06

Browse files
committed
Added a class for media types that come back in the tweet stream. Updated api to use these
1 parent bc0530d commit 4050a06

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

twitter/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,6 @@
3838
from .status import Status
3939
from .user import User, UserStatus
4040
from .category import Category
41+
from .media import Media
4142
from .list import List
4243
from .api import Api

twitter/api.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,6 @@ def GetHomeTimeline(self,
592592
parameters['include_entities'] = 'false'
593593
json_data = self._RequestUrl(url, 'GET', data=parameters)
594594
data = self._ParseAndCheckTwitter(json_data.content)
595-
596595
return [Status.NewFromJsonDict(x) for x in data]
597596

598597
def GetUserTimeline(self,

twitter/media.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python
2+
3+
4+
class Media(object):
5+
"""A class representing the Media component of a tweet.
6+
7+
The Media structure exposes the following properties:
8+
9+
media.expanded_url
10+
media.display_url
11+
media.url
12+
media.media_url_https
13+
media.media_url
14+
media.type
15+
"""
16+
17+
def __init__(self, **kwargs):
18+
"""An object to the information for each Media entity for a tweet
19+
This class is normally instantiated by the twitter.Api class and
20+
returned in a sequence.
21+
"""
22+
param_defaults = {
23+
'expanded_url': None,
24+
'display_url': None,
25+
'url': None,
26+
'media_url_https': None,
27+
'media_url': None,
28+
'type': None,
29+
}
30+
31+
for (param, default) in param_defaults.iteritems():
32+
setattr(self, param, kwargs.get(param, default))
33+
34+
@property
35+
def Expanded_url(self):
36+
return self.expanded_url or False
37+
38+
@property
39+
def Url(self):
40+
return self.url or False
41+
42+
@property
43+
def Media_url_https(self):
44+
return self.media_url_https or False
45+
46+
@property
47+
def Media_url(self):
48+
return self.media_url or False
49+
50+
@property
51+
def Type(self):
52+
return self.type or False
53+
54+
def __eq__(self, other):
55+
return other.Media_url == self.Media_url and other.Type == self.Type
56+
57+
def __hash__(self):
58+
return hash((self.Media_url, self.Type))
59+
60+
@staticmethod
61+
def NewFromJsonDict(data):
62+
"""Create a new instance based on a JSON dict.
63+
64+
Args:
65+
data: A JSON dict, as converted from the JSON in the twitter API
66+
Returns:
67+
A twitter.Category instance
68+
"""
69+
return Media(expanded_url=data.get('expanded_url', None),
70+
display_url=data.get('display_url', None),
71+
url=data.get('url', None),
72+
media_url_https=data.get('media_url_https', None),
73+
media_url=data.get('media_url', None),
74+
type=data.get('type', None),
75+
)

twitter/status.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
from calendar import timegm
44
import rfc822
55
import time
6+
from sets import Set
67

78
from twitter import json, Hashtag, TwitterError, Url
9+
from twitter.media import Media
810

911

1012
class Status(object):
@@ -118,7 +120,8 @@ def __init__(self, **kwargs):
118120
'media': None,
119121
'withheld_copyright': None,
120122
'withheld_in_countries': None,
121-
'withheld_scope': None}
123+
'withheld_scope': None,
124+
}
122125

123126
for (param, default) in param_defaults.iteritems():
124127
setattr(self, param, kwargs.get(param, default))
@@ -400,19 +403,19 @@ def __str__(self):
400403
return self.AsJsonString()
401404

402405
def __repr__(self):
403-
"""A string representation of this twitter.Status instance.
406+
"""A string representation of this twitter.Status instance.
404407
The return value is the ID of status, username and datetime.
405408
Returns:
406409
A string representation of this twitter.Status instance with
407410
the ID of status, username and datetime.
408411
"""
409-
if self.user:
410-
representation = "Status(ID=%s, screen_name='%s', created_at='%s')" % (
411-
self.id, self.user.screen_name, self.created_at)
412-
else:
413-
representation = "Status(ID=%s, created_at='%s')" % (
414-
self.id, self.created_at)
415-
return representation
412+
if self.user:
413+
representation = "Status(ID=%s, screen_name='%s', created_at='%s')" % (
414+
self.id, self.user.screen_name, self.created_at)
415+
else:
416+
representation = "Status(ID=%s, created_at='%s')" % (
417+
self.id, self.created_at)
418+
return representation
416419

417420
def AsJsonString(self, allow_non_ascii=False):
418421
"""A JSON string representation of this twitter.Status instance.
@@ -525,7 +528,7 @@ def NewFromJsonDict(data):
525528
urls = None
526529
user_mentions = None
527530
hashtags = None
528-
media = None
531+
media = Set()
529532
if 'entities' in data:
530533
if 'urls' in data['entities']:
531534
urls = [Url.NewFromJsonDict(u) for u in data['entities']['urls']]
@@ -536,14 +539,14 @@ def NewFromJsonDict(data):
536539
if 'hashtags' in data['entities']:
537540
hashtags = [Hashtag.NewFromJsonDict(h) for h in data['entities']['hashtags']]
538541
if 'media' in data['entities']:
539-
media = data['entities']['media']
540-
else:
541-
media = []
542+
for m in data['entities']['media']:
543+
media.add(Media.NewFromJsonDict(m))
542544

543545
# the new extended entities
544546
if 'extended_entities' in data:
545547
if 'media' in data['extended_entities']:
546-
media = [m for m in data['extended_entities']['media']]
548+
for m in data['extended_entities']['media']:
549+
media.add(Media.NewFromJsonDict(m))
547550

548551
return Status(created_at=data.get('created_at', None),
549552
favorited=data.get('favorited', None),
@@ -574,4 +577,5 @@ def NewFromJsonDict(data):
574577
scopes=data.get('scopes', None),
575578
withheld_copyright=data.get('withheld_copyright', None),
576579
withheld_in_countries=data.get('withheld_in_countries', None),
577-
withheld_scope=data.get('withheld_scope', None))
580+
withheld_scope=data.get('withheld_scope', None),
581+
)

0 commit comments

Comments
 (0)
X Tutup