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
    """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 = []
    )

39
    """ statuses/home_timeline [Coming soon] """
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
    )

71
    """ statuses/retweeted_by_me [Coming soon] """
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']
    )

102
    """ statuses/update [Geolocation parameters coming soon] """
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
    )

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

129
    """ statuses/retweets [Coming soon] """
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
    """ statuses/friends """
Josh Roesslein's avatar
Josh Roesslein committed
150
151
152
    friends = bind_api(
        path = '/statuses/friends.json',
        parser = parse_users,
153
        allowed_param = ['id', 'user_id', 'screen_name', 'page', 'cursor']
Josh Roesslein's avatar
Josh Roesslein committed
154
155
    )

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

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

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

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

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

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

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

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

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

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

237
    """ followers/ids """
Josh Roesslein's avatar
Josh Roesslein committed
238
239
    followers_ids = bind_api(
        path = '/followers/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
    """ account/verify_credentials """
Josh Roesslein's avatar
Josh Roesslein committed
245
246
247
248
    def verify_credentials(self):
        try:
            return bind_api(
                path = '/account/verify_credentials.json',
Josh Roesslein's avatar
Josh Roesslein committed
249
                parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
250
251
252
253
254
                require_auth = True
            )(self)
        except TweepError:
            return False

255
    """ account/rate_limit_status """
Josh Roesslein's avatar
Josh Roesslein committed
256
257
258
259
260
    rate_limit_status = bind_api(
        path = '/account/rate_limit_status.json',
        parser = parse_json
    )

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

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

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

291
    """ account/update_profile_background_image """
Josh Roesslein's avatar
Josh Roesslein committed
292
    def update_profile_background_image(self, filename, *args, **kargs):
Josh Roesslein's avatar
Josh Roesslein committed
293
        headers, post_data = API._pack_image(filename, 800)
Josh Roesslein's avatar
Josh Roesslein committed
294
295
296
        bind_api(
            path = '/account/update_profile_background_image.json',
            method = 'POST',
Josh Roesslein's avatar
Josh Roesslein committed
297
            parser = parse_user,
Josh Roesslein's avatar
Josh Roesslein committed
298
299
300
301
            allowed_param = ['tile'],
            require_auth = True
        )(self, post_data=post_data, headers=headers)

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

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

318
    """ favorites/create """
319
320
321
322
323
324
325
326
    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
327

328
    """ favorites/destroy """
329
330
331
332
333
334
335
336
    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
337

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

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

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

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

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

        return True

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

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

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

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

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

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

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

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

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

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

Josh Roesslein's avatar
Josh Roesslein committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
    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)

Josh Roesslein's avatar
Josh Roesslein committed
498
499
500
501
502
503
504
505
    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)

Josh Roesslein's avatar
Josh Roesslein committed
506
507
508
509
    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
510
            allowed_param = ['since_id', 'max_id', 'count', 'page']
Josh Roesslein's avatar
Josh Roesslein committed
511
512
513
514
515
516
517
518
519
520
521
522
        )(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',
Josh Roesslein's avatar
Josh Roesslein committed
523
            parser = parse_list,
Josh Roesslein's avatar
Josh Roesslein committed
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
574
575
576
577
578
579
580
581
582
583
584
            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

585
    """ search """
586
587
588
589
590
591
    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']
    )
Josh Roesslein's avatar
Josh Roesslein committed
592

593
    """ trends """
594
595
596
597
598
    trends = bind_api(
        search_api = True,
        path = '/trends.json',
        parser = parse_json
    )
Josh Roesslein's avatar
Josh Roesslein committed
599

600
    """ trends/current """
601
602
603
604
605
606
    trends_current = bind_api(
        search_api = True,
        path = '/trends/current.json',
        parser = parse_json,
        allowed_param = ['exclude']
    )
607

608
    """ trends/daily """
609
610
611
612
613
614
    trends_daily = bind_api(
        search_api = True,
        path = '/trends/daily.json',
        parser = parse_json,
        allowed_param = ['date', 'exclude']
    )
615

616
    """ trends/weekly """
617
618
619
620
621
622
    trends_weekly = bind_api(
        search_api = True,
        path = '/trends/weekly.json',
        parser = parse_json,
        allowed_param = ['date', 'exclude']
    )
623

624
    """ Internal use only """
Josh Roesslein's avatar
Josh Roesslein committed
625
    @staticmethod
Josh Roesslein's avatar
Josh Roesslein committed
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
661
662
663
    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
664