当前位置: 首页>>代码示例>>Python>>正文


Python Twython.get_application_rate_limit_status方法代码示例

本文整理汇总了Python中twython.Twython.get_application_rate_limit_status方法的典型用法代码示例。如果您正苦于以下问题:Python Twython.get_application_rate_limit_status方法的具体用法?Python Twython.get_application_rate_limit_status怎么用?Python Twython.get_application_rate_limit_status使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twython.Twython的用法示例。


在下文中一共展示了Twython.get_application_rate_limit_status方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: wrapper

# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_application_rate_limit_status [as 别名]
        def wrapper(*args, **kwargs):
            """
            A decorator function to provide Twitter API rate limiting.

            A request is made to the API to get the number
            of remaining requests. If no requests remain then the
            current thread must wait until the specified reset time.

            See: https://dev.twitter.com/rest/public/rate-limits

            E.g.

            @limiter("users/lookup")
            def lookup_user():
                something()

            """

            cr = [app_key, app_secret, auth_token, auth_secret]
            twitter = Twython(*cr)
            resource = request.split("/")[0]

            # rate_limit_status has rate limit
            a = twitter.get_application_rate_limit_status()

            b = a["resources"]
            b = b["application"]
            b = b["/application/rate_limit_status"]

            c = a["resources"]
            c = c[resource]
            c = c["/%s" % request]

            now = time()

            # respect rate_limit_status limits
            if b["remaining"] == 0:
                # wait for reset
                reset = b["reset"]
                notify(reset)
                sleep(reset - now)

            # respect requested limits
            if c["remaining"] == 0:
                # wait for reset
                reset = c["reset"]
                notify(reset)
                sleep(reset - now)

            return f(*args, **kwargs)
开发者ID:taylorrees,项目名称:penemue,代码行数:52,代码来源:limiter.py

示例2: TwitterHelper

# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_application_rate_limit_status [as 别名]

#.........这里部分代码省略.........
            place = result["result"]["places"][0]
            location.place_id_twitter = place["id"]

            return location
        else:
            return None

    @retry(**retry_args)
    def update_profile_image(self, file_path):
        if file_path:
            logger.info("updating profile image %s" % file_path)
            with open(file_path, 'rb') as file:
                self.twitter.update_profile_image(image=file)

    @retry(**retry_args)
    def update_profile_banner_image(self, file_path):
        if file_path:
            logger.info("updating banner image %s" % file_path)
            with open(file_path, 'rb') as file:
                try:
                    self.twitter.update_profile_banner_image(banner=file)
                except TwythonError as ex:
                    if "Response was not valid JSON" in str(ex):
                        # twython issue i think
                        logger.warning(ex)
                    else:
                        raise

    @retry(**retry_args)
    def update_profile(self, **kwargs):
        return self.twitter.update_profile(**kwargs)

    @retry(**retry_args)
    def get_list_statuses(self, **kwargs):
        return self.twitter.get_list_statuses(**kwargs)

    @retry(**retry_args)
    def get_user_suggestions_by_slug(self, **kwargs):
        return self.twitter.get_user_suggestions_by_slug(**kwargs)

    @retry(**retry_args)
    def get_user_suggestions(self, **kwargs):
        return self.twitter.get_user_suggestions(**kwargs)

    @retry(**retry_args)
    def lookup_status(self, **kwargs):
        return self.twitter.lookup_status(**kwargs)

    @retry(**retry_args)
    def get_followers(self, **kwargs):
        kwargs["stringify_ids"] = True
        followers = set()
        cursor = -1
        while cursor != "0":
            kwargs["cursor"] = cursor
            logger.info("getting followers")
            response = self.twitter.get_followers_ids(**kwargs)
            cursor = response["next_cursor_str"]
            followers = followers.union(set(response["ids"]))

        return followers

    @retry(**retry_args)
    def get_following(self, **kwargs):
        kwargs["stringify_ids"] = True
        following = set()
        cursor = -1
        while cursor != "0":
            kwargs["cursor"] = cursor
            logger.info("getting following")
            response = self.twitter.get_friends_ids(**kwargs)
            cursor = response["next_cursor_str"]
            following = following.union(set(response["ids"]))
        return following

    @retry(**retry_args)
    def update_rate_limits(self):
        data = self.twitter.get_application_rate_limit_status()
        self.rates = RateLimits(data)
        logger.info("Updated rate limits for {}: {}".format(self.identity.screen_name, self.rates.display()))

    def _rate_limit(self, limit_name, func, *args, **kwargs):
        if self.rates.can(limit_name):
            try:
                return func(*args, **kwargs)
            except Exception as ex:
                logger.warning(ex)
                return None
        else:
            logger.warning("{} limit exceeded".format(limit_name))
            return None

    @retry(**retry_args)
    def get_statuses(self, id_strs):
        id_strs_csv = ",".join(id_strs)
        return self.twitter.lookup_status(id=id_strs_csv)

    @retry(**retry_args)
    def get_status(self, id_str):
        return self.twitter.show_status(id=id_str)
开发者ID:andrewtatham,项目名称:twitterpibot,代码行数:104,代码来源:twitterhelper.py

示例3: TwythonAPITestCase

# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_application_rate_limit_status [as 别名]

#.........这里部分代码省略.........
        # Multi add/delete members
        self.api.create_list_members(list_id=list_id,
                                     screen_name=screen_names)
        self.api.delete_list_members(list_id=list_id,
                                     screen_name=screen_names)

        # Single add/delete member
        self.api.add_list_member(list_id=list_id, screen_name='justinbieber')
        self.api.delete_list_member(list_id=list_id, screen_name='justinbieber')

        self.api.delete_list(list_id=list_id)

    def test_get_list_subscribers(self):
        """Test list of subscribers of a specific list succeeds"""
        self.api.get_list_subscribers(list_id=test_list_id)

    def test_subscribe_is_subbed_and_unsubscribe_to_list(self):
        """Test subscribing, is a list sub and unsubbing to list succeeds"""
        self.api.subscribe_to_list(list_id=test_list_id)
        # Returns 404 if user is not a subscriber
        self.api.is_list_subscriber(list_id=test_list_id,
                                    screen_name=screen_name)
        self.api.unsubscribe_from_list(list_id=test_list_id)

    def test_is_list_member(self):
        """Test returning if specified user is member of a list succeeds"""
        # Returns 404 if not list member
        self.api.is_list_member(list_id=test_list_id, screen_name='jack')

    def test_get_list_members(self):
        """Test listing members of the specified list succeeds"""
        self.api.get_list_members(list_id=test_list_id)

    def test_get_specific_list(self):
        """Test getting specific list succeeds"""
        self.api.get_specific_list(list_id=test_list_id)

    def test_get_list_subscriptions(self):
        """Test collection of the lists the specified user is
        subscribed to succeeds"""
        self.api.get_list_subscriptions(screen_name='twitter')

    def test_show_owned_lists(self):
        """Test collection of lists the specified user owns succeeds"""
        self.api.show_owned_lists(screen_name='twitter')

    # Saved Searches
    def test_get_saved_searches(self):
        """Test getting list of saved searches for authenticated
        user succeeds"""
        self.api.get_saved_searches()

    def test_create_get_destroy_saved_search(self):
        """Test getting list of saved searches for authenticated
        user succeeds"""
        saved_search = self.api.create_saved_search(query='#Twitter')
        saved_search_id = saved_search['id_str']

        self.api.show_saved_search(id=saved_search_id)
        self.api.destroy_saved_search(id=saved_search_id)

    # Places & Geo
    def test_get_geo_info(self):
        """Test getting info about a geo location succeeds"""
        self.api.get_geo_info(place_id='df51dec6f4ee2b2c')

    def test_reverse_geo_code(self):
        """Test reversing geocode succeeds"""
        self.api.reverse_geocode(lat='37.76893497', long='-122.42284884')

    def test_search_geo(self):
        """Test search for places that can be attached
        to a statuses/update succeeds"""
        self.api.search_geo(query='Toronto')

    def test_get_similar_places(self):
        """Test locates places near the given coordinates which
        are similar in name succeeds"""
        self.api.get_similar_places(lat='37', long='-122', name='Twitter HQ')

    # Trends
    def test_get_place_trends(self):
        """Test getting the top 10 trending topics for a specific
        WOEID succeeds"""
        self.api.get_place_trends(id=1)

    def test_get_available_trends(self):
        """Test returning locations that Twitter has trending
        topic information for succeeds"""
        self.api.get_available_trends()

    def test_get_closest_trends(self):
        """Test getting the locations that Twitter has trending topic
        information for, closest to a specified location succeeds"""
        self.api.get_closest_trends(lat='37', long='-122')

    # Help
    def test_get_application_rate_limit_status(self):
        """Test getting application rate limit status succeeds"""
        self.oauth2_api.get_application_rate_limit_status()
开发者ID:hungtrv,项目名称:twython,代码行数:104,代码来源:test_core.py

示例4: TwythonEndpointsTestCase

# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_application_rate_limit_status [as 别名]

#.........这里部分代码省略.........
    def test_get_specific_list(self):
        """Test getting specific list succeeds"""
        self.api.get_specific_list(slug=test_list_slug,
                                   owner_screen_name=test_list_owner_screen_name)

    @unittest.skip('skipping non-updated test')
    def test_get_list_subscriptions(self):
        """Test collection of the lists the specified user is
        subscribed to succeeds"""
        self.api.get_list_subscriptions(screen_name='twitter')

    @unittest.skip('skipping non-updated test')
    def test_show_owned_lists(self):
        """Test collection of lists the specified user owns succeeds"""
        self.api.show_owned_lists(screen_name='twitter')

    # Saved Searches
    @unittest.skip('skipping non-updated test')
    def test_get_saved_searches(self):
        """Test getting list of saved searches for authenticated
        user succeeds"""
        self.api.get_saved_searches()

    @unittest.skip('skipping non-updated test')
    def test_create_get_destroy_saved_search(self):
        """Test getting list of saved searches for authenticated
        user succeeds"""
        saved_search = self.api.create_saved_search(query='#Twitter')
        saved_search_id = saved_search['id_str']

        self.api.show_saved_search(id=saved_search_id)
        self.api.destroy_saved_search(id=saved_search_id)

    # Places & Geo
    @unittest.skip('skipping non-updated test')
    def test_get_geo_info(self):
        """Test getting info about a geo location succeeds"""
        self.api.get_geo_info(place_id='df51dec6f4ee2b2c')

    @unittest.skip('skipping non-updated test')
    def test_reverse_geo_code(self):
        """Test reversing geocode succeeds"""
        self.api.reverse_geocode(lat='37.76893497', long='-122.42284884')

    @unittest.skip('skipping non-updated test')
    def test_search_geo(self):
        """Test search for places that can be attached
        to a statuses/update succeeds"""
        self.api.search_geo(query='Toronto')

    @unittest.skip('skipping non-updated test')
    def test_get_similar_places(self):
        """Test locates places near the given coordinates which
        are similar in name succeeds"""
        self.api.get_similar_places(lat='37', long='-122', name='Twitter HQ')

    # Trends
    @unittest.skip('skipping non-updated test')
    def test_get_place_trends(self):
        """Test getting the top 10 trending topics for a specific
        WOEID succeeds"""
        self.api.get_place_trends(id=1)

    @unittest.skip('skipping non-updated test')
    def test_get_available_trends(self):
        """Test returning locations that Twitter has trending
        topic information for succeeds"""
        self.api.get_available_trends()

    @unittest.skip('skipping non-updated test')
    def test_get_closest_trends(self):
        """Test getting the locations that Twitter has trending topic
        information for, closest to a specified location succeeds"""
        self.api.get_closest_trends(lat='37', long='-122')

    # Help
    @unittest.skip('skipping non-updated test')
    def test_get_twitter_configuration(self):
        """Test getting Twitter's configuration succeeds"""
        self.api.get_twitter_configuration()

    @unittest.skip('skipping non-updated test')
    def test_get_supported_languages(self):
        """Test getting languages supported by Twitter succeeds"""
        self.api.get_supported_languages()

    @unittest.skip('skipping non-updated test')
    def test_privacy_policy(self):
        """Test getting Twitter's Privacy Policy succeeds"""
        self.api.get_privacy_policy()

    @unittest.skip('skipping non-updated test')
    def test_get_tos(self):
        """Test getting the Twitter Terms of Service succeeds"""
        self.api.get_tos()

    @unittest.skip('skipping non-updated test')
    def test_get_application_rate_limit_status(self):
        """Test getting application rate limit status succeeds"""
        self.oauth2_api.get_application_rate_limit_status()
开发者ID:JohnCouscous,项目名称:twython,代码行数:104,代码来源:test_endpoints.py

示例5: len

# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_application_rate_limit_status [as 别名]
l = len(sys.argv)

if l == 1:
    limits = input("Enter the resources to check (separated by spaces): ")
elif l == 2:
    limits = sys.argv[1]
elif l > 2:
    limits = sys.argv[1:]
else:
    limits = input("Enter the resources to check (separated by spaces): ")


if limits.lower() == "all":
    limited = "application,favorites,followers,friends,lists,search,statuses,users,help"
else:
    limit = limits.split()
    limited = ",".join(limit)


num = len(limits)
now = dd.utcnow()

