SHARE
TWEET

Untitled

a guest Nov 15th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. current_location = 0
  2.  
  3. while (True):
  4.     class listener(StreamListener):
  5.         num_tweets = 0
  6.         max_tweets = 1
  7.         def on_data(self, data):
  8.             if self.num_tweets <= self.max_tweets:
  9.                 all_data = json.loads(data)
  10.                 tweet = all_data['text']
  11.                 username = all_data['user']['screen_name']
  12.                 sentiment = clf.predict([tweet])
  13.                 location = all_data['user']['location']
  14.                 created_at = all_data['created_at']
  15.                 print((created_at, username, tweet, sentiment,\
  16.                        locations[current_location]["name"],\
  17.                        locations[current_location]["lat_long"]), sentiment)
  18.                 created_at_0 = all_data['created_at']
  19.  
  20.                 data = dict()
  21.                 str_date = created_at_0 #'Sun Nov 03 06:35:15 +0000 2019'
  22.                 dt = parse(str_date)
  23.  
  24.                 created_at_1 = dt.strftime("%Y-%m-%dT%H:%M:%SZ")
  25.                 latlong = locations[current_location]["lat_long"]
  26.                 print( str(latlong[0]) +','+str(latlong[1]))
  27.                 data['city'] = locations[current_location]["name"]
  28.                 data['latlong'] = str(latlong[0]) +','+str(latlong[1])
  29.                 data['created_date'] = created_at_1
  30.                 data['username'] = username
  31.                 data['tweets'] = tweet
  32.                 data['sentiment'] = sentiment[0]
  33.  
  34.                
  35.                 solr = pysolr.Solr('http://88.99.253.49:666/solr/user1', timeout=10)
  36.                 solr.add([data])
  37.                 solr.commit()
  38.                 self.num_tweets =  self.num_tweets + 1
  39.                 return True
  40.             else:
  41.                 return False
  42.  
  43.         def on_error(self, status):
  44.             print(status)
  45.  
  46.     auth = OAuthHandler(ckey, csecret)
  47.     auth.set_access_token(atoken, asecret)
  48.  
  49.     twitterStream = Stream(auth, listener())
  50. #     twitterStream.filter(track=["jakarta"])
  51.  
  52.     if current_location == len(locations):
  53.         current_location = 0
  54.     twitterStream.filter(locations=locations[current_location]["lat_long"])
  55.     current_location = current_location + 1
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top