示例#1
0
 def update_profile_background_image(self, filename, *args, **kargs):
     headers, post_data = API._pack_image(filename, 800)
     bind_api(path='/account/update_profile_background_image.json',
              method='POST',
              payload_type='user',
              allowed_param=['tile'],
              require_auth=True)(self, post_data=post_data, headers=headers)
示例#2
0
 def test(self):
     try:
         bind_api(
             path = '/help/test.json',
         )(self)
     except TweepError:
         return False
     return True
示例#3
0
 def update_profile_background_image(self, filename, *args, **kargs):
     headers, post_data = API._pack_image(filename, 800)
     bind_api(
         path = '/account/update_profile_background_image.json',
         method = 'POST',
         payload_type = 'user',
         allowed_param = ['tile'],
         require_auth = True
     )(self, post_data=post_data, headers=headers)
示例#4
0
 def update_profile_image(self, filename):
     headers, post_data = API._pack_image(filename, 700)
     return bind_api(path='/account/update_profile_image.json',
                     method='POST',
                     payload_type='user',
                     require_auth=True)(self,
                                        post_data=post_data,
                                        headers=headers)
示例#5
0
 def update_profile_image(self, filename):
     headers, post_data = API._pack_image(filename, 700)
     return bind_api(
         path = '/account/update_profile_image.json',
         method = 'POST',
         payload_type = 'user',
         require_auth = True
     )(self, post_data=post_data, headers=headers)
示例#6
0
 def verify_credentials(self, **kargs):
     try:
         return bind_api(
             path='/account/verify_credentials.json',
             payload_type='user',
             require_auth=True,
             allowed_param=['include_entities', 'skip_status'],
         )(self, **kargs)
     except TweepError, e:
         if e.response and e.response.status == 401:
             return False
         raise
示例#7
0
 def verify_credentials(self, **kargs):
     try:
         return bind_api(
             path = '/account/verify_credentials.json',
             payload_type = 'user',
             require_auth = True,
             allowed_param = ['include_entities', 'skip_status'],
         )(self, **kargs)
     except TweepError, e:
         if e.response and e.response.status == 401:
             return False
         raise
示例#8
0
 def test(self):
     try:
         bind_api(path='/help/test.json', )(self)
     except TweepError:
         return False
     return True
