X Tutup
Skip to content

Commit 22e3ffe

Browse files
committed
first pass at unified models
1 parent 80b747b commit 22e3ffe

File tree

3 files changed

+253
-10
lines changed

3 files changed

+253
-10
lines changed

twitter/__init__.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@
3838

3939
from ._file_cache import _FileCache # noqa
4040
from .error import TwitterError # noqa
41-
from .direct_message import DirectMessage # noqa
42-
from .hashtag import Hashtag # noqa
4341
from .parse_tweet import ParseTweet # noqa
44-
from .trend import Trend # noqa
45-
from .url import Url # noqa
42+
43+
from .models import (
44+
Category,
45+
DirectMessage,
46+
Hashtag,
47+
List,
48+
Media,
49+
Trend,
50+
Url,
51+
User,
52+
UserStatus,
53+
)
54+
4655
from .status import Status # noqa
47-
from .user import User, UserStatus # noqa
48-
from .category import Category # noqa
49-
from .media import Media # noqa
50-
from .list import List # noqa
5156
from .api import Api # noqa

twitter/api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
from urllib import __version__ as urllib_version
4545

4646
from twitter import (__version__, _FileCache, json, DirectMessage, List,
47-
Status, Trend, TwitterError, User, UserStatus)
48-
from twitter.category import Category
47+
Status, Trend, TwitterError, User, UserStatus, Category)
4948

5049
from twitter.ratelimit import RateLimit
5150

twitter/models.py

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
import json
2+
3+
4+
class TwitterModel(object):
5+
6+
""" Base class from which all twitter models will inherit. """
7+
8+
def __init__(self, **kwargs):
9+
self.param_defaults = {}
10+
11+
def __str__(self):
12+
return self.AsJsonString()
13+
14+
def __eq__(self, other):
15+
return other and \
16+
self.AsDict() == other.AsDict()
17+
18+
def __ne__(self, other):
19+
return not self.__eq__(other)
20+
21+
def AsJsonString(self):
22+
return json.dumps(self.AsDict(), sort_keys=True)
23+
24+
def AsDict(self):
25+
data = {}
26+
for (key, value) in self.param_defaults.items():
27+
if getattr(getattr(self, key, None), 'AsDict', None):
28+
data[key] = getattr(self, key).AsDict()
29+
elif getattr(self, key, None):
30+
data[key] = getattr(self, key, None)
31+
return data
32+
33+
@classmethod
34+
def NewFromJsonDict(cls, data, **kwargs):
35+
""" Create a new instance based on a JSON dict.
36+
37+
Args:
38+
data: A JSON dict, as converted from the JSON in the twitter API
39+
40+
Returns:
41+
A twitter.Media instance
42+
"""
43+
44+
if kwargs:
45+
for key, val in kwargs.items():
46+
data[key] = val
47+
48+
return cls(**data)
49+
50+
51+
class Media(TwitterModel):
52+
53+
"""A class representing the Media component of a tweet. """
54+
55+
def __init__(self, **kwargs):
56+
self.param_defaults = {
57+
'id': None,
58+
'expanded_url': None,
59+
'display_url': None,
60+
'url': None,
61+
'media_url_https': None,
62+
'media_url': None,
63+
'type': None,
64+
}
65+
66+
for (param, default) in self.param_defaults.items():
67+
setattr(self, param, kwargs.get(param, default))
68+
69+
def __repr__(self):
70+
""" Representation of this twitter.Media instance.
71+
72+
Returns:
73+
Media(ID=XXX, type=YYY, display_url='ZZZ')
74+
"""
75+
return "Media(ID={mid}, Type={type}, DisplayURL='{url}')".format(
76+
mid=self.id,
77+
type=self.type,
78+
url=self.display_url)
79+
80+
81+
class List(TwitterModel):
82+
83+
"""A class representing the List structure used by the twitter API. """
84+
85+
def __init__(self, **kwargs):
86+
self.param_defaults = {
87+
'id': None,
88+
'name': None,
89+
'slug': None,
90+
'description': None,
91+
'full_name': None,
92+
'mode': None,
93+
'uri': None,
94+
'member_count': None,
95+
'subscriber_count': None,
96+
'following': None,
97+
'user': None}
98+
99+
for (param, default) in self.param_defaults.items():
100+
setattr(self, param, kwargs.get(param, default))
101+
102+
103+
class Category(TwitterModel):
104+
105+
"""A class representing the suggested user category structure. """
106+
107+
def __init__(self, **kwargs):
108+
self.param_defaults = {
109+
'name': None,
110+
'slug': None,
111+
'size': None,
112+
}
113+
114+
for (param, default) in self.param_defaults.iteritems():
115+
setattr(self, param, kwargs.get(param, default))
116+
117+
118+
class DirectMessage(TwitterModel):
119+
120+
"""A class representing a Direct Message. """
121+
122+
def __init__(self, **kwargs):
123+
self.param_defaults = {
124+
'id': None,
125+
'created_at': None,
126+
'sender_id': None,
127+
'sender_screen_name': None,
128+
'recipient_id': None,
129+
'recipient_screen_name': None,
130+
'text': None}
131+
132+
for (param, default) in self.param_defaults.items():
133+
setattr(self, param, kwargs.get(param, default))
134+
135+
136+
class Trend(TwitterModel):
137+
138+
""" A class representing a trending topic. """
139+
140+
def __init__(self, **kwargs):
141+
self.param_defaults = {
142+
'name': None,
143+
'query': None,
144+
'timestamp': None,
145+
'url': None}
146+
147+
for (param, default) in self.param_defaults.items():
148+
setattr(self, param, kwargs.get(param, default))
149+
150+
151+
class Hashtag(TwitterModel):
152+
153+
""" A class representing a twitter hashtag. """
154+
155+
def __init__(self, **kwargs):
156+
self.param_defaults = {
157+
'text': None
158+
}
159+
160+
for (param, default) in self.param_defaults.items():
161+
setattr(self, param, kwargs.get(param, default))
162+
163+
164+
class Url(TwitterModel):
165+
166+
""" A class representing an URL contained in a tweet. """
167+
168+
def __init__(self, **kwargs):
169+
self.param_defaults = {
170+
'url': None,
171+
'expanded_url': None}
172+
173+
for (param, default) in self.param_defaults.items():
174+
setattr(self, param, kwargs.get(param, default))
175+
176+
177+
class UserStatus(TwitterModel):
178+
179+
""" A class representing the UserStatus structure. This is an abbreviated
180+
form of the twitter.User object. """
181+
182+
def __init__(self, **kwargs):
183+
self.param_defaults = {
184+
'name': None,
185+
'id': None,
186+
'id_str': None,
187+
'screen_name': None,
188+
'following': None,
189+
'followed_by': None}
190+
191+
for (param, default) in self.param_defaults.items():
192+
setattr(self, param, kwargs.get(param, default))
193+
194+
195+
class User(TwitterModel):
196+
197+
"""A class representing the User structure. """
198+
199+
def __init__(self, **kwargs):
200+
self.param_defaults = {
201+
'id': None,
202+
'name': None,
203+
'screen_name': None,
204+
'location': None,
205+
'description': None,
206+
'default_profile': None,
207+
'default_profile_image': None,
208+
'profile_image_url': None,
209+
'profile_background_tile': None,
210+
'profile_background_image_url': None,
211+
'profile_banner_url': None,
212+
'profile_sidebar_fill_color': None,
213+
'profile_background_color': None,
214+
'profile_link_color': None,
215+
'profile_text_color': None,
216+
'protected': None,
217+
'utc_offset': None,
218+
'time_zone': None,
219+
'followers_count': None,
220+
'friends_count': None,
221+
'statuses_count': None,
222+
'favourites_count': None,
223+
'url': None,
224+
'status': None,
225+
'geo_enabled': None,
226+
'verified': None,
227+
'lang': None,
228+
'notifications': None,
229+
'contributors_enabled': None,
230+
'created_at': None,
231+
'listed_count': None}
232+
233+
for (param, default) in self.param_defaults.items():
234+
setattr(self, param, kwargs.get(param, default))
235+
236+
def __repr__(self):
237+
return "User(ID={uid}, Screenname={sn})".format(
238+
uid=self.id,
239+
sn=self.screen_name)

0 commit comments

Comments
 (0)
X Tutup