So I was running the streamingclient today and then it disconnected with this Stream encountered HTTP error: 409. I used my second account (so different bearer token) to test what was wrong but still getting the error. How can I fix it?
this is my code
import tweepy
import sys
import os
import codecs
import re
BEARER_TOKEN=""
DATA_FILE= "streaming_data5.csv"
if not os.path.exists(DATA_FILE):
os.mknod(DATA_FILE)
class BasicTwitterListener (tweepy.StreamingClient):
def __init__(self, n_tweets):
super().__init__(BEARER_TOKEN)
self.n_tweets = n_tweets
def on_tweet(self, tweet):
tweet.text =re.sub("[\r\n]+","", tweet.text) #print each tweet onto new line
print(tweet.created_at, tweet.text, tweet.author_id)
with codecs.open(DATA_FILE, "a") as f:
f.write("%s||%s||%s\n" % (tweet.created_at, tweet.text, tweet.author_id))
listener = BasicTwitterListener(10)
listener.add_rules(tweepy.StreamRule('"breaking news" OR "latest news" OR #breakingnews - AIRDROP n-is:retweet lang:en'))
listener.filter(expansions=["author_id"], tweet_fields=["created_at"])
What is the full error message from the API? There should be a json object with the full error returned: Twitter API Response Codes & Error Support | Twitter Developer Platform 409 isn’t listed specifically but maybe it’s related to reconnecting multiple times with the same credentials? Sometimes when deploying something, 2 instances are actually launched, in different threads in workers for example, so it can happen without you knowing.
Separate to the 409 error, you have an ambiguous rule:
"breaking news" OR "latest news" OR #breakingnews - AIRDROP n-is:retweet lang:en
For OR clauses, it’s best to use () parentheses to group the operators, otherwise you would get unexpected matches - as a space is an implicit AND. There in an extra space between - and AIRDROP too, not sure if that’s intentional.
Maybe more like:
'("breaking news" OR "latest news" OR #breakingnews) -AIRDROP -is:retweet lang:en'
1 Like