api.py 19.7 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
16
    """Twitter API"""

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

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

36
    """ statuses/home_timeline [Coming soon] """
Josh Roesslein's avatar
Josh Roesslein committed
37
38
39
40
41
42
43
    home_timeline = bind_api(
        path = '/statuses/home_timeline.json',
        parser = parse_statuses,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

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

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

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

68
    """ statuses/retweeted_by_me [Coming soon] """
Josh Roesslein's avatar
Josh Roesslein committed
69
70
71
72
73
74
75
    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
    )

76
    """ statuses/retweeted_to_me """
Josh Roesslein's avatar
Josh Roesslein committed
77
78
79
80
81
82
83
    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
    )

84
    """ statuses/retweets_of_me """
Josh Roesslein's avatar
Josh Roesslein committed
85
86
87
88
89
90
91
    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
    )

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

99
    """ statuses/update [Geolocation parameters coming soon] """
Josh Roesslein's avatar
Josh Roesslein committed
100
101
102
103
    update_status = bind_api(
        path = '/statuses/update.json',
        method = 'POST',
        parser = parse_status,
104
        allowed_param = ['status', 'in_reply_to_status_id', 'lat', 'long'],
Josh Roesslein's avatar
Josh Roesslein committed
105
106
107
        require_auth = True
    )

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

117
    """ statuses/retweet [Coming soon] """
Josh Roesslein's avatar
Josh Roesslein committed
118
119
120
121
122
123
124
125
    retweet = bind_api(
        path = '/statuses/retweet/id.json',
        method = 'POST',
        parser = parse_status,
        allowed_param = ['id'],
        require_auth = True
    )

126
    """ statuses/retweets [Coming soon] """
127
128
129
130
131
132
133
134
    def retweets(self, id, *args, **kargs):
        return bind_api(
            path = '/statuses/retweets/%s.json' % id,
            parser = parse_retweets,
            allowed_param = ['count'],
            require_auth = True
        )(self, *args, **kargs)

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

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

146
    """ statuses/friends """
Josh Roesslein's avatar
Josh Roesslein committed
147
148
149
    friends = bind_api(
        path = '/statuses/friends.json',
        parser = parse_users,
150
        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
151
152
    )

153
    """ statuses/followers """
Josh Roesslein's avatar
Josh Roesslein committed
154
155
156
    followers = bind_api(
        path = '/statuses/followers.json',
        parser = parse_users,
157
        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
158
159
    )

160
    """ direct_messages """
Josh Roesslein's avatar
Josh Roesslein committed
161
162
163
164
165
166
167
    direct_messages = bind_api(
        path = '/direct_messages.json',
        parser = parse_directmessages,
        allowed_param = ['since_id', 'max_id', 'count', 'page'],
        require_auth = True
    )

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

176
    """ direct_messages/new """
Josh Roesslein's avatar
Josh Roesslein committed
177
178
    send_direct_message = bind_api(
        path = '/direct_messages/new.json',
179
        method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
180
181
        parser = parse_dm,
        allowed_param = ['user', 'text'],
182
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
183
    )
184

185
    """ direct_messages/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
186
187
188
189
190
191
192
193
    destroy_direct_message = bind_api(
        path = '/direct_messages/destroy.json',
        method = 'DELETE',
        parser = parse_dm,
        allowed_param = ['id'],
        require_auth = True
    )

194
    """ friendships/create """
Josh Roesslein's avatar
Josh Roesslein committed
195
196
    create_friendship = bind_api(
        path = '/friendships/create.json',
197
        method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
198
199
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name', 'follow'],
200
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
201
202
    )

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

212
    """ friendships/exists """
Josh Roesslein's avatar
Josh Roesslein committed
213
214
215
216
217
218
    exists_friendship = bind_api(
        path = '/friendships/exists.json',
        parser = parse_json,
        allowed_param = ['user_a', 'user_b']
    )

219
    """ friendships/show """
Josh Roesslein's avatar
Josh Roesslein committed
220
221
222
223
224
225
226
    show_friendship = bind_api(
        path = '/friendships/show.json',
        parser = parse_friendship,
        allowed_param = ['source_id', 'source_screen_name',
                          'target_id', 'target_screen_name']
    )

227
    """ friends/ids """
Josh Roesslein's avatar
Josh Roesslein committed
228
229
    friends_ids = bind_api(
        path = '/friends/ids.json',
230
        parser = parse_ids,
231
        allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
232
233
    )

234
    """ followers/ids """
Josh Roesslein's avatar
Josh Roesslein committed
235
236
    followers_ids = bind_api(
        path = '/followers/ids.json',
237
        parser = parse_ids,
238
        allowed_param = ['id', 'user_id', 'screen_name', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
239
240
    )

241
    """ account/verify_credentials """
Josh Roesslein's avatar
Josh Roesslein committed
242
243
244
245
    def verify_credentials(self):
        try:
            return bind_api(
                path = '/account/verify_credentials.json',
Josh Roesslein's avatar
Josh Roesslein committed
246
                parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
247
248
249
250
251
                require_auth = True
            )(self)
        except TweepError:
            return False

252
    """ account/rate_limit_status """
Josh Roesslein's avatar
Josh Roesslein committed
253
254
255
256
257
    rate_limit_status = bind_api(
        path = '/account/rate_limit_status.json',
        parser = parse_json
    )

258
    """ account/update_delivery_device """
Josh Roesslein's avatar
Josh Roesslein committed
259
260
261
262
263
264
265
266
    set_delivery_device = bind_api(
        path = '/account/update_delivery_device.json',
        method = 'POST',
        allowed_param = ['device'],
        parser = parse_user,
        require_auth = True
    )

267
    """ account/update_profile_colors """
Josh Roesslein's avatar
Josh Roesslein committed
268
269
270
271
272
273
274
275
276
277
    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
    )

278
    """ account/update_profile_image """
Josh Roesslein's avatar
Josh Roesslein committed
279
    def update_profile_image(self, filename):
Josh Roesslein's avatar
Josh Roesslein committed
280
        headers, post_data = API._pack_image(filename, 700)
Josh Roesslein's avatar
Josh Roesslein committed
281
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
282
283
            path = '/account/update_profile_image.json',
            method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
284
            parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
285
286
287
            require_auth = True
        )(self, post_data=post_data, headers=headers)

288
    """ account/update_profile_background_image """
Josh Roesslein's avatar
Josh Roesslein committed
289
    def update_profile_background_image(self, filename, *args, **kargs):
Josh Roesslein's avatar
Josh Roesslein committed
290
        headers, post_data = API._pack_image(filename, 800)
Josh Roesslein's avatar
Josh Roesslein committed
291
292
293
        bind_api(
            path = '/account/update_profile_background_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
298
            allowed_param = ['tile'],
            require_auth = True
        )(self, post_data=post_data, headers=headers)

299
    """ account/update_profile """
Josh Roesslein's avatar
Josh Roesslein committed
300
301
302
303
    update_profile = bind_api(
        path = '/account/update_profile.json',
        method = 'POST',
        parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
304
        allowed_param = ['name', 'url', 'location', 'description'],
305
        require_auth = True
Josh Roesslein's avatar
Josh Roesslein committed
306
307
    )

308
    """ favorites """
Josh Roesslein's avatar
Josh Roesslein committed
309
310
311
312
313
314
    favorites = bind_api(
        path = '/favorites.json',
        parser = parse_statuses,
        allowed_param = ['id', 'page']
    )

315
    """ favorites/create """
316
317
318
319
320
321
322
323
    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
324

325
    """ favorites/destroy """
326
327
328
329
330
331
332
333
    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
334

335
    """ notifications/follow """
Josh Roesslein's avatar
Josh Roesslein committed
336
337
338
339
340
341
342
343
    enable_notifications = bind_api(
        path = '/notifications/follow.json',
        method = 'POST',
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

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

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

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

371
    """ blocks/exists """
Josh Roesslein's avatar
Josh Roesslein committed
372
    def exists_block(self, *args, **kargs):
Josh Roesslein's avatar
Josh Roesslein committed
373
374
375
376
377
378
        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
379
            )(self, *args, **kargs)
Josh Roesslein's avatar
Josh Roesslein committed
380
381
382
383
384
        except TweepError:
            return False

        return True

385
    """ blocks/blocking """
Josh Roesslein's avatar
Josh Roesslein committed
386
387
388
389
390
391
392
    blocks = bind_api(
        path = '/blocks/blocking.json',
        parser = parse_users,
        allowed_param = ['page'],
        require_auth = True
    )

393
    """ blocks/blocking/ids """
Josh Roesslein's avatar
Josh Roesslein committed
394
395
396
397
398
399
    blocks_ids = bind_api(
        path = '/blocks/blocking/ids.json',
        parser = parse_json,
        require_auth = True
    )

400
    """ report_spam """
Josh Roesslein's avatar
Josh Roesslein committed
401
402
403
404
405
406
407
408
    report_spam = bind_api(
        path = '/report_spam.json',
        method = 'POST',
        parser = parse_user,
        allowed_param = ['id', 'user_id', 'screen_name'],
        require_auth = True
    )

409
    """ saved_searches """
Josh Roesslein's avatar
Josh Roesslein committed
410
411
412
413
414
415
    saved_searches = bind_api(
        path = '/saved_searches.json',
        parser = parse_saved_searches,
        require_auth = True
    )

416
    """ saved_searches/show """
Josh Roesslein's avatar
Josh Roesslein committed
417
418
419
420
421
422
423
    def get_saved_search(self, id):
        return bind_api(
            path = '/saved_searches/show/%s.json' % id,
            parser = parse_saved_search,
            require_auth = True
        )(self)

424
    """ saved_searches/create """
Josh Roesslein's avatar
Josh Roesslein committed
425
426
427
428
429
430
431
432
    create_saved_search = bind_api(
        path = '/saved_searches/create.json',
        method = 'POST',
        parser = parse_saved_search,
        allowed_param = ['query'],
        require_auth = True
    )

433
    """ saved_searches/destroy """
Josh Roesslein's avatar
Josh Roesslein committed
434
435
436
437
438
439
440
441
442
    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)

443
    """ help/test """
Josh Roesslein's avatar
Josh Roesslein committed
444
    def test(self):
445
446
447
448
449
450
451
452
        try:
            return bind_api(
                path = '/help/test.json',
                parser = parse_return_true
            )(self)
        except TweepError:
            return False

Josh Roesslein's avatar
Josh Roesslein committed
453
454
    def create_list(self, *args, **kargs):
        return bind_api(
Josh Roesslein's avatar
Josh Roesslein committed
455
            path = '/%s/lists.json' % self.auth.get_username(),
Josh Roesslein's avatar
Josh Roesslein committed
456
457
458
459
460
461
            method = 'POST',
            parser = parse_list,
            allowed_param = ['name', 'mode'],
            require_auth = True
        )(self, *args, **kargs)

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

Josh Roesslein's avatar
Josh Roesslein committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
    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)

    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)

    def list_timeline(self, owner, slug, *args, **kargs):
        return bind_api(
            path = '/%s/lists/%s/statuses.json' % (owner, slug),
            parser = parse_statuses,
            allowed_param = ['page']
        )(self, *args, **kargs)

    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',
            parser = parse_user,
            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)

    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)

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

574
    """ search """
Josh Roesslein's avatar
Josh Roesslein committed
575
576
577
578
579
580

    def search(self, *args, **kargs):
        return bind_api(
            host = 'search.' + self.host,
            path = '/search.json',
            parser = parse_search_results,
581
            allowed_param = ['q', 'lang', 'locale', 'rpp', 'page', 'since_id', 'geocode', 'show_user'],
Josh Roesslein's avatar
Josh Roesslein committed
582
        )(self, *args, **kargs)
583
    search.pagination_mode = 'page'
Josh Roesslein's avatar
Josh Roesslein committed
584

585
    """ trends """
Josh Roesslein's avatar
Josh Roesslein committed
586
587
588
589
    def trends(self):
        return bind_api(
            host = 'search.' + self.host,
            path = '/trends.json',
590
            parser = parse_json
Josh Roesslein's avatar
Josh Roesslein committed
591
592
        )(self)

593
    """ trends/current """
594
595
596
597
598
599
600
601
    def trends_current(self, *args, **kargs):
        return bind_api(
            host = 'search.' + self.host,
            path = '/trends/current.json',
            parser = parse_json,
            allowed_param = ['exclude']
        )(self, *args, **kargs)

602
    """ trends/daily """
603
604
605
606
607
608
609
610
    def trends_daily(self, *args, **kargs):
        return bind_api(
            host = "search." + self.host,
            path = '/trends/daily.json',
            parser = parse_json,
            allowed_param = ['date', 'exclude']
        )(self, *args, **kargs)

611
    """ trends/weekly """
612
613
614
615
616
617
618
619
    def trends_weekly(self, *args, **kargs):
        return bind_api(
            host = "search." + self.host,
            path = '/trends/weekly.json',
            parser = parse_json,
            allowed_param = ['date', 'exclude']
        )(self, *args, **kargs)

620
621
    """ Internal use only """

Josh Roesslein's avatar
Josh Roesslein committed
622
    @staticmethod
Josh Roesslein's avatar
Josh Roesslein committed
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
    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
661