try:
    ltd = twitter.get_application_rate_limit_status(resources=limited)
except TwythonError as e:
    print(e)
    pass

pp(ltd)
print("Valid at:  {0} UTC".format(now.isoformat()))
开发者ID:Hasimir,项目名称:twython-tools,代码行数:32,代码来源:view-rate-limit.py

示例6: __init__

# 需要导入模块: from twython import Twython [as 别名]
# 或者: from twython.Twython import get_application_rate_limit_status [as 别名]
class ParserREST:
    def __init__ (self, config):
        self.config = config
        self.db_cursor = DBCursor(self.config['database'])
        self.twitter = Twython(self.config['twitter']['appkeys']['app_key'],
                            self.config['twitter']['appkeys']['app_secret'],
                            self.config['twitter']['appkeys']['oauth_token'],
                            self.config['twitter']['appkeys']['oauth_token_secret'])
        
        self.time_start = time.time()
        try:
            data = self.twitter.get_application_rate_limit_status(resources = 'statuses')
            self.timeline_request_counter = (data['resources']['statuses']
                                                ['/statuses/user_timeline']['remaining'])
        except TwythonError as e:
            self.timeline_request_counter = int(self.config['twitter']
                                                    ['user_timeline']['limit_user'])
            logging.exception('ParserREST.__init__: {}'.format(e))

    def parse(self):
        self.process = True
        while self.process:
            users_list = self.db_cursor.get_parse_user_list(
                            last_time = int(self.config['twitter']['user_in_base']['timeout']),
                            list_size = int(self.config['twitter']['user_in_base']['list_limit']))
            if users_list:
                for i in users_list:
                    self.parse_twitter_user(i[0])
            else:
                self.process = False
    
    def parse_twitter_user(self,user_id):
        logging.info('user_id: {}'.format(user_id))
        result = 0
        try:
            statistic = self.db_cursor.select_user_statistic(user_id)
            if statistic is None:
                user = self.twitter.show_user(user_id=user_id)
                self.db_cursor.add_user_data(user)
                if user['statuses_count'] == 0:
                    self.db_cursor.add_user_data({'last_tweet_id':1,
                                                'last_statuses_count':0,
                                                'user_id':user_id})
                    return
                else:
                    result = self.get_user_timeline(user_id, 1)
                    self.db_cursor.update_user_statistic({
                                'last_tweet_id':result['last_id'],
                                'last_statuses_count':result['statuses_count'],
                                'user_id':user_id})
            else:
                result = self.get_user_timeline(user_id, statistic['last_tweet_id'])
                self.db_cursor.update_user_statistic({
                            'last_tweet_id':result['last_id'],
                            'last_statuses_count':result['statuses_count'] +
                                                statistic['statuses_count'],
                            'user_id':user_id})
        except TwythonError as e:
            logging.exception('ParserREST.parse_twitter_user: {} Data: {}'.format(e, result))
            return
    
    def get_user_timeline(self, user_id, since_id):
        statuses_count = 0
        if since_id > 1:
            since_id_tmp = since_id - 1
        else:
            since_id_tmp = 1
        first = True
        process = True
        last_id = since_id
        while process:
            current_time = time.time()
            if ((current_time - self.time_start) >
                    float(self.config['twitter']['time_window'])):
                self.time_start = current_time
                self.timeline_request_counter = int(self.config['twitter']
                                                    ['user_timeline']['limit_user'])
            if self.timeline_request_counter == 0:
                logging.info('Have to wait.')
                time.sleep(float(self.config['twitter']['time_window']) - (current_time - self.time_start))
            else:
                self.timeline_request_counter = self.timeline_request_counter - 1
                try:
                    if first:
                        user_timeline = self.twitter.get_user_timeline(
                                                user_id = user_id, 
                                                count = int(self.config['twitter']
                                                        ['user_timeline']['page_size']),
                                                since_id = since_id_tmp,
                                                exclude_replies = True,
                                                include_rts = False,
                                                trim_user = True)
                    else:
                        user_timeline = self.twitter.get_user_timeline(
                                                user_id = user_id, 
                                                count = int(self.config['twitter']
                                                        ['user_timeline']['page_size']),
                                                since_id = since_id_tmp,
                                                max_id = max_id,
                                                exclude_replies = True,
#.........这里部分代码省略.........
开发者ID:GeoTwiter,项目名称:GeoTwitter,代码行数:103,代码来源:harvester.py


注:本文中的twython.Twython.get_application_rate_limit_status方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。