Hey!
I am working on twitter analysis with python. I have the code running and everything looks good, however, when I add the tweet to the tweets file it repeats existing tweet, how can I fix that?
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import sentmod as s
#consumer key, consumer secret, access token, access secret.
ckey= "PpwBFRmltifRyYA57YcYtD5Sg"
csecret="BGQGAGeGHQwjoEF5OW2LSfbvoGyDm6qEDNftQxsyZwNVgylj31"
atoken="953938307855069185-6gJncM43PJmIZAsuM8MVrvyzRBhBjus"
asecret="SfMuQo9UKrTY9jXiBjEE1S9NoreAE8HSx3tEfiSPqzTkU"
class listener(StreamListener):
def on_data(self, data):
all_data = json.loads(data)
tweet = all_data["text"]
sentiment_value, confidence = s.sentiment(tweet)
tweets= open("tweets.txt","a",encoding="utf-8")
tweets.write(tweet)
tweets.write('\n\n\n')
tweets.close()
print(tweet,sentiment_value, confidence)
if confidence*100 >= 60:
output = open("twitter-out.txt","a")
output.write(sentiment_value)
output.write('\n\n\n')
output.close()
return True
def on_error(self, status):
print(status)
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=["Siemens"],languages=['en'])
Any help please?
What I have tried:
Normal loops but i don't know how to apply them here