示例#9
0
class API(object):
    """Twitter API"""
    def __init__(self,
                 auth_handler=None,
                 host='api.twitter.com',
                 search_host='search.twitter.com',
                 cache=None,
                 secure=True,
                 api_root='/1.1',
                 search_root='',
                 retry_count=0,
                 retry_delay=0,
                 retry_errors=None,
                 timeout=60,
                 parser=None,
                 compression=False):
        self.auth = auth_handler
        self.host = host
        self.search_host = search_host
        self.api_root = api_root
        self.search_root = search_root
        self.cache = cache
        self.secure = secure
        self.compression = compression
        self.retry_count = retry_count
        self.retry_delay = retry_delay
        self.retry_errors = retry_errors
        self.timeout = timeout
        self.parser = parser or ModelParser()

    """ statuses/home_timeline """
    home_timeline = bind_api(path='/statuses/home_timeline.json',
                             payload_type='status',
                             payload_list=True,
                             allowed_param=['since_id', 'max_id', 'count'],
                             require_auth=True)
    """ statuses/user_timeline """
    user_timeline = bind_api(path='/statuses/user_timeline.json',
                             payload_type='status',
                             payload_list=True,
                             allowed_param=[
                                 'id', 'user_id', 'screen_name', 'since_id',
                                 'max_id', 'count', 'include_rts'
                             ])
    """ statuses/mentions """
    mentions_timeline = bind_api(path='/statuses/mentions_timeline.json',
                                 payload_type='status',
                                 payload_list=True,
                                 allowed_param=['since_id', 'max_id', 'count'],
                                 require_auth=True)
    """/statuses/:id/retweeted_by.format"""
    retweeted_by = bind_api(path='/statuses/{id}/retweeted_by.json',
                            payload_type='status',
                            payload_list=True,
                            allowed_param=['id', 'count', 'page'],
                            require_auth=True)
    """/related_results/show/:id.format"""
    related_results = bind_api(path='/related_results/show/{id}.json',
                               payload_type='relation',
                               payload_list=True,
                               allowed_param=['id'],
                               require_auth=False)
    """/statuses/:id/retweeted_by/ids.format"""
    retweeted_by_ids = bind_api(path='/statuses/{id}/retweeted_by/ids.json',
                                payload_type='ids',
                                allowed_param=['id', 'count', 'page'],
                                require_auth=True)
    """ statuses/retweets_of_me """
    retweets_of_me = bind_api(path='/statuses/retweets_of_me.json',
                              payload_type='status',
                              payload_list=True,
                              allowed_param=['since_id', 'max_id', 'count'],
                              require_auth=True)
    """ statuses/show """
    get_status = bind_api(path='/statuses/show.json',
                          payload_type='status',
                          allowed_param=['id'])
    """ statuses/update """
    update_status = bind_api(path='/statuses/update.json',
                             method='POST',
                             payload_type='status',
                             allowed_param=[
                                 'status', 'in_reply_to_status_id', 'lat',
                                 'long', 'source', 'place_id'
                             ],
                             require_auth=True)
    """ statuses/destroy """
    destroy_status = bind_api(path='/statuses/destroy/{id}.json',
                              method='POST',
                              payload_type='status',
                              allowed_param=['id'],
                              require_auth=True)
    """ statuses/retweet """
    retweet = bind_api(path='/statuses/retweet/{id}.json',
                       method='POST',
                       payload_type='status',
                       allowed_param=['id'],
                       require_auth=True)
    """ statuses/retweets """
    retweets = bind_api(path='/statuses/retweets/{id}.json',
                        payload_type='status',
                        payload_list=True,
                        allowed_param=['id', 'count'],
                        require_auth=True)
    """ users/show """
    get_user = bind_api(path='/users/show.json',
                        payload_type='user',
                        allowed_param=['id', 'user_id', 'screen_name'])
    ''' statuses/oembed '''
    get_oembed = bind_api(path='/statuses/oembed.json',
                          payload_type='json',
                          allowed_param=[
                              'id', 'url', 'maxwidth', 'hide_media',
                              'omit_script', 'align', 'related', 'lang'
                          ])
    """ Perform bulk look up of users from user ID or screenname """

    def lookup_users(self, user_ids=None, screen_names=None):
        return self._lookup_users(list_to_csv(user_ids),
                                  list_to_csv(screen_names))

    _lookup_users = bind_api(
        path='/users/lookup.json',
        payload_type='user',
        payload_list=True,
        allowed_param=['user_id', 'screen_name'],
    )
    """ Get the authenticated user """

    def me(self):
        return self.get_user(screen_name=self.auth.get_username())

    """ users/search """
    search_users = bind_api(path='/users/search.json',
                            payload_type='user',
                            payload_list=True,
                            require_auth=True,
                            allowed_param=['q', 'per_page', 'page'])
    """ users/suggestions/:slug """
    suggested_users = bind_api(path='/users/suggestions/{slug}.json',
                               payload_type='user',
                               payload_list=True,
                               require_auth=True,
                               allowed_param=['slug', 'lang'])
    """ users/suggestions """
    suggested_categories = bind_api(path='/users/suggestions.json',
                                    payload_type='category',
                                    payload_list=True,
                                    allowed_param=['lang'],
                                    require_auth=True)
    """ users/suggestions/:slug/members """
    suggested_users_tweets = bind_api(
        path='/users/suggestions/{slug}/members.json',
        payload_type='status',
        payload_list=True,
        allowed_param=['slug'],
        require_auth=True)
    """ direct_messages """
    direct_messages = bind_api(path='/direct_messages.json',
                               payload_type='direct_message',
                               payload_list=True,
                               allowed_param=['since_id', 'max_id', 'count'],
                               require_auth=True)
    """ direct_messages/show """
    get_direct_message = bind_api(path='/direct_messages/show/{id}.json',
                                  payload_type='direct_message',
                                  allowed_param=['id'],
                                  require_auth=True)
    """ direct_messages/sent """
    sent_direct_messages = bind_api(
        path='/direct_messages/sent.json',
        payload_type='direct_message',
        payload_list=True,
        allowed_param=['since_id', 'max_id', 'count', 'page'],
        require_auth=True)
    """ direct_messages/new """
    send_direct_message = bind_api(
        path='/direct_messages/new.json',
        method='POST',
        payload_type='direct_message',
        allowed_param=['user', 'screen_name', 'user_id', 'text'],
        require_auth=True)
    """ direct_messages/destroy """
    destroy_direct_message = bind_api(path='/direct_messages/destroy.json',
                                      method='DELETE',
                                      payload_type='direct_message',
                                      allowed_param=['id'],
                                      require_auth=True)
    """ friendships/create """
    create_friendship = bind_api(
        path='/friendships/create.json',
        method='POST',
        payload_type='user',
        allowed_param=['id', 'user_id', 'screen_name', 'follow'],
        require_auth=True)
    """ friendships/destroy """
    destroy_friendship = bind_api(
        path='/friendships/destroy.json',
        method='DELETE',
        payload_type='user',
        allowed_param=['id', 'user_id', 'screen_name'],
        require_auth=True)
    """ friendships/show """
    show_friendship = bind_api(path='/friendships/show.json',
                               payload_type='friendship',
                               allowed_param=[
                                   'source_id', 'source_screen_name',
                                   'target_id', 'target_screen_name'
                               ])
    """ Perform bulk look up of friendships from user ID or screenname """

    def lookup_friendships(self, user_ids=None, screen_names=None):
        return self._lookup_friendships(list_to_csv(user_ids),
                                        list_to_csv(screen_names))

    _lookup_friendships = bind_api(path='/friendships/lookup.json',
                                   payload_type='relationship',
                                   payload_list=True,
                                   allowed_param=['user_id', 'screen_name'],
                                   require_auth=True)
    """ friends/ids """
    friends_ids = bind_api(
        path='/friends/ids.json',
        payload_type='ids',
        allowed_param=['id', 'user_id', 'screen_name', 'cursor'])
    """ friends/list """
    friends = bind_api(
        path='/friends/list.json',
        payload_type='user',
        payload_list=True,
        allowed_param=['id', 'user_id', 'screen_name', 'cursor'])
    """ friendships/incoming """
    friendships_incoming = bind_api(path='/friendships/incoming.json',
                                    payload_type='ids',
                                    allowed_param=['cursor'])
    """ friendships/outgoing"""
    friendships_outgoing = bind_api(path='/friendships/outgoing.json',
                                    payload_type='ids',
                                    allowed_param=['cursor'])
    """ followers/ids """
    followers_ids = bind_api(
        path='/followers/ids.json',
        payload_type='ids',
        allowed_param=['id', 'user_id', 'screen_name', 'cursor'])
    """ followers/list """
    followers = bind_api(
        path='/followers/list.json',
        payload_type='user',
        payload_list=True,
        allowed_param=['id', 'user_id', 'screen_name', 'cursor'])
    """ account/verify_credentials """

    def verify_credentials(self, **kargs):
        try:
            return bind_api(
                path='/account/verify_credentials.json',
                payload_type='user',
                require_auth=True,
                allowed_param=['include_entities', 'skip_status'],
            )(self, **kargs)
        except TweepError, e:
            if e.response and e.response.status == 401:
                return False
            raise
示例#10
0
    def verify_credentials(self, **kargs):
        try:
            return bind_api(
                path='/account/verify_credentials.json',
                payload_type='user',
                require_auth=True,
                allowed_param=['include_entities', 'skip_status'],
            )(self, **kargs)
        except TweepError, e:
            if e.response and e.response.status == 401:
                return False
            raise

    """ account/rate_limit_status """
    rate_limit_status = bind_api(path='/application/rate_limit_status.json',
                                 payload_type='json',
                                 allowed_param=['resources'],
                                 use_cache=False)
    """ account/update_delivery_device """
    set_delivery_device = bind_api(path='/account/update_delivery_device.json',
                                   method='POST',
                                   allowed_param=['device'],
                                   payload_type='user',
                                   require_auth=True)
    """ account/update_profile_colors """
    update_profile_colors = bind_api(
        path='/account/update_profile_colors.json',
        method='POST',
        payload_type='user',
        allowed_param=[
            'profile_background_color', 'profile_text_color',
            'profile_link_color', 'profile_sidebar_fill_color',
示例#11
0
        try:
            return bind_api(
                path = '/account/verify_credentials.json',
                payload_type = 'user',
                require_auth = True,
                allowed_param = ['include_entities', 'skip_status'],
            )(self, **kargs)
        except TweepError, e:
            if e.response and e.response.status == 401:
                return False
            raise

    """ account/rate_limit_status """
    rate_limit_status = bind_api(
        path = '/application/rate_limit_status.json',
        payload_type = 'json',
        allowed_param = ['resources'],
        use_cache = False
    )

    """ account/update_delivery_device """
    set_delivery_device = bind_api(
        path = '/account/update_delivery_device.json',
        method = 'POST',
        allowed_param = ['device'],
        payload_type = 'user',
        require_auth = True
    )

    """ account/update_profile_colors """
    update_profile_colors = bind_api(
        path = '/account/update_profile_colors.json',