Skip to content

Instantly share code, notes, and snippets.

@samuelteixeiras
Last active March 2, 2022 17:03
Show Gist options
  • Save samuelteixeiras/f4e1966b5ca9d46c9f72213e9f5d1d25 to your computer and use it in GitHub Desktop.
Save samuelteixeiras/f4e1966b5ca9d46c9f72213e9f5d1d25 to your computer and use it in GitHub Desktop.
from twitter_auth import API_KEY, API_SECRET
import tweepy as tp
from datetime import datetime
import csv
import time
SOURCE_KEYWORDS = 'ireland alcohol' + ' -filter:retweets'
auth = tp.OAuth2AppHandler(
API_KEY, API_SECRET
)
api = tp.API(auth)
def save_to_csv(tweets):
outtweets = [[tweet.id_str, tweet.created_at, tweet.full_text] for tweet in tweets]
now = datetime.now()
formatted_now = now.strftime("%m_%d_%Y_%H_%M_%S")
#write the csv
with open(f'new_{formatted_now}_tweets.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(["id","created_at","text"])
writer.writerows(outtweets)
def get_tweets():
tweets = api.search_tweets(q=SOURCE_KEYWORDS, count=100, tweet_mode="extended")
print(tweets.count)
save_to_csv(tweets)
def background_process():
while True:
formatted_now = datetime.now().strftime("%m_%d_%Y_%H_%M_%S")
print(f"running {formatted_now}")
get_tweets()
# run every 5 minutes
time.sleep(60 * 5)
if __name__ == "__main__":
background_process()
@samuelteixeiras
Copy link
Author

Run with: python run.py

@samuelteixeiras
Copy link
Author

samuelteixeiras commented Feb 13, 2022

create a file called twitter_auth and insert the API_KEY, API_SECRET of your application

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment