Skip to content

Instantly share code, notes, and snippets.

@neuman
Created September 22, 2017 21:59
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 neuman/4ada6086dbe5275054bac0ac5d3ee282 to your computer and use it in GitHub Desktop.
Save neuman/4ada6086dbe5275054bac0ac5d3ee282 to your computer and use it in GitHub Desktop.
class TweetScraper(object):
def scrape(self, query, when=None, tokens=None):
output = []
if when == None:
when = (datetime.datetime.now() - datetime.timedelta(hours=24)).strftime('%Y-%m-%d')
for tweet in self.get_tweets_since(query, when, tokens):
blob = TextBlob(tweet['text'])
new_tweet = cm.Tweet(text=tweet['text'], url=tweet['id'], profile_image_url=tweet['user']['profile_image_url'], screen_name=tweet['user']['screen_name'], favourites_count=tweet['favorite_count'], retweet_count=tweet['retweet_count'], followers_count=tweet['user']['followers_count'], polarity=blob.polarity, subjectivity=blob.subjectivity)
new_tweet.tweet_id = tweet['id_str']
output.append(new_tweet)
return output
def get_authenticated_twython(self, tokens=None):
from twython import Twython
if tokens == None:
twython_instance = Twython(app_key=settings.TWITTER_APP_KEY,
app_secret=settings.TWITTER_APP_KEY_SECRET,
oauth_token=settings.TWITTER_ACCESS_TOKEN,
oauth_token_secret=settings.TWITTER_ACCESS_TOKEN_SECRET)
else:
twython_instance = Twython(app_key=settings.TWITTER_APP_KEY,
app_secret=settings.TWITTER_APP_KEY_SECRET,
oauth_token=tokens["oauth_token"],
oauth_token_secret=tokens["oauth_token_secret"])
return twython_instance
def get_tweets_since(self, query, when, tokens=None):
twython_instance = self.get_authenticated_twython(tokens)
#raise Exception(query)
#print query
search = twython_instance.search(q='#'+query, result_type='recent', since=when, count=100, filter='retweets')
tweets = search['statuses']
return tweets
def follow_users_who_tweet_about(self, query, count=500):
twython_instance = self.get_authenticated_twython()
results = twython_instance.search(q=query, count=count)
for t in results["statuses"]:
#print t["user"]["screen_name"]
twython_instance.create_friendship(screen_name=t["user"]["screen_name"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment