Im currently cURLing the twitter API stream (http://stream.twitter.com/1/statuses/sample.json), so I keep getting data. I want to stop the cURLing stream as soon as I get the X number of objects from it (in the example I give 10 as an arbitrary number).
You can see how I tried to close the connection in the code below. The code below curling.perform () is never executed because it is a continuous stream of data. So I tried to close the stream in body_callback, however, since execution () is currently running, I cannot call close ().
Any help would be appreciated.
the code:
import pycurl
import base64
import json
userName = 'twitter_username'
password = 'twitter_password'
apiURL = 'http://stream.twitter.com/1/statuses/sample.json'
tweets = []
def how_many_tweets():
print 'Collected: ',len(tweets)
return len(tweets)
class Tweet:
def __init__(self):
self.raw = ''
self.id = ''
self.content = ''
def decode_json(self):
return True
def set_id(self):
return True
def set_content(self):
return True
def set_raw(self, data):
self.raw = data
class Stream:
def __init__(self):
self.tweetBeingRead =''
def body_callback(self, buf):
if(buf.startswith('{"in_reply_to_status_id_str"')):
print 'Added:'
print self.tweetBeingRead
theTweetBeingProcessed = Tweet()
theTweetBeingProcessed.set_raw(self.tweetBeingRead)
tweets.append(theTweetBeingProcessed)
self.tweet = buf
else:
self.tweetBeingRead = self.tweetBeingRead+buf
if(how_many_tweets()>10):
try:
curling.close()
except Exception as CurlError:
print ' Tried closing stream: ',CurlError
datastream = Stream()
curling = pycurl.Curl()
curling.setopt(curling.URL, apiURL)
curling.setopt(curling.HTTPHEADER, ['Authorization: '+base64.b64encode(userName+":"+password)])
curling.setopt(curling.WRITEFUNCTION, datastream.body_callback)
curling.perform()
print 'I cant reach here.'
curling.close()