Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
import tweepy
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= "xxxxx"
csecret="xxxx"
atoken="xxxxx"
asecret="xxxxx"

class listener(StreamListener):
    
    def on_data(self, data):
        all_data = json.loads(data)
        tweet = all_data["text"]       
        sentiment_value, confidence = s.sentiment(tweet)
        tweet.encode('utf-8', 'ignore')
        if "RT" in tweet:
            pass
        else:
            tweets=open("tweets.txt","a",encoding="utf-8")
            tweets.write(tweet)
            tweets.write('\n')
            tweets.write(str(sentiment_value))
            tweets.write('\n')
            tweets.write(str(confidence))
            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')
                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=['Cat'],languages=['en'])


What I have tried:

adding this line:
twitterStream.filter(track=['Cat'],locations=[51.4,22.23,56.59,25.76],languages=['en'])



but i am getting tweets from the country without following the keyword i am looking for
Posted
Updated 10-Feb-18 18:04pm
v3

1 solution

 
Share this answer
 
Comments
Member 13647869 11-Feb-18 2:16am    
I tried that, but it gives me the tweets from the location. I need the tweets with the keyword 'Cat' from the users in the location
Member 13647869 15-Feb-18 6:47am    
As far as I understand the relationship between the location and keyword is an OR relationship. the solution is to go to the module that allows you to stream and change the relationship to AND.Just letting this out there

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900