Skip to content

Instantly share code, notes, and snippets.

@alfard
Created April 10, 2014 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfard/10392688 to your computer and use it in GitHub Desktop.
Save alfard/10392688 to your computer and use it in GitHub Desktop.
Stream_tweepy.py
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