<s> import asynchat <EOL> import asyncore <EOL> import socket <EOL> import base64 <EOL> import urllib <EOL> import sys <EOL> from urlparse import urlparse <EOL> from tlslite . api import * <EOL> try : <EOL> import json <EOL> except ImportError : <EOL> import simplejson as json <EOL> USERAGENT = "<STR_LIT>" <EOL> class TwitterStreamGET ( asynchat . async_chat ) : <EOL> def __init__ ( self , user , pword , url , action , debug = False , preprocessor = json . loads ) : <EOL> asynchat . async_chat . __init__ ( self ) <EOL> self . authkey = base64 . b64encode ( "<STR_LIT>" % ( user , pword ) ) <EOL> self . preprocessor = preprocessor <EOL> self . url = url <EOL> self . host = urlparse ( url ) [ <NUM_LIT:1> ] <EOL> try : <EOL> proxy = urlparse ( urllib . getproxies ( ) [ '<STR_LIT>' ] ) [ <NUM_LIT:1> ] . split ( '<STR_LIT::>' ) <EOL> proxy [ <NUM_LIT:1> ] = int ( proxy [ <NUM_LIT:1> ] ) or <NUM_LIT> <EOL> self . proxy = tuple ( proxy ) <EOL> except : <EOL> self . proxy = None <EOL> self . inbuf = "<STR_LIT>" <EOL> self . action = action <EOL> self . debug = debug <EOL> self . set_terminator ( "<STR_LIT:\r\n>" ) <EOL> self . create_socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> if self . proxy : <EOL> self . connect ( self . proxy ) <EOL> else : <EOL> self . connect ( ( self . host , <NUM_LIT> ) ) <EOL> @ property <EOL> def request ( self ) : <EOL> request = '<STR_LIT>' % self . url <EOL> request += '<STR_LIT>' % self . authkey <EOL> request += '<STR_LIT>' <EOL> request += '<STR_LIT>' % USERAGENT <EOL> request += '<STR_LIT:\r\n>' <EOL> return request <EOL> def collect_incoming_data ( self , data ) : <EOL> self . inbuf += data <EOL> def found_terminator ( self ) : <EOL> if self . inbuf . startswith ( "<STR_LIT>" ) and not self . inbuf . endswith ( "<STR_LIT>" ) : <EOL> print >> sys . stderr , self . inbuf <EOL> elif self . inbuf . startswith ( '<STR_LIT:{>' ) : <EOL> if self . preprocessor : <EOL> a = self . preprocessor ( self . inbuf ) <EOL> else : <EOL> a = self . inbuf <EOL> self . action ( a ) <EOL> if self . debug : <EOL> print >> sys . stderr , self . inbuf <EOL> self . inbuf = "<STR_LIT>" <EOL> def handle_connect ( self ) : <EOL> if self . debug : <EOL> print >> sys . stderr , self . request <EOL> self . socket = TLSConnection ( self . socket ) <EOL> self . socket . handshakeClientCert ( ) <EOL> self . push ( self . request ) <EOL> def handle_close ( self ) : <EOL> self . close ( ) <EOL> @ staticmethod <EOL> def run ( ) : <EOL> asyncore . loop ( ) <EOL> def cleanup ( self ) : <EOL> print >> sys . stderr , self . inbuf <EOL> self . close ( ) <EOL> class TwitterStreamPOST ( TwitterStreamGET ) : <EOL> def __init__ ( self , user , pword , url , action , data = tuple ( ) , debug = False , preprocessor = json . loads ) : <EOL> TwitterStreamGET . __init__ ( self , user , pword , url , action , debug , preprocessor ) <EOL> self . data = data <EOL> @ property <EOL> def request ( self ) : <EOL> data = urllib . urlencode ( self . data ) <EOL> request = '<STR_LIT>' % self . url <EOL> request += '<STR_LIT>' % self . authkey <EOL> request += '<STR_LIT>' <EOL> request += '<STR_LIT>' % USERAGENT <EOL> request += '<STR_LIT>' <EOL> request += '<STR_LIT>' % len ( data ) <EOL> request += '<STR_LIT:\r\n>' <EOL> request += '<STR_LIT:%s>' % data <EOL> return request </s>
