Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Last active September 13, 2021 00:04
Show Gist options
  • Select an option

  • Save marcosan93/fb1424ce07c2c2bf52fc7e118c4fae87 to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/fb1424ce07c2c2bf52fc7e118c4fae87 to your computer and use it in GitHub Desktop.
def getTweets(search_term, until, limit=20):
"""
Configures Twint and returns a dataframe of tweets for a specific day.
"""
# Configuring Twint for search
c = twint.Config()
# The limit of tweets to retrieve
c.Limit = limit
# Search term
c.Search = search_term
# Removing retweets
c.Filter_retweets = True
# Popular tweets
c.Popular_tweets = True
# Lowercasing tweets
c.Lowercase = True
# English only
c.Lang = 'en'
# Tweets until a specified date
c.Until = until + " 00:00:00"
# Making the results pandas friendly
c.Pandas = True
# Stopping print in terminal
c.Hide_output = True
# Searching
twint.run.Search(c)
# Assigning the DF
df = twint.storage.panda.Tweets_df
# Returning an empty DF if no tweets were found
if len(df)<=0:
return pd.DataFrame()
# Formatting the date
df['date'] = df['date'].apply(lambda x: x.split(" ")[0])
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment