api.py 21.5 KB
Newer Older
1
# Tweepy
2
3
# Copyright 2009-2010 Joshua Roesslein
# See LICENSE for details.
4

5
6
7
import os
import mimetypes

Josh Roesslein's avatar
Josh Roesslein committed
8
9
from tweepy.binder import bind_api
from tweepy.error import TweepError
10
from tweepy.parsers import ModelParser
11
from tweepy.utils import list_to_csv
12

Josh Roesslein's avatar
Josh Roesslein committed
13

14
class API(object):
Josh Roesslein's avatar
Josh Roesslein committed
15
16
    """Twitter API"""

17
18
19
    def __init__(self, auth_handler=None,
            host='api.twitter.com', search_host='search.twitter.com',
             cache=None, secure=False, api_root='/1', search_root='',
20
            retry_count=0, retry_delay=0, retry_errors=None,
21
            parser=None):
Josh Roesslein's avatar
Josh Roesslein committed
22
        self.auth = auth_handler
Josh Roesslein's avatar
Josh Roesslein committed
23
        self.host = host
24
        self.search_host = search_host
Josh Roesslein's avatar
Josh Roesslein committed
25
        self.api_root = api_root
26
        self.search_root = search_root
Josh Roesslein's avatar
Josh Roesslein committed
27
28
        self.cache = cache
        self.secure = secure
29
30
        self.retry_count = retry_count
        self.retry_delay = retry_delay
31
        self.retry_errors = retry_errors
32
        self.parser = parser or ModelParser()
Josh Roesslein's avatar
Josh Roesslein committed
33

34
    """ statuses/public_timeline """
Josh Roesslein's avatar
Josh Roesslein committed
35
36
    public_timeline = bind_api(
        path = '/statuses/public_timeline.json',
37
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
38
39
40
        allowed_param = []
    )

Ferenc Szalai's avatar
Ferenc Szalai committed
41
    """ statuses/home_timeline """
Josh Roesslein's avatar
Josh Roesslein committed
42
43
    home_timeline = bind_api(
        path = '/statuses/home_timeline.json',
44
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
45
46
47
48
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

49
    """ statuses/friends_timeline """
Josh Roesslein's avatar
Josh Roesslein committed
50
51
    friends_timeline = bind_api(
        path = '/statuses/friends_timeline.json',
52
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
53
54
55
56
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

57
    """ statuses/user_timeline """
Josh Roesslein's avatar
Josh Roesslein committed
58
59
    user_timeline = bind_api(
        path = '/statuses/user_timeline.json',
60
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
61
62
63
64
        allowed_param = ['id', 'user_id', 'screen_name', 'since_id',
                          'max_id', 'count', 'page']
    )

65
    """ statuses/mentions """
Josh Roesslein's avatar
Josh Roesslein committed
66
67
    mentions = bind_api(
        path = '/statuses/mentions.json',
68
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
69
70
71
72
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    """/statuses/:id/retweeted_by.format"""
    retweeted_by = bind_api(
        path = '/statuses/{id}/retweeted_by.json',
        payload_type = 'status', payload_list = True,
        allowed_param = ['id', 'count', 'page'],
        require_auth = True
    )

    """/statuses/:id/retweeted_by/ids.format"""
    retweeted_by_ids = bind_api(
        path = '/statuses/{id}/retweeted_by/ids.json',
        payload_type = 'ids',
        allowed_param = ['id', 'count', 'page'],
        require_auth = True
    )

Ferenc Szalai's avatar
Ferenc Szalai committed
89
    """ statuses/retweeted_by_me """
Josh Roesslein's avatar
Josh Roesslein committed
90
91
    retweeted_by_me = bind_api(
        path = '/statuses/retweeted_by_me.json',
92
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
93
94
95
96
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

97
    """ statuses/retweeted_to_me """
Josh Roesslein's avatar
Josh Roesslein committed
98
99
    retweeted_to_me = bind_api(
        path = '/statuses/retweeted_to_me.json',
100
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
101
102
103
104
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

105
    """ statuses/retweets_of_me """
Josh Roesslein's avatar
Josh Roesslein committed
106
107
    retweets_of_me = bind_api(
        path = '/statuses/retweets_of_me.json',
108
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
109
110
111
112
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

113
    """ statuses/show """
Josh Roesslein's avatar
Josh Roesslein committed
114
115
    get_status = bind_api(
        path = '/statuses/show.json',
116
        payload_type = 'status',
Josh Roesslein's avatar
Josh Roesslein committed
117
118
119
        allowed_param = ['id']
    )

Ferenc Szalai's avatar
Ferenc Szalai committed
120
    """ statuses/update """
Josh Roesslein's avatar
Josh Roesslein committed
121
122
123
    update_status = bind_api(
        path = '/statuses/update.json',
        method = 'POST',
124
        payload_type = 'status',
125
        allowed_param = ['status', 'in_reply_to_status_id', 'lat', 'long', 'source', 'place_id'],
Josh Roesslein's avatar
Josh Roesslein committed
126
127
128
        require_auth = True
    )

129
    """ statuses/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
130
131
132
    destroy_status = bind_api(
        path = '/statuses/destroy.json',
        method = 'DELETE',
133
        payload_type = 'status',
Josh Roesslein's avatar
Josh Roesslein committed
134
135
136
137
        allowed_param = ['id'],
        require_auth = True
    )

Ferenc Szalai's avatar
Ferenc Szalai committed
138
    """ statuses/retweet """
139
140
141
    retweet = bind_api(
        path = '/statuses/retweet/{id}.json',
        method = 'POST',
142
        payload_type = 'status',
143
144
145
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
146

Ferenc Szalai's avatar
Ferenc Szalai committed
147
    """ statuses/retweets """
148
149
    retweets = bind_api(
        path = '/statuses/retweets/{id}.json',
150
        payload_type = 'status', payload_list = True,
151
152
153
        allowed_param = ['id', 'count'],
        require_auth = True
    )
154

155
    """ users/show """
Josh Roesslein's avatar
Josh Roesslein committed
156
157
    get_user = bind_api(
        path = '/users/show.json',
158
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
159
160
161
        allowed_param = ['id', 'user_id', 'screen_name']
    )

162
163
164
165
166
167
168
169
170
171
172
    """ Perform bulk look up of users from user ID or screenname """
    def lookup_users(self, user_ids=None, screen_names=None):
        return self._lookup_users(list_to_csv(user_ids), list_to_csv(screen_names))

    _lookup_users = bind_api(
        path = '/users/lookup.json',
        payload_type = 'user', payload_list = True,
        allowed_param = ['user_id', 'screen_name'],
        require_auth = True
    )

173
    """ Get the authenticated user """
Josh Roesslein's avatar
Josh Roesslein committed
174
    def me(self):
Josh Roesslein's avatar
Josh Roesslein committed
175
        return self.get_user(screen_name=self.auth.get_username())
Josh Roesslein's avatar
Josh Roesslein committed
176

177
178
179
    """ users/search """
    search_users = bind_api(
        path = '/users/search.json',
180
        payload_type = 'user', payload_list = True,
181
182
183
184
        require_auth = True,
        allowed_param = ['q', 'per_page', 'page']
    )

185
    """ statuses/friends """
Josh Roesslein's avatar
Josh Roesslein committed
186
187
    friends = bind_api(
        path = '/statuses/friends.json',
188
        payload_type = 'user', payload_list = True,
189
        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
190
191
    )

192
    """ statuses/followers """
Josh Roesslein's avatar
Josh Roesslein committed
193
194
    followers = bind_api(
        path = '/statuses/followers.json',
195
        payload_type = 'user', payload_list = True,
196
        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
197
198
    )

199
    """ direct_messages """
Josh Roesslein's avatar
Josh Roesslein committed
200
201
    direct_messages = bind_api(
        path = '/direct_messages.json',
202
        payload_type = 'direct_message', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
203
204
205
206
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

207
    """ direct_messages/sent """
Josh Roesslein's avatar
Josh Roesslein committed
208
209
    sent_direct_messages = bind_api(
        path = '/direct_messages/sent.json',
210
        payload_type = 'direct_message', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
211
212
213
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )
214

215
    """ direct_messages/new """
Josh Roesslein's avatar
Josh Roesslein committed
216
217
    send_direct_message = bind_api(
        path = '/direct_messages/new.json',
218
        method = 'POST',
219
        payload_type = 'direct_message',
220
        allowed_param = ['user', 'screen_name', 'user_id', 'text'],
221
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
222
    )
223

224
    """ direct_messages/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
225
226
227
    destroy_direct_message = bind_api(
        path = '/direct_messages/destroy.json',
        method = 'DELETE',
228
        payload_type = 'direct_message',
Josh Roesslein's avatar
Josh Roesslein committed
229
230
231
232
        allowed_param = ['id'],
        require_auth = True
    )

233
    """ friendships/create """
Josh Roesslein's avatar
Josh Roesslein committed
234
235
    create_friendship = bind_api(
        path = '/friendships/create.json',
236
        method = 'POST',
237
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
238
        allowed_param = ['id', 'user_id', 'screen_name', 'follow'],
239
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
240
241
    )

242
    """ friendships/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
243
244
245
    destroy_friendship = bind_api(
        path = '/friendships/destroy.json',
        method = 'DELETE',
246
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
247
248
249
250
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

251
    """ friendships/exists """
Josh Roesslein's avatar
Josh Roesslein committed
252
253
    exists_friendship = bind_api(
        path = '/friendships/exists.json',
254
        payload_type = 'json',
Josh Roesslein's avatar
Josh Roesslein committed
255
256
257
        allowed_param = ['user_a', 'user_b']
    )

258
    """ friendships/show """
Josh Roesslein's avatar
Josh Roesslein committed
259
260
    show_friendship = bind_api(
        path = '/friendships/show.json',
261
        payload_type = 'friendship',
Josh Roesslein's avatar
Josh Roesslein committed
262
263
264
265
        allowed_param = ['source_id', 'source_screen_name',
                          'target_id', 'target_screen_name']
    )

266
    """ friends/ids """
Josh Roesslein's avatar
Josh Roesslein committed
267
268
    friends_ids = bind_api(
        path = '/friends/ids.json',
269
        payload_type = 'ids',
270
        allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
271
272
    )

273
    """ followers/ids """
Josh Roesslein's avatar
Josh Roesslein committed
274
275
    followers_ids = bind_api(
        path = '/followers/ids.json',
276
        payload_type = 'ids',
277
        allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
278
279
    )

280
    """ account/verify_credentials """
Josh Roesslein's avatar
Josh Roesslein committed
281
282
283
284
    def verify_credentials(self):
        try:
            return bind_api(
                path = '/account/verify_credentials.json',
285
                payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
286
287
288
289
290
                require_auth = True
            )(self)
        except TweepError:
            return False

291
    """ account/rate_limit_status """
Josh Roesslein's avatar
Josh Roesslein committed
292
293
    rate_limit_status = bind_api(
        path = '/account/rate_limit_status.json',
294
        payload_type = 'json'
Josh Roesslein's avatar
Josh Roesslein committed
295
296
    )

297
    """ account/update_delivery_device """
Josh Roesslein's avatar
Josh Roesslein committed
298
299
300
301
    set_delivery_device = bind_api(
        path = '/account/update_delivery_device.json',
        method = 'POST',
        allowed_param = ['device'],
302
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
303
304
305
        require_auth = True
    )

306
    """ account/update_profile_colors """
Josh Roesslein's avatar
Josh Roesslein committed
307
308
309
    update_profile_colors = bind_api(
        path = '/account/update_profile_colors.json',
        method = 'POST',
310
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
311
312
313
314
315
316
        allowed_param = ['profile_background_color', 'profile_text_color',
                          'profile_link_color', 'profile_sidebar_fill_color',
                          'profile_sidebar_border_color'],
        require_auth = True
    )

317
    """ account/update_profile_image """
Josh Roesslein's avatar
Josh Roesslein committed
318
    def update_profile_image(self, filename):
Josh Roesslein's avatar
Josh Roesslein committed
319
        headers, post_data = API._pack_image(filename, 700)
Josh Roesslein's avatar
Josh Roesslein committed
320
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
321
322
            path = '/account/update_profile_image.json',
            method = 'POST',
323
            payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
324
325
326
            require_auth = True
        )(self, post_data=post_data, headers=headers)

327
    """ account/update_profile_background_image """
Josh Roesslein's avatar
Josh Roesslein committed
328
    def update_profile_background_image(self, filename, *args, **kargs):
Josh Roesslein's avatar
Josh Roesslein committed
329
        headers, post_data = API._pack_image(filename, 800)
Josh Roesslein's avatar
Josh Roesslein committed
330
331
332
        bind_api(
            path = '/account/update_profile_background_image.json',
            method = 'POST',
333
            payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
334
335
336
337
            allowed_param = ['tile'],
            require_auth = True
        )(self, post_data=post_data, headers=headers)

338
    """ account/update_profile """
Josh Roesslein's avatar
Josh Roesslein committed
339
340
341
    update_profile = bind_api(
        path = '/account/update_profile.json',
        method = 'POST',
342
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
343
        allowed_param = ['name', 'url', 'location', 'description'],
344
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
345
346
    )

347
    """ favorites """
Josh Roesslein's avatar
Josh Roesslein committed
348
349
    favorites = bind_api(
        path = '/favorites.json',
350
        payload_type = 'status', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
351
352
353
        allowed_param = ['id', 'page']
    )

354
    """ favorites/create """
355
356
357
    create_favorite = bind_api(
        path = '/favorites/create/{id}.json',
        method = 'POST',
358
        payload_type = 'status',
359
360
361
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
362

363
    """ favorites/destroy """
364
365
366
    destroy_favorite = bind_api(
        path = '/favorites/destroy/{id}.json',
        method = 'DELETE',
367
        payload_type = 'status',
368
369
370
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
371

372
    """ notifications/follow """
Josh Roesslein's avatar
Josh Roesslein committed
373
374
375
    enable_notifications = bind_api(
        path = '/notifications/follow.json',
        method = 'POST',
376
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
377
378
379
380
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

381
    """ notifications/leave """
Josh Roesslein's avatar
Josh Roesslein committed
382
383
384
    disable_notifications = bind_api(
        path = '/notifications/leave.json',
        method = 'POST',
385
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
386
387
388
389
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

390
    """ blocks/create """
Josh Roesslein's avatar
Josh Roesslein committed
391
392
393
    create_block = bind_api(
        path = '/blocks/create.json',
        method = 'POST',
394
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
395
        allowed_param = ['id', 'user_id', 'screen_name'],
Josh Roesslein's avatar
Josh Roesslein committed
396
397
398
        require_auth = True
    )

399
    """ blocks/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
400
401
402
    destroy_block = bind_api(
        path = '/blocks/destroy.json',
        method = 'DELETE',
403
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
404
        allowed_param = ['id', 'user_id', 'screen_name'],
Josh Roesslein's avatar
Josh Roesslein committed
405
406
407
        require_auth = True
    )

408
    """ blocks/exists """
Josh Roesslein's avatar
Josh Roesslein committed
409
    def exists_block(self, *args, **kargs):
Josh Roesslein's avatar
Josh Roesslein committed
410
411
412
413
414
        try:
            bind_api(
                path = '/blocks/exists.json',
                allowed_param = ['id', 'user_id', 'screen_name'],
                require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
415
            )(self, *args, **kargs)
Josh Roesslein's avatar
Josh Roesslein committed
416
417
418
419
        except TweepError:
            return False
        return True

420
    """ blocks/blocking """
Josh Roesslein's avatar
Josh Roesslein committed
421
422
    blocks = bind_api(
        path = '/blocks/blocking.json',
423
        payload_type = 'user', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
424
425
426
427
        allowed_param = ['page'],
        require_auth = True
    )

428
    """ blocks/blocking/ids """
Josh Roesslein's avatar
Josh Roesslein committed
429
430
    blocks_ids = bind_api(
        path = '/blocks/blocking/ids.json',
431
        payload_type = 'json',
Josh Roesslein's avatar
Josh Roesslein committed
432
433
434
        require_auth = True
    )

435
    """ report_spam """
Josh Roesslein's avatar
Josh Roesslein committed
436
437
438
    report_spam = bind_api(
        path = '/report_spam.json',
        method = 'POST',
439
        payload_type = 'user',
Josh Roesslein's avatar
Josh Roesslein committed
440
441
442
443
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

444
    """ saved_searches """
Josh Roesslein's avatar
Josh Roesslein committed
445
446
    saved_searches = bind_api(
        path = '/saved_searches.json',
447
        payload_type = 'saved_search', payload_list = True,
Josh Roesslein's avatar
Josh Roesslein committed
448
449
450
        require_auth = True
    )

451
    """ saved_searches/show """
452
453
    get_saved_search = bind_api(
        path = '/saved_searches/show/{id}.json',
454
        payload_type = 'saved_search',
455
456
457
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
458

459
    """ saved_searches/create """
Josh Roesslein's avatar
Josh Roesslein committed
460
461
462
    create_saved_search = bind_api(
        path = '/saved_searches/create.json',
        method = 'POST',
463
        payload_type = 'saved_search',
Josh Roesslein's avatar
Josh Roesslein committed
464
465
466
467
        allowed_param = ['query'],
        require_auth = True
    )

468
    """ saved_searches/destroy """
469
470
471
    destroy_saved_search = bind_api(
        path = '/saved_searches/destroy/{id}.json',
        method = 'DELETE',
472
        payload_type = 'saved_search',
473
474
475
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
476

477
    """ help/test """
Josh Roesslein's avatar
Josh Roesslein committed
478
    def test(self):
479
        try:
480
            bind_api(
481
482
483
484
                path = '/help/test.json',
            )(self)
        except TweepError:
            return False
485
        return True
486

Josh Roesslein's avatar
Josh Roesslein committed
487
488
    def create_list(self, *args, **kargs):
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
489
            path = '/%s/lists.json' % self.auth.get_username(),
Josh Roesslein's avatar
Josh Roesslein committed
490
            method = 'POST',
491
            payload_type = 'list',
492
            allowed_param = ['name', 'mode', 'description'],
Josh Roesslein's avatar
Josh Roesslein committed
493
494
495
            require_auth = True
        )(self, *args, **kargs)

Josh Roesslein's avatar
Josh Roesslein committed
496
497
498
499
    def destroy_list(self, slug):
        return bind_api(
            path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
            method = 'DELETE',
500
            payload_type = 'list',
Josh Roesslein's avatar
Josh Roesslein committed
501
502
503
            require_auth = True
        )(self)

Josh Roesslein's avatar
Josh Roesslein committed
504
505
    def update_list(self, slug, *args, **kargs):
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
506
            path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
Josh Roesslein's avatar
Josh Roesslein committed
507
            method = 'POST',
508
            payload_type = 'list',
509
            allowed_param = ['name', 'mode', 'description'],
Josh Roesslein's avatar
Josh Roesslein committed
510
511
512
            require_auth = True
        )(self, *args, **kargs)

513
514
    lists = bind_api(
        path = '/{user}/lists.json',
515
        payload_type = 'list', payload_list = True,
516
517
518
        allowed_param = ['user', 'cursor'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
519

520
521
    lists_memberships = bind_api(
        path = '/{user}/lists/memberships.json',
522
        payload_type = 'list', payload_list = True,
523
524
525
        allowed_param = ['user', 'cursor'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
526

527
528
    lists_subscriptions = bind_api(
        path = '/{user}/lists/subscriptions.json',
529
        payload_type = 'list', payload_list = True,
530
531
532
        allowed_param = ['user', 'cursor'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
533

534
535
    list_timeline = bind_api(
        path = '/{owner}/lists/{slug}/statuses.json',
536
        payload_type = 'status', payload_list = True,
537
538
        allowed_param = ['owner', 'slug', 'since_id', 'max_id', 'count', 'page']
    )
Josh Roesslein's avatar
Josh Roesslein committed
539

540
541
    get_list = bind_api(
        path = '/{owner}/lists/{slug}.json',
542
        payload_type = 'list',
543
544
        allowed_param = ['owner', 'slug']
    )
Josh Roesslein's avatar
Josh Roesslein committed
545
546
547
548
549

    def add_list_member(self, slug, *args, **kargs):
        return bind_api(
            path = '/%s/%s/members.json' % (self.auth.get_username(), slug),
            method = 'POST',
550
            payload_type = 'list',
Josh Roesslein's avatar
Josh Roesslein committed
551
552
553
554
555
556
557
558
            allowed_param = ['id'],
            require_auth = True
        )(self, *args, **kargs)

    def remove_list_member(self, slug, *args, **kargs):
        return bind_api(
            path = '/%s/%s/members.json' % (self.auth.get_username(), slug),
            method = 'DELETE',
559
            payload_type = 'list',
Josh Roesslein's avatar
Josh Roesslein committed
560
561
562
563
            allowed_param = ['id'],
            require_auth = True
        )(self, *args, **kargs)

564
565
    list_members = bind_api(
        path = '/{owner}/{slug}/members.json',
566
        payload_type = 'user', payload_list = True,
567
568
        allowed_param = ['owner', 'slug', 'cursor']
    )
Josh Roesslein's avatar
Josh Roesslein committed
569
570
571
572
573

    def is_list_member(self, owner, slug, user_id):
        try:
            return bind_api(
                path = '/%s/%s/members/%s.json' % (owner, slug, user_id),
574
                payload_type = 'user'
Josh Roesslein's avatar
Josh Roesslein committed
575
576
577
578
            )(self)
        except TweepError:
            return False

579
580
581
    subscribe_list = bind_api(
        path = '/{owner}/{slug}/subscribers.json',
        method = 'POST',
582
        payload_type = 'list',
583
584
585
        allowed_param = ['owner', 'slug'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
586

587
588
589
    unsubscribe_list = bind_api(
        path = '/{owner}/{slug}/subscribers.json',
        method = 'DELETE',
590
        payload_type = 'list',
591
592
593
        allowed_param = ['owner', 'slug'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
594

595
596
    list_subscribers = bind_api(
        path = '/{owner}/{slug}/subscribers.json',
597
        payload_type = 'user', payload_list = True,
598
599
        allowed_param = ['owner', 'slug', 'cursor']
    )
Josh Roesslein's avatar
Josh Roesslein committed
600
601
602
603
604

    def is_subscribed_list(self, owner, slug, user_id):
        try:
            return bind_api(
                path = '/%s/%s/subscribers/%s.json' % (owner, slug, user_id),
605
                payload_type = 'user'
Josh Roesslein's avatar
Josh Roesslein committed
606
607
608
609
            )(self)
        except TweepError:
            return False

610
    """ trends/available """
611
612
    trends_available = bind_api(
        path = '/trends/available.json',
613
        payload_type = 'json',
614
615
616
        allowed_param = ['lat', 'long']
    )

617
    """ trends/location """
618
619
    trends_location = bind_api(
        path = '/trends/{woeid}.json',
620
        payload_type = 'json',
621
622
        allowed_param = ['woeid']
    )
623

624
    """ search """
625
626
627
    search = bind_api(
        search_api = True,
        path = '/search.json',
628
        payload_type = 'search_result', payload_list = True,
629
630
        allowed_param = ['q', 'lang', 'locale', 'rpp', 'page', 'since_id', 'geocode', 'show_user']
    )
631
    search.pagination_mode = 'page'
Josh Roesslein's avatar
Josh Roesslein committed
632

633
    """ trends """
634
635
636
    trends = bind_api(
        search_api = True,
        path = '/trends.json',
637
        payload_type = 'json'
638
    )
Josh Roesslein's avatar
Josh Roesslein committed
639

640
    """ trends/current """
641
642
643
    trends_current = bind_api(
        search_api = True,
        path = '/trends/current.json',
644
        payload_type = 'json',
645
646
        allowed_param = ['exclude']
    )
647

648
    """ trends/daily """
649
650
651
    trends_daily = bind_api(
        search_api = True,
        path = '/trends/daily.json',
652
        payload_type = 'json',
653
654
        allowed_param = ['date', 'exclude']
    )
655

656
    """ trends/weekly """
657
658
659
    trends_weekly = bind_api(
        search_api = True,
        path = '/trends/weekly.json',
660
        payload_type = 'json',
661
662
        allowed_param = ['date', 'exclude']
    )
663

664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
    """ geo/reverse_geocode """
    reverse_geocode = bind_api(
        path = '/geo/reverse_geocode.json',
        payload_type = 'json',
        allowed_param = ['lat', 'long', 'accuracy', 'granularity', 'max_results']
    )

    """ geo/nearby_places """
    nearby_places = bind_api(
        path = '/geo/nearby_places.json',
        payload_type = 'json',
        allowed_param = ['lat', 'long', 'ip', 'accuracy', 'granularity', 'max_results']
    )

    """ geo/id """
    geo_id = bind_api(
        path = '/geo/id/{id}.json',
        payload_type = 'json',
        allowed_param = ['id']
    )

685
    """ Internal use only """
Josh Roesslein's avatar
Josh Roesslein committed
686
    @staticmethod
Josh Roesslein's avatar
Josh Roesslein committed
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    def _pack_image(filename, max_size):
        """Pack image from file into multipart-formdata post body"""
        # image must be less than 700kb in size
        try:
            if os.path.getsize(filename) > (max_size * 1024):
                raise TweepError('File is too big, must be less than 700kb.')
        except os.error, e:
            raise TweepError('Unable to access file')

        # image must be gif, jpeg, or png
        file_type = mimetypes.guess_type(filename)
        if file_type is None:
            raise TweepError('Could not determine file type')
        file_type = file_type[0]
        if file_type not in ['image/gif', 'image/jpeg', 'image/png']:
            raise TweepError('Invalid file type for image: %s' % file_type)

        # build the mulitpart-formdata body
        fp = open(filename, 'rb')
        BOUNDARY = 'Tw3ePy'
        body = []
        body.append('--' + BOUNDARY)
        body.append('Content-Disposition: form-data; name="image"; filename="%s"' % filename)
        body.append('Content-Type: %s' % file_type)
        body.append('')
        body.append(fp.read())
        body.append('--' + BOUNDARY + '--')
        body.append('')
        fp.close()
        body = '\r\n'.join(body)

        # build headers
        headers = {
            'Content-Type': 'multipart/form-data; boundary=Tw3ePy',
            'Content-Length': len(body)
        }

        return headers, body
725