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

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
Josh Roesslein's avatar
Josh Roesslein committed
10
from tweepy.parsers import *
11

Josh Roesslein's avatar
Josh Roesslein committed
12

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

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

32
    """ statuses/public_timeline """
Josh Roesslein's avatar
Josh Roesslein committed
33
34
35
36
37
38
    public_timeline = bind_api(
        path = '/statuses/public_timeline.json',
        parser = parse_statuses,
        allowed_param = []
    )

Ferenc Szalai's avatar
Ferenc Szalai committed
39
    """ statuses/home_timeline """
Josh Roesslein's avatar
Josh Roesslein committed
40
41
42
43
44
45
46
    home_timeline = bind_api(
        path = '/statuses/home_timeline.json',
        parser = parse_statuses,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

47
    """ statuses/friends_timeline """
Josh Roesslein's avatar
Josh Roesslein committed
48
49
50
51
52
53
54
    friends_timeline = bind_api(
        path = '/statuses/friends_timeline.json',
        parser = parse_statuses,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

55
    """ statuses/user_timeline """
Josh Roesslein's avatar
Josh Roesslein committed
56
57
58
59
60
61
62
    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']
    )

63
    """ statuses/mentions """
Josh Roesslein's avatar
Josh Roesslein committed
64
65
66
67
68
69
70
    mentions = bind_api(
        path = '/statuses/mentions.json',
        parser = parse_statuses,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

Ferenc Szalai's avatar
Ferenc Szalai committed
71
    """ statuses/retweeted_by_me """
Josh Roesslein's avatar
Josh Roesslein committed
72
73
74
75
76
77
78
    retweeted_by_me = bind_api(
        path = '/statuses/retweeted_by_me.json',
        parser = parse_statuses,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

79
    """ statuses/retweeted_to_me """
Josh Roesslein's avatar
Josh Roesslein committed
80
81
82
83
84
85
86
    retweeted_to_me = bind_api(
        path = '/statuses/retweeted_to_me.json',
        parser = parse_statuses,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

87
    """ statuses/retweets_of_me """
Josh Roesslein's avatar
Josh Roesslein committed
88
89
90
91
92
93
94
    retweets_of_me = bind_api(
        path = '/statuses/retweets_of_me.json',
        parser = parse_statuses,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

95
    """ statuses/show """
Josh Roesslein's avatar
Josh Roesslein committed
96
97
98
99
100
101
    get_status = bind_api(
        path = '/statuses/show.json',
        parser = parse_status,
        allowed_param = ['id']
    )

Ferenc Szalai's avatar
Ferenc Szalai committed
102
    """ statuses/update """
Josh Roesslein's avatar
Josh Roesslein committed
103
104
105
106
    update_status = bind_api(
        path = '/statuses/update.json',
        method = 'POST',
        parser = parse_status,
107
        allowed_param = ['status', 'in_reply_to_status_id', 'lat', 'long'],
Josh Roesslein's avatar
Josh Roesslein committed
108
109
110
        require_auth = True
    )

111
    """ statuses/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
112
113
114
115
116
117
118
119
    destroy_status = bind_api(
        path = '/statuses/destroy.json',
        method = 'DELETE',
        parser = parse_status,
        allowed_param = ['id'],
        require_auth = True
    )

Ferenc Szalai's avatar
Ferenc Szalai committed
120
121
122
123
124
125
126
127
    """ statuses/retweet """
    def retweet(self, id, *args, **kargs):
        return bind_api(
            path = '/statuses/retweet/%s.json' % id,
            method = 'POST',
            parser = parse_status,
            require_auth = True
        )(self, *args, **kargs)
Josh Roesslein's avatar
Josh Roesslein committed
128

Ferenc Szalai's avatar
Ferenc Szalai committed
129
    """ statuses/retweets """
130
131
132
    def retweets(self, id, *args, **kargs):
        return bind_api(
            path = '/statuses/retweets/%s.json' % id,
133
            parser = parse_statuses,
134
135
136
137
            allowed_param = ['count'],
            require_auth = True
        )(self, *args, **kargs)

138
    """ users/show """
Josh Roesslein's avatar
Josh Roesslein committed
139
140
141
142
143
144
    get_user = bind_api(
        path = '/users/show.json',
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name']
    )

145
    """ Get the authenticated user """
Josh Roesslein's avatar
Josh Roesslein committed
146
    def me(self):
Josh Roesslein's avatar
Josh Roesslein committed
147
        return self.get_user(screen_name=self.auth.get_username())
Josh Roesslein's avatar
Josh Roesslein committed
148

149
150
151
152
153
154
155
156
    """ users/search """
    search_users = bind_api(
        path = '/users/search.json',
        parser = parse_users,
        require_auth = True,
        allowed_param = ['q', 'per_page', 'page']
    )

157
    """ statuses/friends """
Josh Roesslein's avatar
Josh Roesslein committed
158
159
160
    friends = bind_api(
        path = '/statuses/friends.json',
        parser = parse_users,
161
        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
162
163
    )

164
    """ statuses/followers """
Josh Roesslein's avatar
Josh Roesslein committed
165
166
167
    followers = bind_api(
        path = '/statuses/followers.json',
        parser = parse_users,
168
        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
169
170
    )

171
    """ direct_messages """
Josh Roesslein's avatar
Josh Roesslein committed
172
173
174
175
176
177
178
    direct_messages = bind_api(
        path = '/direct_messages.json',
        parser = parse_directmessages,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

179
    """ direct_messages/sent """
Josh Roesslein's avatar
Josh Roesslein committed
180
181
182
183
184
185
    sent_direct_messages = bind_api(
        path = '/direct_messages/sent.json',
        parser = parse_directmessages,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )
186

187
    """ direct_messages/new """
Josh Roesslein's avatar
Josh Roesslein committed
188
189
    send_direct_message = bind_api(
        path = '/direct_messages/new.json',
190
        method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
191
192
        parser = parse_dm,
        allowed_param = ['user', 'text'],
193
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
194
    )
195

196
    """ direct_messages/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
197
198
199
200
201
202
203
204
    destroy_direct_message = bind_api(
        path = '/direct_messages/destroy.json',
        method = 'DELETE',
        parser = parse_dm,
        allowed_param = ['id'],
        require_auth = True
    )

205
    """ friendships/create """
Josh Roesslein's avatar
Josh Roesslein committed
206
207
    create_friendship = bind_api(
        path = '/friendships/create.json',
208
        method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
209
210
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name', 'follow'],
211
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
212
213
    )

214
    """ friendships/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
215
216
217
218
219
220
221
222
    destroy_friendship = bind_api(
        path = '/friendships/destroy.json',
        method = 'DELETE',
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

223
    """ friendships/exists """
Josh Roesslein's avatar
Josh Roesslein committed
224
225
226
227
228
229
    exists_friendship = bind_api(
        path = '/friendships/exists.json',
        parser = parse_json,
        allowed_param = ['user_a', 'user_b']
    )

230
    """ friendships/show """
Josh Roesslein's avatar
Josh Roesslein committed
231
232
233
234
235
236
237
    show_friendship = bind_api(
        path = '/friendships/show.json',
        parser = parse_friendship,
        allowed_param = ['source_id', 'source_screen_name',
                          'target_id', 'target_screen_name']
    )

238
    """ friends/ids """
Josh Roesslein's avatar
Josh Roesslein committed
239
240
    friends_ids = bind_api(
        path = '/friends/ids.json',
241
        parser = parse_ids,
242
        allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
243
244
    )

245
    """ followers/ids """
Josh Roesslein's avatar
Josh Roesslein committed
246
247
    followers_ids = bind_api(
        path = '/followers/ids.json',
248
        parser = parse_ids,
249
        allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
250
251
    )

252
    """ account/verify_credentials """
Josh Roesslein's avatar
Josh Roesslein committed
253
254
255
256
    def verify_credentials(self):
        try:
            return bind_api(
                path = '/account/verify_credentials.json',
Josh Roesslein's avatar
Josh Roesslein committed
257
                parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
258
259
260
261
262
                require_auth = True
            )(self)
        except TweepError:
            return False

263
    """ account/rate_limit_status """
Josh Roesslein's avatar
Josh Roesslein committed
264
265
266
267
268
    rate_limit_status = bind_api(
        path = '/account/rate_limit_status.json',
        parser = parse_json
    )

269
    """ account/update_delivery_device """
Josh Roesslein's avatar
Josh Roesslein committed
270
271
272
273
274
275
276
277
    set_delivery_device = bind_api(
        path = '/account/update_delivery_device.json',
        method = 'POST',
        allowed_param = ['device'],
        parser = parse_user,
        require_auth = True
    )

278
    """ account/update_profile_colors """
Josh Roesslein's avatar
Josh Roesslein committed
279
280
281
282
283
284
285
286
287
288
    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
    )

289
    """ account/update_profile_image """
Josh Roesslein's avatar
Josh Roesslein committed
290
    def update_profile_image(self, filename):
Josh Roesslein's avatar
Josh Roesslein committed
291
        headers, post_data = API._pack_image(filename, 700)
Josh Roesslein's avatar
Josh Roesslein committed
292
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
293
294
            path = '/account/update_profile_image.json',
            method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
295
            parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
296
297
298
            require_auth = True
        )(self, post_data=post_data, headers=headers)

299
    """ account/update_profile_background_image """
Josh Roesslein's avatar
Josh Roesslein committed
300
    def update_profile_background_image(self, filename, *args, **kargs):
Josh Roesslein's avatar
Josh Roesslein committed
301
        headers, post_data = API._pack_image(filename, 800)
Josh Roesslein's avatar
Josh Roesslein committed
302
303
304
        bind_api(
            path = '/account/update_profile_background_image.json',
            method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
305
            parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
306
307
308
309
            allowed_param = ['tile'],
            require_auth = True
        )(self, post_data=post_data, headers=headers)

310
    """ account/update_profile """
Josh Roesslein's avatar
Josh Roesslein committed
311
312
313
314
    update_profile = bind_api(
        path = '/account/update_profile.json',
        method = 'POST',
        parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
315
        allowed_param = ['name', 'url', 'location', 'description'],
316
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
317
318
    )

319
    """ favorites """
Josh Roesslein's avatar
Josh Roesslein committed
320
321
322
323
324
325
    favorites = bind_api(
        path = '/favorites.json',
        parser = parse_statuses,
        allowed_param = ['id', 'page']
    )

326
    """ favorites/create """
327
328
329
330
331
332
333
334
    def create_favorite(self, id):
        return bind_api(
            path = '/favorites/create/%s.json' % id,
            method = 'POST',
            parser = parse_status,
            allowed_param = ['id'],
            require_auth = True
        )(self, id)
Josh Roesslein's avatar
Josh Roesslein committed
335

336
    """ favorites/destroy """
337
338
339
340
341
342
343
344
    def destroy_favorite(self, id):
        return bind_api(
            path = '/favorites/destroy/%s.json' % id,
            method = 'DELETE',
            parser = parse_status,
            allowed_param = ['id'],
            require_auth = True
        )(self, id)
Josh Roesslein's avatar
Josh Roesslein committed
345

346
    """ notifications/follow """
Josh Roesslein's avatar
Josh Roesslein committed
347
348
349
350
351
352
353
354
    enable_notifications = bind_api(
        path = '/notifications/follow.json',
        method = 'POST',
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

355
    """ notifications/leave """
Josh Roesslein's avatar
Josh Roesslein committed
356
357
358
359
360
361
362
363
    disable_notifications = bind_api(
        path = '/notifications/leave.json',
        method = 'POST',
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

364
    """ blocks/create """
Josh Roesslein's avatar
Josh Roesslein committed
365
366
367
368
    create_block = bind_api(
        path = '/blocks/create.json',
        method = 'POST',
        parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
369
        allowed_param = ['id', 'user_id', 'screen_name'],
Josh Roesslein's avatar
Josh Roesslein committed
370
371
372
        require_auth = True
    )

373
    """ blocks/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
374
375
376
377
    destroy_block = bind_api(
        path = '/blocks/destroy.json',
        method = 'DELETE',
        parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
378
        allowed_param = ['id', 'user_id', 'screen_name'],
Josh Roesslein's avatar
Josh Roesslein committed
379
380
381
        require_auth = True
    )

382
    """ blocks/exists """
Josh Roesslein's avatar
Josh Roesslein committed
383
    def exists_block(self, *args, **kargs):
Josh Roesslein's avatar
Josh Roesslein committed
384
385
386
387
388
389
        try:
            bind_api(
                path = '/blocks/exists.json',
                parser = parse_none,
                allowed_param = ['id', 'user_id', 'screen_name'],
                require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
390
            )(self, *args, **kargs)
Josh Roesslein's avatar
Josh Roesslein committed
391
392
393
394
395
        except TweepError:
            return False

        return True

396
    """ blocks/blocking """
Josh Roesslein's avatar
Josh Roesslein committed
397
398
399
400
401
402
403
    blocks = bind_api(
        path = '/blocks/blocking.json',
        parser = parse_users,
        allowed_param = ['page'],
        require_auth = True
    )

404
    """ blocks/blocking/ids """
Josh Roesslein's avatar
Josh Roesslein committed
405
406
407
408
409
410
    blocks_ids = bind_api(
        path = '/blocks/blocking/ids.json',
        parser = parse_json,
        require_auth = True
    )

411
    """ report_spam """
Josh Roesslein's avatar
Josh Roesslein committed
412
413
414
415
416
417
418
419
    report_spam = bind_api(
        path = '/report_spam.json',
        method = 'POST',
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

420
    """ saved_searches """
Josh Roesslein's avatar
Josh Roesslein committed
421
422
423
424
425
426
    saved_searches = bind_api(
        path = '/saved_searches.json',
        parser = parse_saved_searches,
        require_auth = True
    )

427
    """ saved_searches/show """
Josh Roesslein's avatar
Josh Roesslein committed
428
429
430
431
432
433
434
    def get_saved_search(self, id):
        return bind_api(
            path = '/saved_searches/show/%s.json' % id,
            parser = parse_saved_search,
            require_auth = True
        )(self)

435
    """ saved_searches/create """
Josh Roesslein's avatar
Josh Roesslein committed
436
437
438
439
440
441
442
443
    create_saved_search = bind_api(
        path = '/saved_searches/create.json',
        method = 'POST',
        parser = parse_saved_search,
        allowed_param = ['query'],
        require_auth = True
    )

444
    """ saved_searches/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
445
446
447
448
449
450
451
452
453
    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)

454
    """ help/test """
Josh Roesslein's avatar
Josh Roesslein committed
455
    def test(self):
456
457
458
459
460
461
462
463
        try:
            return bind_api(
                path = '/help/test.json',
                parser = parse_return_true
            )(self)
        except TweepError:
            return False

Josh Roesslein's avatar
Josh Roesslein committed
464
465
    def create_list(self, *args, **kargs):
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
466
            path = '/%s/lists.json' % self.auth.get_username(),
Josh Roesslein's avatar
Josh Roesslein committed
467
468
469
470
471
472
            method = 'POST',
            parser = parse_list,
            allowed_param = ['name', 'mode'],
            require_auth = True
        )(self, *args, **kargs)

Josh Roesslein's avatar
Josh Roesslein committed
473
474
475
476
477
478
479
480
    def destroy_list(self, slug):
        return bind_api(
            path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
            method = 'DELETE',
            parser = parse_list,
            require_auth = True
        )(self)

Josh Roesslein's avatar
Josh Roesslein committed
481
482
    def update_list(self, slug, *args, **kargs):
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
483
            path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
Josh Roesslein's avatar
Josh Roesslein committed
484
485
486
487
488
489
            method = 'POST',
            parser = parse_list,
            allowed_param = ['name', 'mode'],
            require_auth = True
        )(self, *args, **kargs)

Josh Roesslein's avatar
Josh Roesslein committed
490
491
492
493
494
495
496
    def lists(self, *args, **kargs):
        return bind_api(
            path = '/%s/lists.json' % self.auth.get_username(),
            parser = parse_lists,
            allowed_param = ['cursor'],
            require_auth = True
        )(self, *args, **kargs)
497
    lists.pagination_mode = 'cursor'
Josh Roesslein's avatar
Josh Roesslein committed
498
499
500
501
502
503
504
505

    def lists_memberships(self, *args, **kargs):
        return bind_api(
            path = '/%s/lists/memberships.json' % self.auth.get_username(),
            parser = parse_lists,
            allowed_param = ['cursor'],
            require_auth = True
        )(self, *args, **kargs)
506
    lists_memberships.pagination_mode = 'cursor'
Josh Roesslein's avatar
Josh Roesslein committed
507

Josh Roesslein's avatar
Josh Roesslein committed
508
509
510
511
512
513
514
    def lists_subscriptions(self, *args, **kargs):
        return bind_api(
            path = '/%s/lists/subscriptions.json' % self.auth.get_username(),
            parser = parse_lists,
            allowed_param = ['cursor'],
            require_auth = True
        )(self, *args, **kargs)
515
    lists_subscriptions.pagination_mode = 'cursor'
Josh Roesslein's avatar
Josh Roesslein committed
516

Josh Roesslein's avatar
Josh Roesslein committed
517
518
519
520
    def list_timeline(self, owner, slug, *args, **kargs):
        return bind_api(
            path = '/%s/lists/%s/statuses.json' % (owner, slug),
            parser = parse_statuses,
Josh Roesslein's avatar
Josh Roesslein committed
521
            allowed_param = ['since_id', 'max_id', 'count', 'page']
Josh Roesslein's avatar
Josh Roesslein committed
522
        )(self, *args, **kargs)
523
    list_timeline.pagination_mode = 'page'
Josh Roesslein's avatar
Josh Roesslein committed
524
525
526
527
528
529
530
531
532
533
534

    def get_list(self, owner, slug):
        return bind_api(
            path = '/%s/lists/%s.json' % (owner, slug),
            parser = parse_list
        )(self)

    def add_list_member(self, slug, *args, **kargs):
        return bind_api(
            path = '/%s/%s/members.json' % (self.auth.get_username(), slug),
            method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
535
            parser = parse_list,
Josh Roesslein's avatar
Josh Roesslein committed
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
            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',
            parser = parse_user,
            allowed_param = ['id'],
            require_auth = True
        )(self, *args, **kargs)

    def list_members(self, owner, slug, *args, **kargs):
        return bind_api(
            path = '/%s/%s/members.json' % (owner, slug),
            parser = parse_users,
            allowed_param = ['cursor']
        )(self, *args, **kargs)
555
    list_members.pagination_mode = 'cursor'
Josh Roesslein's avatar
Josh Roesslein committed
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587

    def is_list_member(self, owner, slug, user_id):
        try:
            return bind_api(
                path = '/%s/%s/members/%s.json' % (owner, slug, user_id),
                parser = parse_user
            )(self)
        except TweepError:
            return False

    def subscribe_list(self, owner, slug):
        return bind_api(
            path = '/%s/%s/subscribers.json' % (owner, slug),
            method = 'POST',
            parser = parse_list,
            require_auth = True
        )(self)

    def unsubscribe_list(self, owner, slug):
        return bind_api(
            path = '/%s/%s/subscribers.json' % (owner, slug),
            method = 'DELETE',
            parser = parse_list,
            require_auth = True
        )(self)

    def list_subscribers(self, owner, slug, *args, **kargs):
        return bind_api(
            path = '/%s/%s/subscribers.json' % (owner, slug),
            parser = parse_users,
            allowed_param = ['cursor']
        )(self, *args, **kargs)
588
    list_subscribers.pagination_mode = 'cursor'
Josh Roesslein's avatar
Josh Roesslein committed
589
590
591
592
593

    def is_subscribed_list(self, owner, slug, user_id):
        try:
            return bind_api(
                path = '/%s/%s/subscribers/%s.json' % (owner, slug, user_id),
594
                parser = parse_user
Josh Roesslein's avatar
Josh Roesslein committed
595
596
597
598
            )(self)
        except TweepError:
            return False

599
600
601
602
603
604
605
606
607
608
609
610
611
612
    """ trends/available [coming soon] """
    trends_available = bind_api(
        path = '/trends/available.json',
        parser = parse_json,
        allowed_param = ['lat', 'long']
    )

    """ trends/location [coming soon] """
    def trends_location(self, woeid, *args, **kargs):
        return bind_api(
            path = '/trends/%s.json' % woeid,
            parser = parse_json,
        )(self, *args, **kargs)

613
    """ search """
614
615
616
617
618
619
    search = bind_api(
        search_api = True,
        path = '/search.json',
        parser = parse_search_results,
        allowed_param = ['q', 'lang', 'locale', 'rpp', 'page', 'since_id', 'geocode', 'show_user']
    )
620
    search.pagination_mode = 'page'
Josh Roesslein's avatar
Josh Roesslein committed
621

622
    """ trends """
623
624
625
626
627
    trends = bind_api(
        search_api = True,
        path = '/trends.json',
        parser = parse_json
    )
Josh Roesslein's avatar
Josh Roesslein committed
628

629
    """ trends/current """
630
631
632
633
634
635
    trends_current = bind_api(
        search_api = True,
        path = '/trends/current.json',
        parser = parse_json,
        allowed_param = ['exclude']
    )
636

637
    """ trends/daily """
638
639
640
641
642
643
    trends_daily = bind_api(
        search_api = True,
        path = '/trends/daily.json',
        parser = parse_json,
        allowed_param = ['date', 'exclude']
    )
644

645
    """ trends/weekly """
646
647
648
649
650
651
    trends_weekly = bind_api(
        search_api = True,
        path = '/trends/weekly.json',
        parser = parse_json,
        allowed_param = ['date', 'exclude']
    )
652

653
    """ Internal use only """
Josh Roesslein's avatar
Josh Roesslein committed
654
    @staticmethod
Josh Roesslein's avatar
Josh Roesslein committed
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
    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
693