Search code examples
pythontwitterpycurl

Python: Twitter Streaming and pycurl problems


I am having problems with pycurl in conjunction with Twitter's Streaming API filter stream. What is happening when I run the code below it seems to barf on the perform call. I know this because I placed print statements before and after the perform call. I am using Python 2.6.1 and I am on a Mac if that matters.

#!/usr/bin/python
print "Content-type: text/html"
print
import pycurl, json, urllib

STREAM_URL = "http://stream.twitter.com/1/statuses/filter.json?follow=1&count=100"
USER = "user"
PASS = "password"
print "<html><head></head><body>"


class Client:
    def __init__(self):
        self.buffer = ""
        self.conn = pycurl.Curl()
        self.conn.setopt(pycurl.POST,1)
        self.conn.setopt(pycurl.USERPWD, "%s:%s" % (USER,PASS))
        self.conn.setopt(pycurl.URL, STREAM_URL)
        self.conn.setopt(pycurl.WRITEFUNCTION, self.on_receive)

        try:
            self.conn.perform()
            self.conn.close()
        except BaseException:
            traceback.print_exc()

    def on_receive(self,data):
        self.buffer += data
        if data.endswith("\r\n") and self.buffer.strip():
            content = json.loads(self.buffer)
            self.buffer = ""
            print content
            if "text" in content:
                print u"{0[user][name]}: {0[text]}".format(content)

client = Client()

print "</body></html>"

Solution

  • First, try turning on verbosity to help debug:

        self.conn.setopt(pycurl.VERBOSE ,1)
    

    It looks like you aren't setting the basic auth mode:

        self.conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
    

    Also according to the documentation, you need to provide a POST of the parameters to the API, not pass them in as a GET parameter:

        data = dict( track='stack overflow' )
        self.conn.setopt(pycurl.POSTFIELDS,urlencode(data))