Skip to content

Instantly share code, notes, and snippets.

@pytholabsbot1
Last active February 21, 2019 15:03
Show Gist options
  • Save pytholabsbot1/517ecdd84421de9f973ea6470bafd2a6 to your computer and use it in GitHub Desktop.
Save pytholabsbot1/517ecdd84421de9f973ea6470bafd2a6 to your computer and use it in GitHub Desktop.
import sys
import jsonpickle
import os
searchQuery = 'brexit' # this is what we're searching for
maxTweets = 1000 # Some arbitrary large number
tweetsPerQry = 100 # this is the max the API permits
fName = 'tweets.txt' # We'll store the tweets in a text file.
# If results from a specific ID onwards are reqd, set since_id to that ID.
# else default to no lower limit, go as far back as API allows
sinceId = None
places = []
time = []
tweets = []
# If results only below a specific ID are, set max_id to that ID.
# else default to no upper limit, start from the most recent tweet matching the search query.
max_id = -1
tweetCount = 0
print("Downloading max {0} tweets".format(maxTweets))
with open(fName, 'w') as f:
while tweetCount < maxTweets:
try:
if (max_id <= 0):
if (not sinceId):
new_tweets = api.search(q=searchQuery, count=tweetsPerQry)
else:
new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
since_id=sinceId)
else:
if (not sinceId):
new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
max_id=str(max_id - 1))
else:
new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
max_id=str(max_id - 1),
since_id=sinceId)
if not new_tweets:
print("No more tweets found")
break
for tweet in new_tweets:
#add data to lists
#1. creted at
time.append((tweet.created_at.month,tweet.created_at.year))
#location of user
places.append(tweet.user.location)
#text of tweet
tweets.append(tweet.text)
f.write(jsonpickle.encode(tweet.text, unpicklable=False) +'\n')
tweetCount += len(new_tweets)
print("Downloaded {0} tweets".format(tweetCount))
max_id = new_tweets[-1].id
except tweepy.TweepError as e:
# Just exit if any error
print("some error : " + str(e))
break
print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment