Skip to content

Instantly share code, notes, and snippets.

@nickhargreaves
Created August 1, 2015 14:20
Show Gist options
  • Save nickhargreaves/44615385daf335166980 to your computer and use it in GitHub Desktop.
Save nickhargreaves/44615385daf335166980 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()
@sysuin
Copy link

sysuin commented Nov 26, 2020

Can you give some idea about how to set up the MySQL database for this task?

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