Skip to content

Instantly share code, notes, and snippets.

@JuanjoSalvador
Forked from vickyqian/twitter crawler.txt
Last active March 29, 2020 12:37
Show Gist options
  • Save JuanjoSalvador/7efb1485b2ea6d93a70289b4fa0acbd5 to your computer and use it in GitHub Desktop.
Save JuanjoSalvador/7efb1485b2ea6d93a70289b4fa0acbd5 to your computer and use it in GitHub Desktop.
Script that downloads and counts tweets
import tweepy
import csv
from datetime import date, timedelta, datetime
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN = ''
ACCESS_TOKEN_SECRET = ''
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth, wait_on_rate_limit = True)
# Fecha hasta la que buscar
fecha_origen = datetime(2019, 12, 1)
# Fecha desde la que buscar, por defecto hoy
fecha_destino = date.today()
# Diccionario de tweets y su recuento
tweet_dict = {}
fecha_destino = fecha_destino - timedelta(days = 1)
cursor = tweepy.Cursor(api.search, q = "#coronavirus", count = 100, lang="es", since=str(fecha_origen))
print("Procesando tweets...")
for tweet in cursor.items():
date = str(tweet.created_at.date())
try:
tweet_dict[date] += 1
except KeyError:
tweet_dict[date] = 1
if tweet.created_at.date() < fecha_destino:
time_now = datetime.now().strftime("%d/%m/%y %H:%M")
current_date = str(tweet.created_at.date())
print(f'[{time_now}] Escribiendo datos para {current_date}')
try:
fecha_destino = fecha_destino - timedelta(days = 1)
with open('data.csv', 'w+') as csvfile:
for key in tweet_dict.keys():
csvfile.write("%s, %s\n" % (key, tweet_dict[key]))
except IOError:
print("Ocurrió un error...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment