api.py 9.63 KB
Newer Older
1
2
3
4
# Tweepy
# Copyright 2009 Joshua Roesslein
# See LICENSE

Josh Roesslein's avatar
Josh Roesslein committed
5
import base64
6

7
from binder import bind_api
Josh Roesslein's avatar
Josh Roesslein committed
8
from parsers import *
9
from models import User, Status, DirectMessage, Friendship, SavedSearch
Josh Roesslein's avatar
Josh Roesslein committed
10
from error import TweepError
11

12
"""Twitter API"""
13
14
class API(object):

15
  def __init__(self, username=None, password=None, host='twitter.com', secure=False,
16
                classes={'user': User, 'status': Status,
17
18
                'direct_message': DirectMessage, 'friendship': Friendship,
                'saved_search': SavedSearch}):
Josh Roesslein's avatar
Josh Roesslein committed
19
    if username and password:
20
      self.set_credentials(username, password)
21
22
23
24
25
    else:
      self._b64up = None
    self.host = host
    self.secure = secure
    self.classes = classes
26
    self.username = username
27

Josh Roesslein's avatar
Josh Roesslein committed
28
  def set_credentials(self, username, password):
29
30
31
    self._b64up = base64.b64encode('%s:%s' % (username, password))
    self.username = username

32
  """Get public timeline"""
33
34
  public_timeline = bind_api(
      path = '/statuses/public_timeline.json',
35
36
37
38
39
40
41
42
43
44
45
      parser = parse_statuses,
      allowed_param = []
  )

  """Get friends timeline"""
  friends_timeline = bind_api(
      path = '/statuses/friends_timeline.json',
      parser = parse_statuses,
      allowed_param = ['since_id', 'max_id', 'count', 'page'],
      require_auth = True
  )
46
47
48
49
50
51
52
53
54

  """Get user timeline"""
  user_timeline = bind_api(
      path = '/statuses/user_timeline.json',
      parser = parse_statuses,
      allowed_param = ['id', 'user_id', 'screen_name', 'since_id',
                        'max_id', 'count', 'page']
  )

Josh Roesslein's avatar
Josh Roesslein committed
55
56
57
58
59
60
61
62
  """Get mentions"""
  mentions = bind_api(
      path = '/statuses/mentions.json',
      parser = parse_statuses,
      allowed_param = ['since_id', 'max_id', 'count', 'page'],
      require_auth = True
  )

Josh Roesslein's avatar
Josh Roesslein committed
63
64
65
66
67
68
69
  """Show status"""
  get_status = bind_api(
      path = '/statuses/show.json',
      parser = parse_status,
      allowed_param = ['id']
  )

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
  """Update status"""
  update_status = bind_api(
      path = '/statuses/update.json',
      method = 'POST',
      parser = parse_status,
      allowed_param = ['status', 'in_reply_to_status_id'],
      require_auth = True
  )

  """Destroy status"""
  destroy_status = bind_api(
      path = '/statuses/destroy.json',
      method = 'DELETE',
      parser = parse_status,
      allowed_param = ['id'],
      require_auth = True
  )

88
89
90
91
92
93
94
  """Show user"""
  get_user = bind_api(
      path = '/users/show.json',
      parser = parse_user,
      allowed_param = ['id', 'user_id', 'screen_name']
  )

95
96
97
98
99
100
101
  """Get authenticated user"""
  def me(self):
    if self.username:
      return self.get_user(screen_name=self.username)
    else:
      return None

102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  """Show friends"""
  friends = bind_api(
      path = '/statuses/friends.json',
      parser = parse_users,
      allowed_param = ['id', 'user_id', 'screen_name', 'page']
  )

  """Show followers"""
  followers = bind_api(
      path = '/statuses/followers.json',
      parser = parse_users,
      allowed_param = ['id', 'user_id', 'screen_name', 'page'],
      require_auth = True
  )

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  """Get direct messages"""
  direct_messages = bind_api(
      path = '/direct_messages.json',
      parser = parse_directmessages,
      allowed_param = ['since_id', 'max_id', 'count', 'page'],
      require_auth = True
  )

  """Sent direct messages"""
  sent_direct_messages = bind_api(
      path = '/direct_messages/sent.json',
      parser = parse_directmessages,
      allowed_param = ['since_id', 'max_id', 'count', 'page'],
      require_auth = True
  )

  """Send direct message"""
  send_direct_message = bind_api(
      path = '/direct_messages/new.json',
      method = 'POST',
      parser = parse_dm,
      allowed_param = ['user', 'text'],
      require_auth = True
  )

  """Destroy direct message"""
  destroy_direct_message = bind_api(
      path = '/direct_messages/destroy.json',
      method = 'DELETE',
      parser = parse_dm,
      allowed_param = ['id'],
      require_auth = True
  )

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  """Create friendship"""
  create_friendship = bind_api(
      path = '/friendships/create.json',
      method = 'POST',
      parser = parse_user,
      allowed_param = ['id', 'user_id', 'screen_name', 'follow'],
      require_auth = True
  )

  """Destroy friendship"""
  destroy_friendship = bind_api(
      path = '/friendships/destroy.json',
      method = 'DELETE',
      parser = parse_user,
      allowed_param = ['id', 'user_id', 'screen_name'],
      require_auth = True
  )

  """Check if friendship exists"""
  exists_friendship = bind_api(
      path = '/friendships/exists.json',
172
      parser = parse_json,
173
174
175
176
177
178
179
180
181
182
183
      allowed_param = ['user_a', 'user_b']
  )

  """Show friendship details"""
  show_friendship = bind_api(
      path = '/friendships/show.json',
      parser = parse_friendship,
      allowed_param = ['source_id', 'source_screen_name',
                        'target_id', 'target_screen_name']
  )

