# -*- coding: iso-8859-1 -*-
import pycurl
import datetime

now = datetime.datetime.now()
OUTPUT_FILE_NAME = 'Z:/WinData/Desktop/stream.txt'
# If you uncomment the next line, it will save your file with a different file name each time.
#OUTPUT_FILE_NAME = 'Z:/WinData/Desktop/stream-' + now.strftime('%Y-%m-%d_%H_%M') + '.txt'


STREAM_URL = 'https://stream.twitter.com/1/statuses/filter.json'
OPTIONS = '?track=coke,pepsi'

# change them to your own username and password!
USER = 'rpzw'
PASS = 'cs0931'

MAX = 2 # change me to some big number!!
count = 0

file = open(OUTPUT_FILE_NAME, 'w')

def on_receive(data):
    global count, file

    if count == MAX:
        return pycurl.E_ABORTED_BY_CALLBACK
    
    print(data) # print something else to indicate your progess!!
    count += 1

    file.write(data)
    
    return

conn = pycurl.Curl()
conn.setopt(pycurl.USERPWD, '%s:%s' % (USER, PASS))
conn.setopt(pycurl.URL, STREAM_URL + OPTIONS)
conn.setopt(pycurl.WRITEFUNCTION, on_receive)
conn.setopt(pycurl.VERBOSE, True)
conn.setopt(pycurl.SSL_VERIFYPEER, False)


conn.perform()
conn.close()
file.close()
