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

Mark Smith's avatar
Mark Smith committed
5 6
from __future__ import print_function

7 8 9
import os
import mimetypes

Mark Smith's avatar
Mark Smith committed
10 11
import six

Josh Roesslein's avatar
Josh Roesslein committed
12 13
from tweepy.binder import bind_api
from tweepy.error import TweepError
14
from tweepy.parsers import ModelParser, Parser
15
from tweepy.utils import list_to_csv
16

Josh Roesslein's avatar
Josh Roesslein committed
17

18
class API(object):
Josh Roesslein's avatar
Josh Roesslein committed
19 20
    """Twitter API"""

21
    def __init__(self, auth_handler=None,
22
                 host='api.twitter.com', search_host='search.twitter.com',
Florent Espanet's avatar
Florent Espanet committed
23 24 25 26
                 upload_host='upload.twitter.com', cache=None, api_root='/1.1',
                 search_root='', upload_root='/1.1', retry_count=0,
                 retry_delay=0, retry_errors=None, timeout=60, parser=None,
                 compression=False, wait_on_rate_limit=False,
27 28 29 30 31 32
                 wait_on_rate_limit_notify=False, proxy=''):
        """ Api instance Constructor

        :param auth_handler:
        :param host:  url of the server of the rest api, default:'api.twitter.com'
        :param search_host: url of the search server, default:'search.twitter.com'
Florent Espanet's avatar
Florent Espanet committed
33
        :param upload_host: url of the upload server, default:'upload.twitter.com'
34 35 36
        :param cache: Cache to query if a GET method is used, default:None
        :param api_root: suffix of the api version, default:'/1.1'
        :param search_root: suffix of the search version, default:''
Florent Espanet's avatar
Florent Espanet committed
37
        :param upload_root: suffix of the upload version, default:'/1.1'
38 39 40 41 42 43 44 45 46 47 48 49
        :param retry_count: number of allowed retries, default:0
        :param retry_delay: delay in second between retries, default:0
        :param retry_errors: default:None
        :param timeout: delay before to consider the request as timed out in seconds, default:60
        :param parser: ModelParser instance to parse the responses, default:None
        :param compression: If the response is compressed, default:False
        :param wait_on_rate_limit: If the api wait when it hits the rate limit, default:False
        :param wait_on_rate_limit_notify: If the api print a notification when the rate limit is hit, default:False
        :param proxy: Url to use as proxy during the HTTP request, default:''

        :raise TypeError: If the given parser is not a ModelParser instance.
        """
Josh Roesslein's avatar
Josh Roesslein committed
50
        self.auth = auth_handler
Josh Roesslein's avatar
Josh Roesslein committed
51
        self.host = host
52
        self.search_host = search_host
Florent Espanet's avatar
Florent Espanet committed
53
        self.upload_host = upload_host
Josh Roesslein's avatar
Josh Roesslein committed
54
        self.api_root = api_root
55
        self.search_root = search_root
Florent Espanet's avatar
Florent Espanet committed
56
        self.upload_root = upload_root
Josh Roesslein's avatar
Josh Roesslein committed
57
        self.cache = cache
Mike's avatar
Mike committed
58
        self.compression = compression
59 60
        self.retry_count = retry_count
        self.retry_delay = retry_delay
61
        self.retry_errors = retry_errors
62
        self.timeout = timeout
63
        self.wait_on_rate_limit = wait_on_rate_limit
64
        self.wait_on_rate_limit_notify = wait_on_rate_limit_notify
65
        self.parser = parser or ModelParser()
Aaron Hill's avatar
Aaron Hill committed
66 67 68
        self.proxy = {}
        if proxy:
            self.proxy['https'] = proxy
Josh Roesslein's avatar
Josh Roesslein committed
69

70 71 72 73 74 75
        # Attempt to explain more clearly the parser argument requirements
        # https://github.com/tweepy/tweepy/issues/421
        #
        parser_type = Parser
        if not isinstance(self.parser, parser_type):
            raise TypeError(
Mark Smith's avatar
Mark Smith committed
76 77 78 79 80
                '"parser" argument has to be an instance of "{required}".'
                ' It is currently a {actual}.'.format(
                    required=parser_type.__name__,
                    actual=type(self.parser)
                )
81
            )
Josh Roesslein's avatar
Josh Roesslein committed
82

83 84
    @property
    def home_timeline(self):
85
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/home_timeline
86 87 88 89 90 91 92 93 94 95
            :allowed_param:'since_id', 'max_id', 'count'
        """
        return bind_api(
            api=self,
            path='/statuses/home_timeline.json',
            payload_type='status', payload_list=True,
            allowed_param=['since_id', 'max_id', 'count'],
            require_auth=True
        )

Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
96
    def statuses_lookup(self, id_, include_entities=None,
97
                        trim_user=None, map_=None, tweet_mode=None):
98
        return self._statuses_lookup(list_to_csv(id_), include_entities,
99
                                     trim_user, map_, tweet_mode)
100 101 102

    @property
    def _statuses_lookup(self):
103
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/lookup
104
            :allowed_param:'id', 'include_entities', 'trim_user', 'map', 'tweet_mode'
105 106 107 108 109
        """
        return bind_api(
            api=self,
            path='/statuses/lookup.json',
            payload_type='status', payload_list=True,
110
            allowed_param=['id', 'include_entities', 'trim_user', 'map', 'tweet_mode'],
111 112 113 114 115
            require_auth=True
        )

    @property
    def user_timeline(self):
116
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/user_timeline
rosscg's avatar
rosscg committed
117
            :allowed_param:'id', 'user_id', 'screen_name', 'since_id', 'max_id', 'count', 'include_rts', 'trim_user', 'exclude_replies'
118 119 120 121 122 123
        """
        return bind_api(
            api=self,
            path='/statuses/user_timeline.json',
            payload_type='status', payload_list=True,
            allowed_param=['id', 'user_id', 'screen_name', 'since_id',
124
                           'max_id', 'count', 'include_rts', 'trim_user',
rosscg's avatar
rosscg committed
125
                           'exclude_replies']
126 127 128 129
        )

    @property
    def mentions_timeline(self):
130
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/mentions_timeline
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            :allowed_param:'since_id', 'max_id', 'count'
        """
        return bind_api(
            api=self,
            path='/statuses/mentions_timeline.json',
            payload_type='status', payload_list=True,
            allowed_param=['since_id', 'max_id', 'count'],
            require_auth=True
        )

    @property
    def related_results(self):
        """ :reference: https://dev.twitter.com/docs/api/1.1/get/related_results/show/%3id.format
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/related_results/show/{id}.json',
            payload_type='relation', payload_list=True,
            allowed_param=['id'],
            require_auth=False
        )

    @property
    def retweets_of_me(self):
156
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/retweets_of_me
157 158 159 160 161 162 163 164 165 166 167 168
            :allowed_param:'since_id', 'max_id', 'count'
        """
        return bind_api(
            api=self,
            path='/statuses/retweets_of_me.json',
            payload_type='status', payload_list=True,
            allowed_param=['since_id', 'max_id', 'count'],
            require_auth=True
        )

    @property
    def get_status(self):
169 170
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/show/%3Aid
            :allowed_param:'id'
171 172 173 174 175 176 177 178
        """
        return bind_api(
            api=self,
            path='/statuses/show.json',
            payload_type='status',
            allowed_param=['id']
        )

179
    def update_status(self, *args, **kwargs):
180
        """ :reference: https://dev.twitter.com/rest/reference/post/statuses/update
181
            :allowed_param:'status', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'auto_populate_reply_metadata', 'lat', 'long', 'source', 'place_id', 'display_coordinates', 'media_ids'
182
        """
183
        post_data = {}
184
        media_ids = kwargs.pop("media_ids", None)
185 186
        if media_ids is not None:
            post_data["media_ids"] = list_to_csv(media_ids)
Namwoo Kim's avatar
Namwoo Kim committed
187

188 189 190 191 192
        return bind_api(
            api=self,
            path='/statuses/update.json',
            method='POST',
            payload_type='status',
193
            allowed_param=['status', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'auto_populate_reply_metadata', 'lat', 'long', 'source', 'place_id', 'display_coordinates'],
194
            require_auth=True
195
        )(post_data=post_data, *args, **kwargs)
196

Florent Espanet's avatar
Florent Espanet committed
197 198 199 200 201
    def media_upload(self, filename, *args, **kwargs):
        """ :reference: https://dev.twitter.com/rest/reference/post/media/upload
            :allowed_param:
        """
        f = kwargs.pop('file', None)
202
        headers, post_data = API._pack_image(filename, 4883, form_field='media', f=f)
Florent Espanet's avatar
Florent Espanet committed
203 204 205 206 207 208 209 210 211 212 213 214
        kwargs.update({'headers': headers, 'post_data': post_data})

        return bind_api(
            api=self,
            path='/media/upload.json',
            method='POST',
            payload_type='media',
            allowed_param=[],
            require_auth=True,
            upload_api=True
        )(*args, **kwargs)

215
    def update_with_media(self, filename, *args, **kwargs):
216
        """ :reference: https://dev.twitter.com/rest/reference/post/statuses/update_with_media
217
            :allowed_param:'status', 'possibly_sensitive', 'in_reply_to_status_id', 'in_reply_to_status_id_str', 'auto_populate_reply_metadata', 'lat', 'long', 'place_id', 'display_coordinates'
218
        """
219 220
        f = kwargs.pop('file', None)
        headers, post_data = API._pack_image(filename, 3072, form_field='media[]', f=f)
221 222 223
        kwargs.update({'headers': headers, 'post_data': post_data})

        return bind_api(
224
            api=self,
225
            path='/statuses/update_with_media.json',
226
            method='POST',
227
            payload_type='status',
228
            allowed_param=[
229 230
                'status', 'possibly_sensitive', 'in_reply_to_status_id', 'in_reply_to_status_id_str',
                'auto_populate_reply_metadata', 'lat', 'long', 'place_id', 'display_coordinates'
231 232
            ],
            require_auth=True
Aaron Hill's avatar
Aaron Hill committed
233
        )(*args, **kwargs)
234

235 236
    @property
    def destroy_status(self):
237
        """ :reference: https://dev.twitter.com/rest/reference/post/statuses/destroy/%3Aid
238 239 240 241 242 243 244 245 246 247 248 249 250
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/statuses/destroy/{id}.json',
            method='POST',
            payload_type='status',
            allowed_param=['id'],
            require_auth=True
        )

    @property
    def retweet(self):
251
        """ :reference: https://dev.twitter.com/rest/reference/post/statuses/retweet/%3Aid
252 253 254 255 256 257 258 259 260 261 262
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/statuses/retweet/{id}.json',
            method='POST',
            payload_type='status',
            allowed_param=['id'],
            require_auth=True
        )

legokichi's avatar
legokichi committed
263 264 265 266 267 268 269 270 271 272 273 274 275 276
    @property
    def unretweet(self):
        """ :reference: https://dev.twitter.com/rest/reference/post/statuses/unretweet/%3Aid
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/statuses/unretweet/{id}.json',
            method='POST',
            payload_type='status',
            allowed_param=['id'],
            require_auth=True
        )

277 278
    @property
    def retweets(self):
279
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/retweets/%3Aid
280 281 282 283 284 285 286 287 288 289 290 291
            :allowed_param:'id', 'count'
        """
        return bind_api(
            api=self,
            path='/statuses/retweets/{id}.json',
            payload_type='status', payload_list=True,
            allowed_param=['id', 'count'],
            require_auth=True
        )

    @property
    def retweeters(self):
292
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/retweeters/ids
293 294 295 296 297 298 299 300 301 302 303
            :allowed_param:'id', 'cursor', 'stringify_ids
        """
        return bind_api(
            api=self,
            path='/statuses/retweeters/ids.json',
            payload_type='ids',
            allowed_param=['id', 'cursor', 'stringify_ids']
        )

    @property
    def get_user(self):
304 305
        """ :reference: https://dev.twitter.com/rest/reference/get/users/show
            :allowed_param:'id', 'user_id', 'screen_name'
306 307 308 309 310 311 312 313 314 315
        """
        return bind_api(
            api=self,
            path='/users/show.json',
            payload_type='user',
            allowed_param=['id', 'user_id', 'screen_name']
        )

    @property
    def get_oembed(self):
316 317
        """ :reference: https://dev.twitter.com/rest/reference/get/statuses/oembed
            :allowed_param:'id', 'url', 'maxwidth', 'hide_media', 'omit_script', 'align', 'related', 'lang'
318 319 320 321 322 323 324 325
        """
        return bind_api(
            api=self,
            path='/statuses/oembed.json',
            payload_type='json',
            allowed_param=['id', 'url', 'maxwidth', 'hide_media', 'omit_script', 'align', 'related', 'lang']
        )

chebee7i's avatar
chebee7i committed
326
    def lookup_users(self, user_ids=None, screen_names=None, include_entities=None):
327
        """ Perform bulk look up of users from user ID or screenname """
chebee7i's avatar
chebee7i committed
328 329 330 331 332 333 334 335 336 337
        post_data = {}
        if include_entities is not None:
            include_entities = 'true' if include_entities else 'false'
            post_data['include_entities'] = include_entities
        if user_ids:
            post_data['user_id'] = list_to_csv(user_ids)
        if screen_names:
            post_data['screen_name'] = list_to_csv(screen_names)

        return self._lookup_users(post_data=post_data)
338

339 340
    @property
    def _lookup_users(self):
341
        """ :reference: https://dev.twitter.com/rest/reference/get/users/lookup
chebee7i's avatar
chebee7i committed
342
            allowed_param='user_id', 'screen_name', 'include_entities'
343 344 345 346 347
        """
        return bind_api(
            api=self,
            path='/users/lookup.json',
            payload_type='user', payload_list=True,
chebee7i's avatar
chebee7i committed
348
            method='POST',
349
            allowed_param=['user_id', 'screen_name', 'include_entities']
350
        )
351

Josh Roesslein's avatar
Josh Roesslein committed
352
    def me(self):
353
        """ Get the authenticated user """
Josh Roesslein's avatar
Josh Roesslein committed
354
        return self.get_user(screen_name=self.auth.get_username())
Josh Roesslein's avatar
Josh Roesslein committed
355

356 357
    @property
    def search_users(self):
358
        """ :reference: https://dev.twitter.com/rest/reference/get/users/search
359 360 361 362 363 364 365 366 367 368 369 370
            :allowed_param:'q', 'count', 'page'
        """
        return bind_api(
            api=self,
            path='/users/search.json',
            payload_type='user', payload_list=True,
            require_auth=True,
            allowed_param=['q', 'count', 'page']
        )

    @property
    def suggested_users(self):
371 372
        """ :reference: https://dev.twitter.com/rest/reference/get/users/suggestions/%3Aslug
            :allowed_param:'slug', 'lang'
373 374 375 376 377 378 379 380 381 382 383
        """
        return bind_api(
            api=self,
            path='/users/suggestions/{slug}.json',
            payload_type='user', payload_list=True,
            require_auth=True,
            allowed_param=['slug', 'lang']
        )

    @property
    def suggested_categories(self):
384
        """ :reference: https://dev.twitter.com/rest/reference/get/users/suggestions
385 386 387 388 389 390 391 392 393 394 395 396
            :allowed_param:'lang'
        """
        return bind_api(
            api=self,
            path='/users/suggestions.json',
            payload_type='category', payload_list=True,
            allowed_param=['lang'],
            require_auth=True
        )

    @property
    def suggested_users_tweets(self):
397
        """ :reference: https://dev.twitter.com/rest/reference/get/users/suggestions/%3Aslug/members
398 399 400 401 402 403 404 405 406 407 408 409
            :allowed_param:'slug'
        """
        return bind_api(
            api=self,
            path='/users/suggestions/{slug}/members.json',
            payload_type='status', payload_list=True,
            allowed_param=['slug'],
            require_auth=True
        )

    @property
    def direct_messages(self):
410
        """ :reference: https://dev.twitter.com/rest/reference/get/direct_messages
411
            :allowed_param:'since_id', 'max_id', 'count', 'full_text'
412 413 414 415 416
        """
        return bind_api(
            api=self,
            path='/direct_messages.json',
            payload_type='direct_message', payload_list=True,
417
            allowed_param=['since_id', 'max_id', 'count', 'full_text'],
418 419 420 421 422
            require_auth=True
        )

    @property
    def get_direct_message(self):
423
        """ :reference: https://dev.twitter.com/rest/reference/get/direct_messages/show
424
            :allowed_param:'id', 'full_text'
425 426 427 428 429
        """
        return bind_api(
            api=self,
            path='/direct_messages/show/{id}.json',
            payload_type='direct_message',
430
            allowed_param=['id', 'full_text'],
431 432 433 434 435
            require_auth=True
        )

    @property
    def sent_direct_messages(self):
436
        """ :reference: https://dev.twitter.com/rest/reference/get/direct_messages/sent
437
            :allowed_param:'since_id', 'max_id', 'count', 'page', 'full_text'
438 439 440 441 442
        """
        return bind_api(
            api=self,
            path='/direct_messages/sent.json',
            payload_type='direct_message', payload_list=True,
443
            allowed_param=['since_id', 'max_id', 'count', 'page', 'full_text'],
444 445 446 447 448
            require_auth=True
        )

    @property
    def send_direct_message(self):
449
        """ :reference: https://dev.twitter.com/rest/reference/post/direct_messages/new
450 451 452 453 454 455 456 457 458 459 460 461 462
            :allowed_param:'user', 'screen_name', 'user_id', 'text'
        """
        return bind_api(
            api=self,
            path='/direct_messages/new.json',
            method='POST',
            payload_type='direct_message',
            allowed_param=['user', 'screen_name', 'user_id', 'text'],
            require_auth=True
        )

    @property
    def destroy_direct_message(self):
463
        """ :reference: https://dev.twitter.com/rest/reference/post/direct_messages/destroy
464 465 466 467 468
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/direct_messages/destroy.json',
469
            method='POST',
470 471 472 473 474 475 476
            payload_type='direct_message',
            allowed_param=['id'],
            require_auth=True
        )

    @property
    def create_friendship(self):
477
        """ :reference: https://dev.twitter.com/rest/reference/post/friendships/create
478 479 480 481 482 483 484 485 486 487 488 489 490
            :allowed_param:'id', 'user_id', 'screen_name', 'follow'
        """
        return bind_api(
            api=self,
            path='/friendships/create.json',
            method='POST',
            payload_type='user',
            allowed_param=['id', 'user_id', 'screen_name', 'follow'],
            require_auth=True
        )

    @property
    def destroy_friendship(self):
491
        """ :reference: https://dev.twitter.com/rest/reference/post/friendships/destroy
492 493 494 495 496
            :allowed_param:'id', 'user_id', 'screen_name'
        """
        return bind_api(
            api=self,
            path='/friendships/destroy.json',
497
            method='POST',
498 499 500 501 502 503 504
            payload_type='user',
            allowed_param=['id', 'user_id', 'screen_name'],
            require_auth=True
        )

    @property
    def show_friendship(self):
505
        """ :reference: https://dev.twitter.com/rest/reference/get/friendships/show
506
            :allowed_param:'source_id', 'source_screen_name', 'target_id', 'target_screen_name'
507 508 509 510 511 512 513 514 515
        """
        return bind_api(
            api=self,
            path='/friendships/show.json',
            payload_type='friendship',
            allowed_param=['source_id', 'source_screen_name',
                           'target_id', 'target_screen_name']
        )

516
    def lookup_friendships(self, user_ids=None, screen_names=None):
517
        """ Perform bulk look up of friendships from user ID or screenname """
Santosh Kumar's avatar
Santosh Kumar committed
518
        return self._lookup_friendships(list_to_csv(user_ids), list_to_csv(screen_names))
519

520 521
    @property
    def _lookup_friendships(self):
522
        """ :reference: https://dev.twitter.com/rest/reference/get/friendships/lookup
523 524 525 526 527 528 529 530 531 532 533 534
            :allowed_param:'user_id', 'screen_name'
        """
        return bind_api(
            api=self,
            path='/friendships/lookup.json',
            payload_type='relationship', payload_list=True,
            allowed_param=['user_id', 'screen_name'],
            require_auth=True
        )

    @property
    def friends_ids(self):
535 536
        """ :reference: https://dev.twitter.com/rest/reference/get/friends/ids
            :allowed_param:'id', 'user_id', 'screen_name', 'cursor'
537 538 539 540 541 542 543 544 545 546
        """
        return bind_api(
            api=self,
            path='/friends/ids.json',
            payload_type='ids',
            allowed_param=['id', 'user_id', 'screen_name', 'cursor']
        )

    @property
    def friends(self):
547
        """ :reference: https://dev.twitter.com/rest/reference/get/friends/list
548
            :allowed_param:'id', 'user_id', 'screen_name', 'cursor', 'skip_status', 'include_user_entities'
549 550 551 552 553
        """
        return bind_api(
            api=self,
            path='/friends/list.json',
            payload_type='user', payload_list=True,
554
            allowed_param=['id', 'user_id', 'screen_name', 'cursor', 'skip_status', 'include_user_entities']
555 556 557 558
        )

    @property
    def friendships_incoming(self):
559 560
        """ :reference: https://dev.twitter.com/rest/reference/get/friendships/incoming
            :allowed_param:'cursor'
561 562 563 564 565 566 567 568 569 570
        """
        return bind_api(
            api=self,
            path='/friendships/incoming.json',
            payload_type='ids',
            allowed_param=['cursor']
        )

    @property
    def friendships_outgoing(self):
571 572
        """ :reference: https://dev.twitter.com/rest/reference/get/friendships/outgoing
            :allowed_param:'cursor'
573 574 575 576 577 578 579 580 581 582
        """
        return bind_api(
            api=self,
            path='/friendships/outgoing.json',
            payload_type='ids',
            allowed_param=['cursor']
        )

    @property
    def followers_ids(self):
583 584
        """ :reference: https://dev.twitter.com/rest/reference/get/followers/ids
            :allowed_param:'id', 'user_id', 'screen_name', 'cursor', 'count'
585 586 587 588 589 590 591 592 593 594
        """
        return bind_api(
            api=self,
            path='/followers/ids.json',
            payload_type='ids',
            allowed_param=['id', 'user_id', 'screen_name', 'cursor', 'count']
        )

    @property
    def followers(self):
595
        """ :reference: https://dev.twitter.com/rest/reference/get/followers/list
596 597 598 599 600 601 602 603 604 605
            :allowed_param:'id', 'user_id', 'screen_name', 'cursor', 'count', 'skip_status', 'include_user_entities'
        """
        return bind_api(
            api=self,
            path='/followers/list.json',
            payload_type='user', payload_list=True,
            allowed_param=['id', 'user_id', 'screen_name', 'cursor', 'count',
                           'skip_status', 'include_user_entities']
        )

Namwoo Kim's avatar
Namwoo Kim committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
    @property
    def get_settings(self):
        """ :reference: https://dev.twitter.com/rest/reference/get/account/settings
        """
        return bind_api(
            api=self,
            path='/account/settings.json',
            payload_type='json',
            use_cache=False
        )

    @property
    def set_settings(self):
        """ :reference: https://dev.twitter.com/rest/reference/post/account/settings
            :allowed_param:'sleep_time_enabled', 'start_sleep_time',
            'end_sleep_time', 'time_zone', 'trend_location_woeid',
            'allow_contributor_request', 'lang'
        """
        return bind_api(
            api=self,
            path='/account/settings.json',
            method='POST',
            payload_type='json',
            allowed_param=['sleep_time_enabled', 'start_sleep_time',
                           'end_sleep_time', 'time_zone',
                           'trend_location_woeid', 'allow_contributor_request',
                           'lang'],
            use_cache=False
        )

636
    def verify_credentials(self, **kargs):
637
        """ :reference: https://dev.twitter.com/rest/reference/get/account/verify_credentials
Jacob Ferrero's avatar
Jacob Ferrero committed
638
            :allowed_param:'include_entities', 'skip_status', 'include_email'
639
        """
Josh Roesslein's avatar
Josh Roesslein committed
640 641
        try:
            return bind_api(
642 643 644 645
                api=self,
                path='/account/verify_credentials.json',
                payload_type='user',
                require_auth=True,
Jacob Ferrero's avatar
Jacob Ferrero committed
646
                allowed_param=['include_entities', 'skip_status', 'include_email'],
Aaron Hill's avatar
Aaron Hill committed
647
            )(**kargs)
648
        except TweepError as e:
649 650 651
            if e.response and e.response.status == 401:
                return False
            raise
Josh Roesslein's avatar
Josh Roesslein committed
652

653 654
    @property
    def rate_limit_status(self):
655
        """ :reference: https://dev.twitter.com/rest/reference/get/application/rate_limit_status
656 657 658 659 660 661 662 663 664 665 666 667
            :allowed_param:'resources'
        """
        return bind_api(
            api=self,
            path='/application/rate_limit_status.json',
            payload_type='json',
            allowed_param=['resources'],
            use_cache=False
        )

    @property
    def set_delivery_device(self):
668
        """ :reference: https://dev.twitter.com/rest/reference/post/account/update_delivery_device
669 670 671 672 673 674 675 676 677 678 679 680
            :allowed_param:'device'
        """
        return bind_api(
            api=self,
            path='/account/update_delivery_device.json',
            method='POST',
            allowed_param=['device'],
            payload_type='user',
            require_auth=True
        )

    def update_profile_image(self, filename, file_=None):
681 682 683
        """ :reference: https://dev.twitter.com/rest/reference/post/account/update_profile_image
            :allowed_param:'include_entities', 'skip_status'
        """
684 685 686 687 688 689
        headers, post_data = API._pack_image(filename, 700, f=file_)
        return bind_api(
            api=self,
            path='/account/update_profile_image.json',
            method='POST',
            payload_type='user',
690
            allowed_param=['include_entities', 'skip_status'],
691
            require_auth=True
Josh Roesslein's avatar
Josh Roesslein committed
692 693
        )(self, post_data=post_data, headers=headers)

694
    def update_profile_background_image(self, filename, **kargs):
695 696 697
        """ :reference: https://dev.twitter.com/rest/reference/post/account/update_profile_background_image
            :allowed_param:'tile', 'include_entities', 'skip_status', 'use'
        """
698 699
        f = kargs.pop('file', None)
        headers, post_data = API._pack_image(filename, 800, f=f)
Josh Roesslein's avatar
Josh Roesslein committed
700
        bind_api(
Jordi Riera's avatar
Jordi Riera committed
701
            api=self,
702 703 704
            path='/account/update_profile_background_image.json',
            method='POST',
            payload_type='user',
Arudmin's avatar
Arudmin committed
705
            allowed_param=['tile', 'include_entities', 'skip_status', 'use'],
706
            require_auth=True
Mark Smith's avatar
Mark Smith committed
707
        )(post_data=post_data, headers=headers)
Josh Roesslein's avatar
Josh Roesslein committed
708

709
    def update_profile_banner(self, filename, **kargs):
710 711 712
        """ :reference: https://dev.twitter.com/rest/reference/post/account/update_profile_banner
            :allowed_param:'width', 'height', 'offset_left', 'offset_right'
        """
713 714
        f = kargs.pop('file', None)
        headers, post_data = API._pack_image(filename, 700, form_field="banner", f=f)
715
        bind_api(
Jordi Riera's avatar
Jordi Riera committed
716
            api=self,
717 718 719 720
            path='/account/update_profile_banner.json',
            method='POST',
            allowed_param=['width', 'height', 'offset_left', 'offset_right'],
            require_auth=True
Mark Smith's avatar
Mark Smith committed
721
        )(post_data=post_data, headers=headers)
722

723 724
    @property
    def update_profile(self):
725
        """ :reference: https://dev.twitter.com/rest/reference/post/account/update_profile
726
            :allowed_param:'name', 'url', 'location', 'description', 'profile_link_color'
727 728 729 730 731 732
        """
        return bind_api(
            api=self,
            path='/account/update_profile.json',
            method='POST',
            payload_type='user',
733
            allowed_param=['name', 'url', 'location', 'description', 'profile_link_color'],
734 735 736 737 738
            require_auth=True
        )

    @property
    def favorites(self):
739 740
        """ :reference: https://dev.twitter.com/rest/reference/get/favorites/list
            :allowed_param:'screen_name', 'user_id', 'max_id', 'count', 'since_id', 'max_id'
741 742 743 744 745 746 747 748 749 750
        """
        return bind_api(
            api=self,
            path='/favorites/list.json',
            payload_type='status', payload_list=True,
            allowed_param=['screen_name', 'user_id', 'max_id', 'count', 'since_id', 'max_id']
        )

    @property
    def create_favorite(self):
751
        """ :reference:https://dev.twitter.com/rest/reference/post/favorites/create
752 753 754 755 756 757 758 759 760 761 762 763 764
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/favorites/create.json',
            method='POST',
            payload_type='status',
            allowed_param=['id'],
            require_auth=True
        )

    @property
    def destroy_favorite(self):
765
        """ :reference: https://dev.twitter.com/rest/reference/post/favorites/destroy
766 767 768 769 770 771 772 773 774 775 776 777 778
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/favorites/destroy.json',
            method='POST',
            payload_type='status',
            allowed_param=['id'],
            require_auth=True
        )

    @property
    def create_block(self):
779
        """ :reference: https://dev.twitter.com/rest/reference/post/blocks/create
780 781 782 783 784 785 786 787 788 789 790 791 792
            :allowed_param:'id', 'user_id', 'screen_name'
        """
        return bind_api(
            api=self,
            path='/blocks/create.json',
            method='POST',
            payload_type='user',
            allowed_param=['id', 'user_id', 'screen_name'],
            require_auth=True
        )

    @property
    def destroy_block(self):
793
        """ :reference: https://dev.twitter.com/rest/reference/post/blocks/destroy
794 795 796 797 798
            :allowed_param:'id', 'user_id', 'screen_name'
        """
        return bind_api(
            api=self,
            path='/blocks/destroy.json',
799
            method='POST',
800 801 802 803 804
            payload_type='user',
            allowed_param=['id', 'user_id', 'screen_name'],
            require_auth=True
        )

antoinemcgrath's avatar
update  
antoinemcgrath committed
805 806 807 808 809 810 811 812 813
    @property
    def mutes_ids(self):
        """ :reference: https://dev.twitter.com/rest/reference/get/mutes/users/ids """
        return bind_api(
            api=self,
            path='/mutes/users/ids.json',
            payload_type='json',
            require_auth=True
        )
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844

    @property
    def create_mute(self):
        """ :reference: https://dev.twitter.com/rest/reference/post/mutes/users/create
            :allowed_param:'id', 'user_id', 'screen_name'
        """
        return bind_api(
            api=self,
            path='/mutes/users/create.json',
            method='POST',
            payload_type='user',
            allowed_param=['id', 'user_id', 'screen_name'],
            require_auth=True
        )

    @property
    def destroy_mute(self):
        """ :reference: https://dev.twitter.com/rest/reference/post/mutes/users/destroy
            :allowed_param:'id', 'user_id', 'screen_name'
        """
        return bind_api(
            api=self,
            path='/mutes/users/destroy.json',
            method='POST',
            payload_type='user',
            allowed_param=['id', 'user_id', 'screen_name'],
            require_auth=True
        )



845 846
    @property
    def blocks(self):
847
        """ :reference: https://dev.twitter.com/rest/reference/get/blocks/list
848 849 850 851 852 853 854 855 856 857 858 859
            :allowed_param:'cursor'
        """
        return bind_api(
            api=self,
            path='/blocks/list.json',
            payload_type='user', payload_list=True,
            allowed_param=['cursor'],
            require_auth=True
        )

    @property
    def blocks_ids(self):
860
        """ :reference: https://dev.twitter.com/rest/reference/get/blocks/ids """
861 862 863 864 865 866 867 868 869
        return bind_api(
            api=self,
            path='/blocks/ids.json',
            payload_type='json',
            require_auth=True
        )

    @property
    def report_spam(self):
870
        """ :reference: https://dev.twitter.com/rest/reference/post/users/report_spam
871 872 873 874 875 876 877 878 879 880 881 882 883
            :allowed_param:'user_id', 'screen_name'
        """
        return bind_api(
            api=self,
            path='/users/report_spam.json',
            method='POST',
            payload_type='user',
            allowed_param=['user_id', 'screen_name'],
            require_auth=True
        )

    @property
    def saved_searches(self):
884
        """ :reference: https://dev.twitter.com/rest/reference/get/saved_searches/show/%3Aid """
885 886 887 888 889 890 891 892 893
        return bind_api(
            api=self,
            path='/saved_searches/list.json',
            payload_type='saved_search', payload_list=True,
            require_auth=True
        )

    @property
    def get_saved_search(self):
894
        """ :reference: https://dev.twitter.com/rest/reference/get/saved_searches/show/%3Aid
895 896 897 898 899 900 901 902 903 904 905 906
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/saved_searches/show/{id}.json',
            payload_type='saved_search',
            allowed_param=['id'],
            require_auth=True
        )

    @property
    def create_saved_search(self):
907
        """ :reference: https://dev.twitter.com/rest/reference/post/saved_searches/create
908 909 910 911 912 913 914 915 916 917 918 919 920
            :allowed_param:'query'
        """
        return bind_api(
            api=self,
            path='/saved_searches/create.json',
            method='POST',
            payload_type='saved_search',
            allowed_param=['query'],
            require_auth=True
        )

    @property
    def destroy_saved_search(self):
921
        """ :reference: https://dev.twitter.com/rest/reference/post/saved_searches/destroy/%3Aid
922 923 924 925 926 927 928 929 930 931 932 933 934
            :allowed_param:'id'
        """
        return bind_api(
            api=self,
            path='/saved_searches/destroy/{id}.json',
            method='POST',
            payload_type='saved_search',
            allowed_param=['id'],
            require_auth=True
        )

    @property
    def create_list(self):
935
        """ :reference: https://dev.twitter.com/rest/reference/post/lists/create
936 937 938 939 940 941 942 943 944 945 946 947 948
            :allowed_param:'name', 'mode', 'description'
        """
        return bind_api(
            api=self,
            path='/lists/create.json',
            method='POST',
            payload_type='list',
            allowed_param=['name', 'mode', 'description'],
            require_auth=True
        )

    @property
    def destroy_list(self):
949
        """ :reference: https://dev.twitter.com/rest/reference/post/lists/destroy
950 951 952 953 954 955 956 957 958 959 960 961 962
            :allowed_param:'owner_screen_name', 'owner_id', 'list_id', 'slug'
        """
        return bind_api(
            api=self,
            path='/lists/destroy.json',
            method='POST',
            payload_type='list',
            allowed_param=['owner_screen_name', 'owner_id', 'list_id', 'slug'],
            require_auth=True
        )

    @property
    def update_list(self):
963
        """ :reference: https://dev.twitter.com/rest/reference/post/lists/update
964 965 966 967 968 969 970 971 972 973 974 975 976
            :allowed_param: list_id', 'slug', 'name', 'mode', 'description', 'owner_screen_name', 'owner_id'
        """
        return bind_api(
            api=self,
            path='/lists/update.json',
            method='POST',
            payload_type='list',
            allowed_param=['list_id', 'slug', 'name', 'mode', 'description', 'owner_screen_name', 'owner_id'],
            require_auth=True
        )

    @property
    def lists_all(self):
977
        """ :reference: https://dev.twitter.com/rest/reference/get/lists/list
978 979 980 981 982 983 984 985 986 987 988 989
            :allowed_param:'screen_name', 'user_id'
        """
        return bind_api(
            api=self,
            path='/lists/list.json',
            payload_type='list', payload_list=True,
            allowed_param=['screen_name', 'user_id'],
            require_auth=True
        )

    @property
    def lists_memberships(self):
990
        """ :reference: https://dev.twitter.com/rest/reference/get/lists/memberships
991 992 993 994 995 996 997 998 999 1000 1001 1002
            :allowed_param:'screen_name', 'user_id', 'filter_to_owned_lists', 'cursor'
        """
        return bind_api(
            api=self,
            path='/lists/memberships.json',
            payload_type='list', payload_list=True,
            allowed_param=['screen_name', 'user_id', 'filter_to_owned_lists', 'cursor'],
            require_auth=True
        )

    @property
    def lists_subscriptions(self):
1003
        """ :reference: https://dev.twitter.com/rest/reference/get/lists/subscriptions
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
            :allowed_param:'screen_name', 'user_id', 'cursor'
        """
        return bind_api(
            api=self,
            path='/lists/subscriptions.json',
            payload_type='list', payload_list=True,
            allowed_param=['screen_name', 'user_id', 'cursor'],
            require_auth=True
        )

    @property
    def list_timeline(self):
        """ :reference: https://dev.twitter.com/docs/api/1.1/get/lists/statuses
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1017 1018
            :allowed_param:'owner_screen_name', 'slug', 'owner_id', 'list_id',
             'since_id', 'max_id', 'count', 'include_rts
1019 1020 1021 1022 1023
        """
        return bind_api(
            api=self,
            path='/lists/statuses.json',
            payload_type='status', payload_list=True,
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1024 1025 1026
            allowed_param=['owner_screen_name', 'slug', 'owner_id',
                           'list_id', 'since_id', 'max_id', 'count',
                           'include_rts']
1027 1028 1029 1030
        )

    @property
    def get_list(self):
1031 1032
        """ :reference: https://dev.twitter.com/rest/reference/get/lists/show
            :allowed_param:'owner_screen_name', 'owner_id', 'slug', 'list_id'
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
        """
        return bind_api(
            api=self,
            path='/lists/show.json',
            payload_type='list',
            allowed_param=['owner_screen_name', 'owner_id', 'slug', 'list_id']
        )

    @property
    def add_list_member(self):
        """ :reference: https://dev.twitter.com/docs/api/1.1/post/lists/members/create
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1044 1045
            :allowed_param:'screen_name', 'user_id', 'owner_screen_name',
             'owner_id', 'slug', 'list_id'
1046 1047 1048 1049 1050 1051
        """
        return bind_api(
            api=self,
            path='/lists/members/create.json',
            method='POST',
            payload_type='list',
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1052 1053
            allowed_param=['screen_name', 'user_id', 'owner_screen_name',
                           'owner_id', 'slug', 'list_id'],
1054 1055 1056 1057 1058 1059
            require_auth=True
        )

    @property
    def remove_list_member(self):
        """ :reference: https://dev.twitter.com/docs/api/1.1/post/lists/members/destroy
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1060 1061
            :allowed_param:'screen_name', 'user_id', 'owner_screen_name',
             'owner_id', 'slug', 'list_id'
1062 1063 1064 1065 1066 1067
        """
        return bind_api(
            api=self,
            path='/lists/members/destroy.json',
            method='POST',
            payload_type='list',
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1068 1069
            allowed_param=['screen_name', 'user_id', 'owner_screen_name',
                           'owner_id', 'slug', 'list_id'],
1070 1071
            require_auth=True
        )
1072

kk6's avatar
kk6 committed
1073
    def add_list_members(self, screen_name=None, user_id=None, slug=None,
1074
                         list_id=None, owner_id=None, owner_screen_name=None):
1075
        """ Perform bulk add of list members from user ID or screenname """
kk6's avatar
kk6 committed
1076 1077
        return self._add_list_members(list_to_csv(screen_name),
                                      list_to_csv(user_id),
1078 1079 1080
                                      slug, list_id, owner_id,
                                      owner_screen_name)

1081 1082 1083
    @property
    def _add_list_members(self):
        """ :reference: https://dev.twitter.com/docs/api/1.1/post/lists/members/create_all
1084
            :allowed_param:'screen_name', 'user_id', 'slug', 'list_id',
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1085
            'owner_id', 'owner_screen_name'
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1086

1087 1088 1089 1090 1091 1092
        """
        return bind_api(
            api=self,
            path='/lists/members/create_all.json',
            method='POST',
            payload_type='list',
1093
            allowed_param=['screen_name', 'user_id', 'slug', 'list_id',
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1094
                           'owner_id', 'owner_screen_name'],
1095 1096 1097
            require_auth=True
        )

kk6's avatar
kk6 committed
1098
    def remove_list_members(self, screen_name=None, user_id=None, slug=None,
1099 1100
                            list_id=None, owner_id=None, owner_screen_name=None):
        """ Perform bulk remove of list members from user ID or screenname """
kk6's avatar
kk6 committed
1101
        return self._remove_list_members(list_to_csv(screen_name),
1102 1103 1104 1105 1106 1107 1108
                                         list_to_csv(user_id),
                                         slug, list_id, owner_id,
                                         owner_screen_name)

    @property
    def _remove_list_members(self):
        """ :reference: https://dev.twitter.com/docs/api/1.1/post/lists/members/destroy_all
1109
            :allowed_param:'screen_name', 'user_id', 'slug', 'list_id',
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1110
            'owner_id', 'owner_screen_name'
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1111

1112 1113 1114 1115 1116 1117
        """
        return bind_api(
            api=self,
            path='/lists/members/destroy_all.json',
            method='POST',
            payload_type='list',
1118
            allowed_param=['screen_name', 'user_id', 'slug', 'list_id',
Omer Murat Yildirim's avatar
Omer Murat Yildirim committed
1119
                           'owner_id', 'owner_screen_name'],
1120 1121
            require_auth=True
        )
1122