Last active
August 29, 2015 14:08
-
-
Save anupkalburgi/227944dfc5b618e3d087 to your computer and use it in GitHub Desktop.
twitter_data_collection
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 json | |
| import csv | |
| # Authentication details. To obtain these visit dev.twitter.com | |
| consumer_key = '<Your Token>' | |
| consumer_secret = '<Your Token>' | |
| access_token = '<Your Token>' | |
| access_token_secret = '<Your Token>' | |
| # This is the listener, resposible for receiving data | |
| class StdOutListener(tweepy.StreamListener): | |
| def on_data(self, data): | |
| # Twitter returns data in JSON format - we need to decode it first | |
| decoded = json.loads(data) | |
| if decoded['geo']: | |
| print "random" | |
| row = [] | |
| row.append(decoded['id']) | |
| row.append(decoded['text'].encode('ascii', 'ignore')) | |
| row.append(decoded['geo']['coordinates'][0]) | |
| row.append(decoded['geo']['coordinates'][1]) | |
| with open("geo_twitter_ebola.csv", "a+") as fp: | |
| writer = csv.writer(fp) | |
| writer.writerow(row) | |
| print "Write Done" | |
| return True | |
| def on_error(self, status): | |
| print "Error" | |
| print status | |
| if __name__ == '__main__': | |
| l = StdOutListener() | |
| auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
| auth.set_access_token(access_token, access_token_secret) | |
| # There are different kinds of streams: public stream, user stream, multi-user streams | |
| # In this example follow #programming tag | |
| # For more details refer to https://dev.twitter.com/docs/streaming-apis | |
| stream = tweepy.Stream(auth, l) | |
| stream.filter(track=['ebola']) | |
| #locations=[-121.37,27.241,-64.07,48.30] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment