Skip to content

Instantly share code, notes, and snippets.

@AsaoluElijah
Forked from nickhargreaves/mine_tweets_db.py
Created May 27, 2019 22:56
Show Gist options
  • Save AsaoluElijah/16c8370c0c872954c1b3224a2d5457aa to your computer and use it in GitHub Desktop.
Save AsaoluElijah/16c8370c0c872954c1b3224a2d5457aa to your computer and use it in GitHub Desktop.
Mine tweets by keyword, date and location boundaries using Python and Tweepy and load into mysql database
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
import datetime
import MySQLdb;
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
#mysql connection
conn = MySQLdb.connect(host= "localhost",
user="root",
passwd="root",
db="db_name")
x = conn.cursor()
def date_range(start,end):
current = start
while (end - current).days >= 0:
yield current
current = current + datetime.timedelta(seconds=1)
class TweetListener(StreamListener):
def on_status(self, status):
startDate = datetime.datetime(2015, 7, 21)
stopDate = datetime.datetime(2015, 7, 29)
for date in date_range(startDate,stopDate):
try:
x.execute("""INSERT INTO tweets(text, date) VALUES (%s,%s)""",(status.text,str(status.created_at)))
conn.commit()
except:
conn.rollback()
stream = Stream(auth, TweetListener(), secure=True, )
t = u"keyword" # You can use different hashtags
stream.filter(track=[t])
stream.filter(locations=[32,-5,45,-7])
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment