Skip to content

Instantly share code, notes, and snippets.

@dbonadiman
Last active December 20, 2015 17:59
Show Gist options
  • Select an option

  • Save dbonadiman/6172952 to your computer and use it in GitHub Desktop.

Select an option

Save dbonadiman/6172952 to your computer and use it in GitHub Desktop.

Revisions

  1. dbonadiman revised this gist Mar 29, 2014. 1 changed file with 18 additions and 19 deletions.
    37 changes: 18 additions & 19 deletions twitter-stream.py
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,7 @@
    import os
    import sys
    import json
    import tweepy


    ########
    # Tweepy data listener
    ########
    class StdOutListener(tweepy.StreamListener):
    def do_stuff(self, pdata):
    print pdata['text']
    @@ -17,25 +12,29 @@ def on_data(self, data):
    pdata = json.loads(data)
    self.do_stuff(pdata)
    return True
    except Exception as e:
    except Exception:
    return True

    def on_error(self, status):
    print "Stream on_error: ", status


    def connect():
    # Authenticate
    auth = tweepy.OAuthHandler('Consumer key', 'Consumer secret')
    auth.set_access_token('Access token', 'Access token secret')
    return auth


    def stream():
    try:
    # Read stream
    stream = tweepy.Stream(connect(), StdOutListener())
    #stream.filter(track=['word'])
    stream.sample()
    except Exception:
    print 'could not connect to Twitter'


    #######
    # Init
    #######
    if __name__ == '__main__':
    try:
    # Authenticate
    auth = tweepy.OAuthHandler('Consumer key','Consumer secret')
    auth.set_access_token('Access token','Access token secret')
    # Read stream
    stream = tweepy.Stream(auth, StdOutListener())
    #stream.filter(track=['word'])
    stream.sample()
    except Exception as e:
    print 'could not connect to Twitter'
    stream()
  2. dbonadiman created this gist Aug 7, 2013.
    41 changes: 41 additions & 0 deletions twitter-stream.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    import os
    import sys
    import json
    import tweepy


    ########
    # Tweepy data listener
    ########
    class StdOutListener(tweepy.StreamListener):
    def do_stuff(self, pdata):
    print pdata['text']

    def on_data(self, data):
    pdata = ""
    try:
    pdata = json.loads(data)
    self.do_stuff(pdata)
    return True
    except Exception as e:
    return True

    def on_error(self, status):
    print "Stream on_error: ", status



    #######
    # Init
    #######
    if __name__ == '__main__':
    try:
    # Authenticate
    auth = tweepy.OAuthHandler('Consumer key','Consumer secret')
    auth.set_access_token('Access token','Access token secret')
    # Read stream
    stream = tweepy.Stream(auth, StdOutListener())
    #stream.filter(track=['word'])
    stream.sample()
    except Exception as e:
    print 'could not connect to Twitter'