Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- current_location = 0
- while (True):
- class listener(StreamListener):
- num_tweets = 0
- max_tweets = 1
- def on_data(self, data):
- if self.num_tweets <= self.max_tweets:
- all_data = json.loads(data)
- tweet = all_data['text']
- username = all_data['user']['screen_name']
- sentiment = clf.predict([tweet])
- location = all_data['user']['location']
- created_at = all_data['created_at']
- print((created_at, username, tweet, sentiment,\
- locations[current_location]["name"],\
- locations[current_location]["lat_long"]), sentiment)
- created_at_0 = all_data['created_at']
- data = dict()
- str_date = created_at_0 #'Sun Nov 03 06:35:15 +0000 2019'
- dt = parse(str_date)
- created_at_1 = dt.strftime("%Y-%m-%dT%H:%M:%SZ")
- latlong = locations[current_location]["lat_long"]
- print( str(latlong[0]) +','+str(latlong[1]))
- data['city'] = locations[current_location]["name"]
- data['latlong'] = str(latlong[0]) +','+str(latlong[1])
- data['created_date'] = created_at_1
- data['username'] = username
- data['tweets'] = tweet
- data['sentiment'] = sentiment[0]
- solr = pysolr.Solr('http://88.99.253.49:666/solr/user1', timeout=10)
- solr.add([data])
- solr.commit()
- self.num_tweets = self.num_tweets + 1
- return True
- else:
- return False
- def on_error(self, status):
- print(status)
- auth = OAuthHandler(ckey, csecret)
- auth.set_access_token(atoken, asecret)
- twitterStream = Stream(auth, listener())
- # twitterStream.filter(track=["jakarta"])
- if current_location == len(locations):
- current_location = 0
- twitterStream.filter(locations=locations[current_location]["lat_long"])
- current_location = current_location + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement