heroku 如何修复Twitter API上的“此流当前已达到最大允许连接限制”错误?

ctrmrzij  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(137)

我通过Heroku使用Node.js运行了一个Discord机器人。我一直在实现Twitter API,以便从我们的一些频道中的一些帐户发布推文,但流在一段时间后偶尔崩溃。我知道我没有达到速率限制,而且据我所知,尽管错误说明了这一点,但并没有多个连接在运行。这是我在日志中看到的错误:

Rules: {
   meta: {
   sent: '2022-08-25T17:55:50.381Z',
   summary: { created: 0, not_created: 0, valid: 0, invalid: 0 }
  }
}
/app/node_modules/twitter-v2/build/TwitterError.js:26
return new module.exports(`${json.title}: ${json.detail}`, null, json.type);
TwitterError: ConnectionException: This stream is currently at the maximum allowed connection limit.

这是我在流中使用的代码:

var T = new Twit({
    bearer_token: process.env.BEARER_TOKEN,
    timeout_ms: 60*1000,
    strictSSL: true
})

async function listenForever(streamFactory, dataConsumer) {
      for await (const { data } of streamFactory()) {
        dataConsumer(data);
      }
      listenForever(streamFactory, dataConsumer);
  }
  
async function  setup () {
    const endpointParameters = {
        'tweet.fields': ['text'],
        'expansions': [ 'author_id'],
        'media.fields': [ 'url' ]
    }
    try {
      console.log('Setting up Twitter....')
      const body = {
        "add": [
          // {'value': 'from:account'},
          // {'value': 'from:account'},
          // {'value': 'from:account'},
          // {'value': 'from:account'}
        ]
        // "delete": {
            // ids: [""]
        // }
      }
      const r = await T.post("tweets/search/stream/rules", body);
      console.log("Rules:", r);
  
    } catch (err) {
      console.log(err)
    }
  
    listenForever(
      () => T.stream('tweets/search/stream', endpointParameters),
      (data) => sendMessage(data)
    );
}
7gyucuyw

7gyucuyw1#

我在使用此代码(based on tweepy)时遇到了与Twitter API v2相同的问题:

class CustomStreamingClient(tweepy.StreamingClient):
    def on_tweet(self, tweet):
        try:
            logger_stdout.info(f'{tweet.text}')
        except Exception as e:
            logger_stderr.warning('Custom Streaming Client error - {} - {}'.format(type(e).__name__, e))

def get_stream_rules():
    stream_rules = [tweepy.StreamRule(value=f'from: {ea}', tag=f'{ea}', id=f'{ea}') for ea in ids_to_follow_list]

    return stream_rules

def main():
    try:
        streaming_client = CustomStreamingClient(bearer_token)
        streaming_client.add_rules(add=get_stream_rules())

        streaming_client.filter(expansions=['author_id'])
    except Exception as e:
        logger_stderr.error('Stream error - {} - {}'.format(type(e).__name__, e))

if __name__ == '__main__':
    try:    
        main()
    except KeyboardInterrupt:
        logger_stderr.warning("""
        Closing ...
        """)

相关问题