184
185
186
  """Get list of IDs of users the specified user is following"""
  friends_ids = bind_api(
      path = '/friends/ids.json',
187
      parser = parse_json,
188
189
190
191
192
193
      allowed_param = ['id', 'user_id', 'screen_name', 'page']
  )

  """Get list of IDs of users following the specified user"""
  followers_ids = bind_api(
      path = '/followers/ids.json',
194
      parser = parse_json,
195
196
197
      allowed_param = ['id', 'user_id', 'screen_name', 'page']
  )

198
  """Verify credentials"""
Josh Roesslein's avatar
Josh Roesslein committed
199
200
201
202
  def verify_credentials(self):
    try:
      return bind_api(
          path = '/account/verify_credentials.json',
203
          parser = parse_return_true,
Josh Roesslein's avatar
Josh Roesslein committed
204
205
206
          require_auth = True)(self)
    except TweepError:
      return False
207
208
209
210

  """Rate limit status"""
  rate_limit_status = bind_api(
      path = '/account/rate_limit_status.json',
211
      parser = parse_json
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  )

  """Update delivery device"""
  set_delivery_device = bind_api(
      path = '/account/update_delivery_device.json',
      method = 'POST',
      allowed_param = ['device'],
      parser = parse_user,
      require_auth = True
  )

  """Update profile colors"""
  update_profile_colors = bind_api(
      path = '/account/update_profile_colors.json',
      method = 'POST',
      parser = parse_user,
      allowed_param = ['profile_background_color', 'profile_text_color',
                        'profile_link_color', 'profile_sidebar_fill_color',
                        'profile_sidebar_border_color'],
      require_auth = True
  )

234
  # TODO: add support for changing profile and background images
235
236
237
238
239
240
241
242
243
244

  """Update profile"""
  update_profile = bind_api(
      path = '/account/update_profile.json',
      method = 'POST',
      parser = parse_user,
      allowed_param = ['name', 'email', 'url', 'location', 'description'],
      require_auth = True
  )

245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  """Get favorites"""
  favorites = bind_api(
      path = '/favorites.json',
      parser = parse_statuses,
      allowed_param = ['id', 'page']
  )

  """Create favorite"""
  create_favorite = bind_api(
      path = '/favorites/create.json',
      method = 'POST',
      parser = parse_status,
      allowed_param = ['id'],
      require_auth = True
  )

  """Destroy favorite"""
  destroy_favorite = bind_api(
      path = '/favorites/destroy.json',
      method = 'DELETE',
      parser = parse_status,
      allowed_param = ['id'],
      require_auth = True
  )
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287

  """Enable device notifications"""
  enable_notifications = bind_api(
      path = '/notifications/follow.json',
      method = 'POST',
      parser = parse_user,
      allowed_param = ['id', 'user_id', 'screen_name'],
      require_auth = True
  )

  """Disable device notifications"""
  disable_notifications = bind_api(
      path = '/notifications/leave.json',
      method = 'POST',
      parser = parse_user,
      allowed_param = ['id', 'user_id', 'screen_name'],
      require_auth = True
  )

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
  """Create a block"""
  create_block = bind_api(
      path = '/blocks/create.json',
      method = 'POST',
      parser = parse_user,
      allowed_param = ['id'],
      require_auth = True
  )

  """Destroy a block"""
  destroy_block = bind_api(
      path = '/blocks/destroy.json',
      method = 'DELETE',
      parser = parse_user,
      allowed_param = ['id'],
      require_auth = True
  )

  """Check if block exists"""
  def exists_block(self, **kargs):
308
309
310
311
312
313
314
315
316
317
    try:
      bind_api(
          path = '/blocks/exists.json',
          parser = parse_none,
          allowed_param = ['id', 'user_id', 'screen_name'],
          require_auth = True
    )(self, **kargs)
    except TweepError:
      return False
    return True
318

319
320
321
322
323
324
325
326
  """Get list of users that are blocked"""
  blocks = bind_api(
      path = '/blocks/blocking.json',
      parser = parse_users,
      allowed_param = ['page'],
      require_auth = True
  )

327
  """Get list of ids of users that are blocked"""
328
329
  blocks_ids = bind_api(
      path = '/blocks/blocking/ids.json',
330
      parser = parse_json,
331
332
333
      require_auth = True
  )

334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  """Get list of saved searches"""
  saved_searches = bind_api(
      path = '/saved_searches.json',
      parser = parse_saved_searches,
      require_auth = True
  )

  """Get a single saved search by id"""
  def get_saved_search(self, id):
    return bind_api(
        path = '/saved_searches/show/%s.json' % id,
        parser = parse_saved_search,
        require_auth = True
    )(self)

  """Create new saved search"""
  create_saved_search = bind_api(
      path = '/saved_searches/create.json',
      method = 'POST',
      parser = parse_saved_search,
      allowed_param = ['query'],
      require_auth = True
  )

  """Destroy a saved search"""
  def destroy_saved_search(self, id):
    return bind_api(
        path = '/saved_searches/destroy/%s.json' % id,
        method = 'DELETE',
        parser = parse_saved_search,
        allowed_param = ['id'],
        require_auth = True
    )(self)

368
369
370
371
372
373
  def test(self):
    return bind_api(
        path = '/help/test.json',
        parser = parse_return_true
    )(self)