Last active
December 22, 2016 21:55
-
-
Save dyln/5e3a338cb51ac1cac166efb50e9d4991 to your computer and use it in GitHub Desktop.
final project for the course
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import tweepy | |
| import sys | |
| import jsonpickle | |
| import geojson | |
| import os | |
| # with AppAuthanticator i can gather more tweets :) | |
| auth = tweepy.AppAuthHandler("4R3sDtya1Kt9O38tcoJI1Q", "o9nvU3JoUZisDkaH6eSXARnMiyE5TXN98ib2jDb6N3A") | |
| api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True) | |
| if (not api): | |
| print ("Can't Authenticate") | |
| sys.exit(-1) | |
| #Rusya Ankara Büyükelçisi Sn. Andrey Karlov, Pazartesi 19 Aralık 2016 tarihinde saat 18.30’da | |
| # T= 19:00(gmt+3) 19/12/2016 --> 16:00 (gmt0) | |
| searchQuery = '"Andrei Karlov" -RT since:2016-12-19' # this is what we're searching for | |
| maxTweets = 1000 # Some arbitrary large number | |
| tweetsPerQry = 100 # this is the max the API permits | |
| fName = 'tweets.json' # We'll store the tweets in a json 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 | |
| # 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, lang ='en') | |
| else: | |
| new_tweets = api.search(q=searchQuery, count=tweetsPerQry, | |
| since_id=sinceId, lang='en') | |
| else: | |
| if (not sinceId): | |
| new_tweets = api.search(q=searchQuery, count=tweetsPerQry, | |
| max_id=str(max_id - 1), lang='en') | |
| else: | |
| new_tweets = api.search(q=searchQuery, count=tweetsPerQry, | |
| max_id=str(max_id - 1), | |
| since_id=sinceId, lang='en') | |
| if not new_tweets: | |
| print("No more tweets found") | |
| break | |
| for tweet in new_tweets: | |
| f.write(jsonpickle.encode(tweet._json, 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