Skip to content

Instantly share code, notes, and snippets.

@jmahabal
Created August 30, 2017 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmahabal/678d4d2efbec0bef26dd841dc6c14b01 to your computer and use it in GitHub Desktop.
Save jmahabal/678d4d2efbec0bef26dd841dc6c14b01 to your computer and use it in GitHub Desktop.
from sklearn.externals import joblib
import tweepy
import json
def predict(tweet_text):
# Load our model and vectorizer
clf = joblib.load('model.pkl')
vectorizer = joblib.load('vectorizer.pkl')
# Make the retweet count prediction after vectorizing the tweet
prediction = clf.predict(vectorizer.transform([tweet_text]).toarray())[0]
# Return the prediction
return {'prediction': prediction}
def getWeapon(prediction):
prediction = float(prediction)
if prediction < 100:
return "drop"
elif prediction < 500:
return "pellet"
elif prediction < 2000:
return "grenade"
elif prediction < 5000:
return "bomb"
else:
return "nuke"
with open('secrets.json') as data_file:
secrets = json.load(data_file)
auth = tweepy.OAuthHandler(secrets["consumer_key"], secrets["consumer_secret"])
auth.set_access_token(secrets["access_key"], secrets["access_secret"])
api = tweepy.API(auth)
public_tweets = api.home_timeline()
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
status_id = status.id
screenname = status.user.screen_name
if screenname == "wojespn":
if not hasattr(status, 'retweeted_status'):
prediction = predict(status.text)["prediction"]
# retweet with the tweet_id
api.update_status("#woj"+ getWeapon(prediction) + " (" + str(int(prediction)) + " expected retweets) " + " https://twitter.com/" + screenname + "/status/" + str(status_id))
def on_error(self, status_code):
if status_code == 420:
return False # returning False in on_data disconnects the stream
myStreamListener = MyStreamListener()
myStream = tweepy.Stream(auth = api.auth, listener = myStreamListener)
myStream.filter(follow=['50323173']) # Wojnarowski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment