###Make sure to change the directory to where the Tweepy folder can be found### import os print 'okay' class cd: """Context manager for changing the current working directory""" def __init__(self, newPath): self.newPath = os.path.expanduser(newPath) def __enter__(self): self.savedPath = os.getcwd() os.chdir(self.newPath) def __exit__(self, etype, value, traceback): os.chdir(self.savedPath) print 'oh yeah' import subprocess # just to call an arbitrary command e.g. 'ls' print 'superb' # enter the directory like this: with cd("~H:\Mijn Documenten\GIMA_Scriptie\tweepy-3.4.0"): # we are in ~/tweepy-3.4.0 subprocess.call("ls") print 'yes' # outside the context manager we are back wherever we started. ###Since the directory is set on the right folder, we can import Tweepy now### import tweepy auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) public_tweets = api.home_timeline() for tweet in public_tweets: print tweet.text # Get the User object for twitter... ###Returns a User model (Tweepy model class instance) from the API method### user = api.get_user('twitter') ###Model contains data and some helper methods### print user.screen_name print user.followers_count for friend in user.friends(): print friend.screen_name auth = tweepy.OAuthHandler("consumer_key", "consumer_secret") ###OR: if web application is using a callback URL supplied dynamically### #auth = tweepy.OAuthHandler(consumer_token, consumer_secret, #callback_url) try: redirect_url = auth.get_authorization_url() except tweepy.TweepError: print 'Error! Failed to get request token.' session.set('request_token', auth.request_token) # Example using callback (web app) ###verifier = request.GET.get('oauth_verifier')### # Example w/o callback (desktop) verifier = raw_input('Verifier:') # Let's say this is a web app, so we need to re-build the auth handler # first... auth = tweepy.OAuthHandler(consumer_key, consumer_secret) token = session.get('request_token') session.delete('request_token') auth.request_token = token try: auth.get_access_token(verifier) except tweepy.TweepError: print 'Error! Failed to get access token.' ###Store 2 string values: key and secret token### auth.access_token auth.access_token_secret ###Throw them into database, rebuild OAuthHandler from stored access token### auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(key, secret) ###OAuthHandler is now equipped with access topken, ready for business!### api = tweepy.API(auth) api.update_status('tweepy + oauth!') ###Get coordinates from Twitter### import json from tweepy import Stream from tweepy import OAuthHandler from tweepy.streaming import StreamListener #Enter Twitter API Key information consumer_key = '9tEay70Yd87x8wMy0FfBHejjm' consumer_secret = 'k0krHr7bQyc4M3NQxbE9m58QteQNHg7yZdURq1jWZ6Ob73h0Pl' access_token = ' 3646668313-uG9GZ0LDJUCVqsaxMp6pZF0zNtwwWbfxmvVaeai' access_secret = 'snp1DhXZNrrl4zwSLtIGGfgWq7LehoFwiaTjlHDDMETa0' file = open("H:\\Output_test.csv", "w") file.write("X,Y\n") data_list = [] count = 0 class listener(StreamListener): def on_data(self, data): global count #How many tweets you want to find, could change to time based if count <= 2000: json_data = json.loads(data) coords = json_data["coordinates"] if coords is not None: print coords["coordinates"] lon = coords["coordinates"][0] lat = coords["coordinates"][1] data_list.append(json_data) file.write(str(lon) + ",") file.write(str(lat) + "\n") count += 1 return True else: file.close() return False def on_error(self, status): print status auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_secret) twitterStream = Stream(auth, listener()) #What you want to search for here twitterStream.filter(track=["fietsfan010"]) print'success' print(done)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question