Created
April 10, 2014 15:14
-
-
Save alfard/10392688 to your computer and use it in GitHub Desktop.
Stream_tweepy.py
This file contains 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 sys | |
import tweepy | |
import csv | |
#Code provenant de https://apps.twitter.com | |
consumer_key = '...........................' | |
consumer_secret = '...........................' | |
access_token = '...........................' | |
access_token_secret = '...........................' | |
#Chemin de création du fichier où les tweets seront copiés | |
fw= open('/home/alfard/Documents/Kaggle/Facebook2/TrainClean4.csv',"w") | |
filewrite = csv.writer(fw,delimiter=',', quotechar='"') | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
i=0 | |
class CustomStreamListener(tweepy.StreamListener): | |
def on_status(self, status): | |
global i | |
i=i+1 | |
print status.text | |
filewrite.writerow([status.created_at,status.text.encode("utf-8")]) | |
#Copier des tweets dans le fichier | |
print i | |
def on_error(self, status_code): | |
print >> sys.stderr, 'Encountered error with status code:', status_code | |
return True # Don't kill the stream | |
def on_timeout(self): | |
print >> sys.stderr, 'Timeout...' | |
return True # Don't kill the stream | |
sapi = tweepy.streaming.Stream(auth, CustomStreamListener()) | |
sapi.filter(track=['NKM']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment