简体   繁体   English

如何干燥处理Twitter API速率限制

[英]How to DRY the handling of Twitter API rate limit

I have the following code pattern to handle various exceptions that arise while accessing Twitter API. 我有以下代码模式来处理在访问Twitter API时出现的各种异常。 The basic pattern is: 基本模式是:

while True:
    try:
        # 'access twitter api'
    except tweepy.error.TweepError:
        # 'handling the error'

I have multiple methods, eg get_tweets() , get_friends() , all of which use the same error handling pattern. 我有多种方法,例如get_tweets()get_friends() ,所有方法都使用相同的错误处理模式。 How to DRY this? 如何干燥呢?

For example, below is my get_tweets() method. 例如,下面是我的get_tweets()方法。 How to re-use the error handling in this method in others? 如何在其他方法中重用此方法中的错误处理?

def get_tweets(self, screen_name):        
    while True:
        try:
            rs = self.api.user_timeline(screen_name=screen_name)

        except tweepy.error.TweepError, e:
            if e.response.status_code == 404:
                print "%s does not exist" % (twitter_id)
                return None

            elif e.response.status_code == 429:
                print "out of quota"
                time.sleep(60 * 15)
                continue

            elif e.response.status_code == 503:
                print "Twitter down"
                time.sleep(60 * 15)
                continue

            else:
                print "%s, %s" % (twitter_id, e)
                continue

If the contents of the except statement is repeated multiple places it might a good candidate for some reuse. 如果except语句的内容在多个位置重复,则可能是重用的不错选择。

I see multiple ways you might do this, here are some options. 我看到您可以通过多种方式执行此操作,以下是一些选项。

  1. Define an error handler that can be used by multiple methods: 定义可以由多种方法使用的错误处理程序:
def get_tweets(self, screen_name):
    while True:
        try:
            rs = self.api.user_timeline(screen_name=screen_name)
        except tweepy.error.TweepError, e:
            error_handler(e)

def error_handler(self, e):
    # handle the error

Of course with this option we end up repeating 当然有了这个选项,我们最终会重复

while True:
    try:
        # access twitter api
    except:
        # handle error

2. Instead you can create a general twitter call function which accepts a method and some arguments for the method. 2.相反,您可以创建一个通用的twitter调用函数,该函数接受一个方法和该方法的一些参数。

def twitter_request(self, twitter_method, *args, **kwargs):
    while True:
        try:
            twitter_method(*args, **kwargs)
        except tweepy.error.TweepError, e:
            # handle the error

def get_tweets(self, screen_name):
    twitter_request(self.api.user_timeline, screen_name=screen_name)

These options can also be combined if you need the ability to replace the error handling function for some api calls by adding the error handler function as argument to twitter_request 如果您需要通过将错误处理函数添加为twitter_request参数来替换某些api调用的错误处理函数的能力,也可以组合使用这些选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM