# Pipe the output of this to file, e.g.:
#
#     `python twitter_streaming.py > twitter_data.txt`
#
# The output is in JSON format.

# This uses Tweepy, a Python library for accessing the Twitter API:
# http://www.tweepy.org. Install with `pip install tweepy`.

# The details of using Tweepy with the Twitter streaming API is in:
# http://docs.tweepy.org/en/v3.4.0/streaming_how_to.html

from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

# Read the Twitter API key data from a file (not in the repository)
with open('twitter_api_key.txt') as fileHandle:
    (access_token, access_token_secret, consumer_key, consumer_secret) = \
        [item.strip('\n') for item in fileHandle.readlines()]

# Set the keywords to filter the Twitter stream for
keywords = ['python', 'javascript', 'ruby']

# This is a basic listener that prints received tweets to stdout
# Over-ride the tweepy.Stream listener to provide methods
class StdOutListener(StreamListener):

    def on_data(self, data):
        print data
        return True

    def on_error(self, status):
        print status
        if status == 420:
            return False
        return False
        sys.exit(1)

if __name__ == "__main__":

    # Handle Twitter authentication and connection to Twitter Streaming API
    listener = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    stream = Stream(auth, listener)

    # Filter Twitter stream according to keywords
    stream.filter(track = keywords)

@@ -1,37 +1,31 @@
 # 
-Pipe the output of this to file
+Call without any argument
 , e.
@@ -68,46 +68,23 @@
 g.py
- %3E twitter_data.txt
 %60%0A#%0A# 
-The o
+O
 utput is
  in 
@@ -79,16 +79,47 @@
 utput is
+ automatically written to file,
  in JSON
@@ -484,16 +484,52 @@
 t Stream
+%0Aimport json%0Aimport re%0Aimport pocket
 %0A%0A# Read
@@ -780,103 +780,1066 @@
 )%5D%0A%0A
-# Set the keywords to filter the Twitter stream for%0Akeywords = %5B'python', 'javascript', 'ruby'%5D
+with open('pocket_api_key.txt') as fileHandle:%0A    (pckt_consumer_key, pckt_access_token) = %5C%0A        %5Bitem.strip('%5Cn') for item in fileHandle.readlines()%5D%0A%0A# Set the keywords to filter the Twitter stream for%0Akeywords = %5B'python', 'javascript', 'fortran', 'bash', 'linux', 'css', 'jquery',%0A            'api', 'git'%5D%0A%0Adef getData(tweet, key):%0A    try:%0A        val = tweet%5Bkey%5D%0A    except:%0A        return ''%0A    return val%0A%0Adef word_in_text(word, text):%0A    if re.search(word.lower(), text.lower()):%0A        return True%0A    return False%0A%0Adef extract_link(text):%0A    regex = r'https?:..%5B%5E%5Cs%3C%3E%22%5D+%7Cwww%5C.%5B%5E%5Cs%3C%3E%22%5D+'%0A    match = re.search(regex, text)%0A    if match:%0A        return match.group()%0A    return ''%0A%0Adef myrespond(data):%0A    tweet = json.loads(data)%0A    #fileUnit.write(data)%0A    #testUnit.write(json.dumps(tweet)+%22%5Cn%22) %0A    text = getData(tweet, 'text')%0A    link = extract_link(text)%0A    if (word_in_text('tutorial', text) and link != ''):%0A        print text%0A        print link%0A        print %22-----------------------------------------------------------%22%0A%0A
 %0A%0A# 
@@ -2039,18 +2039,23 @@
     
-print 
+myrespond(
 data
+)
 %0A   
@@ -2246,16 +2246,113 @@
 in__%22:%0A%0A
+    #testUnit = open('twitter_test.txt', 'w')%0A    #fileUnit = open('twitter_data.txt', 'w')%0A    %0A
     # Ha
@@ -2603,16 +2603,164 @@
 stener)%0A
+   %0A    # Pocket authentication%0A    request_token = pocket.Pocket.get_request_token(consumer_key=consumer_key,%0A    redirect_uri=redirect_uri) %0A     
 %0A    # F
@@ -2802,24 +2802,25 @@
 eywords%0A    
+#
 stream.filte

Update twitter_streaming.py to process stream directly instead of writing to file; make parse_tweets.py obsolete.