api.py 19.8 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', 'source'],
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
    """ statuses/retweet """
121
122
123
124
125
126
127
    retweet = bind_api(
        path = '/statuses/retweet/{id}.json',
        method = 'POST',
        parser = parse_status,
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
128

Ferenc Szalai's avatar
Ferenc Szalai committed
129
    """ statuses/retweets """
130
131
132
133
134
135
    retweets = bind_api(
        path = '/statuses/retweets/{id}.json',
        parser = parse_statuses,
        allowed_param = ['id', 'count'],
        require_auth = True
    )
136

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

325
    """ favorites/create """
326
327
328
329
330
331
332
    create_favorite = bind_api(
        path = '/favorites/create/{id}.json',
        method = 'POST',
        parser = parse_status,
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
333

334
    """ favorites/destroy """
335
336
337
338
339
340
341
    destroy_favorite = bind_api(
        path = '/favorites/destroy/{id}.json',
        method = 'DELETE',
        parser = parse_status,
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
342

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

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

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

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

379
    """ blocks/exists """
Josh Roesslein's avatar
Josh Roesslein committed
380
    def exists_block(self, *args, **kargs):
Josh Roesslein's avatar
Josh Roesslein committed
381
382
383
384
385
386
        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
387
            )(self, *args, **kargs)
Josh Roesslein's avatar
Josh Roesslein committed
388
389
390
391
        except TweepError:
            return False
        return True

392
    """ blocks/blocking """
Josh Roesslein's avatar
Josh Roesslein committed
393
394
395
396
397
398
399
    blocks = bind_api(
        path = '/blocks/blocking.json',
        parser = parse_users,
        allowed_param = ['page'],
        require_auth = True
    )

400
    """ blocks/blocking/ids """
Josh Roesslein's avatar
Josh Roesslein committed
401
402
403
404
405
406
    blocks_ids = bind_api(
        path = '/blocks/blocking/ids.json',
        parser = parse_json,
        require_auth = True
    )

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

416
    """ saved_searches """
Josh Roesslein's avatar
Josh Roesslein committed
417
418
419
420
421
422
    saved_searches = bind_api(
        path = '/saved_searches.json',
        parser = parse_saved_searches,
        require_auth = True
    )

423
    """ saved_searches/show """
424
425
426
427
428
429
    get_saved_search = bind_api(
        path = '/saved_searches/show/{id}.json',
        parser = parse_saved_search,
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
430

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

440
    """ saved_searches/destroy """
441
442
443
444
445
446
447
    destroy_saved_search = bind_api(
        path = '/saved_searches/destroy/{id}.json',
        method = 'DELETE',
        parser = parse_saved_search,
        allowed_param = ['id'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
448

449
    """ help/test """
Josh Roesslein's avatar
Josh Roesslein committed
450
    def test(self):
451
452
453
454
455
456
457
458
        try:
            return bind_api(
                path = '/help/test.json',
                parser = parse_return_true
            )(self)
        except TweepError:
            return False

Josh Roesslein's avatar
Josh Roesslein committed
459
460
    def create_list(self, *args, **kargs):
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
461
            path = '/%s/lists.json' % self.auth.get_username(),
Josh Roesslein's avatar
Josh Roesslein committed
462
463
            method = 'POST',
            parser = parse_list,
464
            allowed_param = ['name', 'mode', 'description'],
Josh Roesslein's avatar
Josh Roesslein committed
465
466
467
            require_auth = True
        )(self, *args, **kargs)

Josh Roesslein's avatar
Josh Roesslein committed
468
469
470
471
472
473
474
475
    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
476
477
    def update_list(self, slug, *args, **kargs):
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
478
            path = '/%s/lists/%s.json' % (self.auth.get_username(), slug),
Josh Roesslein's avatar
Josh Roesslein committed
479
480
            method = 'POST',
            parser = parse_list,
481
            allowed_param = ['name', 'mode', 'description'],
Josh Roesslein's avatar
Josh Roesslein committed
482
483
484
            require_auth = True
        )(self, *args, **kargs)

Josh Roesslein's avatar
Josh Roesslein committed
485
486
487
488
489
490
491
    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)
492
    lists.pagination_mode = 'cursor'
Josh Roesslein's avatar
Josh Roesslein committed
493
494
495
496
497
498
499
500

    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)
501
    lists_memberships.pagination_mode = 'cursor'
Josh Roesslein's avatar
Josh Roesslein committed
502

Josh Roesslein's avatar
Josh Roesslein committed
503
504
505
506
507
508
509
    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)
510
    lists_subscriptions.pagination_mode = 'cursor'
Josh Roesslein's avatar
Josh Roesslein committed
511

512
513
514
515
516
    list_timeline = bind_api(
        path = '/{owner}/lists/{slug}/statuses.json',
        parser = parse_statuses,
        allowed_param = ['owner', 'slug', 'since_id', 'max_id', 'count', 'page']
    )
Josh Roesslein's avatar
Josh Roesslein committed
517

518
519
520
521
522
    get_list = bind_api(
        path = '/{owner}/lists/{slug}.json',
        parser = parse_list,
        allowed_param = ['owner', 'slug']
    )
Josh Roesslein's avatar
Josh Roesslein committed
523
524
525
526
527

    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
528
            parser = parse_list,
Josh Roesslein's avatar
Josh Roesslein committed
529
530
531
532
533
534
535
536
            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',
537
            parser = parse_list,
Josh Roesslein's avatar
Josh Roesslein committed
538
539
540
541
            allowed_param = ['id'],
            require_auth = True
        )(self, *args, **kargs)

542
543
544
545
546
    list_members = bind_api(
        path = '/{owner}/{slug}/members.json',
        parser = parse_users,
        allowed_param = ['owner', 'slug', 'cursor']
    )
Josh Roesslein's avatar
Josh Roesslein committed
547
548
549
550
551
552
553
554
555
556

    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

557
558
559
560
561
562
563
    subscribe_list = bind_api(
        path = '/{owner}/{slug}/subscribers.json',
        method = 'POST',
        parser = parse_list,
        allowed_param = ['owner', 'slug'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
564

565
566
567
568
569
570
571
    unsubscribe_list = bind_api(
        path = '/{owner}/{slug}/subscribers.json',
        method = 'DELETE',
        parser = parse_list,
        allowed_param = ['owner', 'slug'],
        require_auth = True
    )
Josh Roesslein's avatar
Josh Roesslein committed
572

573
574
575
576
577
    list_subscribers = bind_api(
        path = '/{owner}/{slug}/subscribers.json',
        parser = parse_users,
        allowed_param = ['owner', 'slug', 'cursor']
    )
Josh Roesslein's avatar
Josh Roesslein committed
578
579
580
581
582

    def is_subscribed_list(self, owner, slug, user_id):
        try:
            return bind_api(
                path = '/%s/%s/subscribers/%s.json' % (owner, slug, user_id),
583
                parser = parse_user
Josh Roesslein's avatar
Josh Roesslein committed
584
585
586
587
            )(self)
        except TweepError:
            return False

588
589
590
591
592
593
594
595
    """ trends/available [coming soon] """
    trends_available = bind_api(
        path = '/trends/available.json',
        parser = parse_json,
        allowed_param = ['lat', 'long']
    )

    """ trends/location [coming soon] """
596
597
598
599
600
    trends_location = bind_api(
        path = '/trends/{woeid}.json',
        parser = parse_json,
        allowed_param = ['woeid']
    )
601

602
    """ search """
603
604
605
606
607
608
    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']
    )
609
    search.pagination_mode = 'page'
Josh Roesslein's avatar
Josh Roesslein committed
610

611
    """ trends """
612
613
614
615
616
    trends = bind_api(
        search_api = True,
        path = '/trends.json',
        parser = parse_json
    )
Josh Roesslein's avatar
Josh Roesslein committed
617

618
    """ trends/current """
619
620
621
622
623
624
    trends_current = bind_api(
        search_api = True,
        path = '/trends/current.json',
        parser = parse_json,
        allowed_param = ['exclude']
    )
625

626
    """ trends/daily """
627
628
629
630
631
632
    trends_daily = bind_api(
        search_api = True,
        path = '/trends/daily.json',
        parser = parse_json,
        allowed_param = ['date', 'exclude']
    )
633

634
    """ trends/weekly """
635
636
637
638
639
640
    trends_weekly = bind_api(
        search_api = True,
        path = '/trends/weekly.json',
        parser = parse_json,
        allowed_param = ['date', 'exclude']
    )
641

642
    """ Internal use only """
Josh Roesslein's avatar
Josh Roesslein committed
643
    @staticmethod
Josh Roesslein's avatar
Josh Roesslein committed
644
645
646
647
648
649
650
651
652
653
654
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
    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
682