Skip to content

Commit

Permalink
Merge pull request #558 from tweepy/fix-556
Browse files Browse the repository at this point in the history
Fix bug where streams freeze up on Python 3 due to string formatting bug...
  • Loading branch information
joshthecoder committed Feb 16, 2015
2 parents fdf1331 + 88172bf commit 9916a54
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
30 changes: 14 additions & 16 deletions tweepy/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from tweepy.api import API
from tweepy.error import TweepError

from tweepy.utils import import_simplejson, urlencode_noplus
from tweepy.utils import import_simplejson
json = import_simplejson()

STREAM_VERSION = '1.1'
Expand Down Expand Up @@ -125,7 +125,7 @@ def on_disconnect(self, notice):
https://dev.twitter.com/docs/streaming-apis/messages#Disconnect_messages_disconnect
"""
return

def on_warning(self, notice):
"""Called when a disconnection warning message arrives"""
return
Expand Down Expand Up @@ -397,44 +397,42 @@ def sample(self, async=False, languages=None):

def filter(self, follow=None, track=None, async=False, locations=None,
stall_warnings=False, languages=None, encoding='utf8'):
self.session.params = {}
self.body = {}
self.session.headers['Content-type'] = "application/x-www-form-urlencoded"
if self.running:
raise TweepError('Stream object already connected!')
self.url = '/%s/statuses/filter.json' % STREAM_VERSION
if follow:
self.session.params['follow'] = u','.join(follow).encode(encoding)
self.body['follow'] = u','.join(follow).encode(encoding)
if track:
self.session.params['track'] = u','.join(track).encode(encoding)
self.body['track'] = u','.join(track).encode(encoding)
if locations and len(locations) > 0:
if len(locations) % 4 != 0:
raise TweepError("Wrong number of locations points, "
"it has to be a multiple of 4")
self.session.params['locations'] = u','.join(['%.4f' % l for l in locations])
self.body['locations'] = u','.join(['%.4f' % l for l in locations])
if stall_warnings:
self.session.params['stall_warnings'] = stall_warnings
self.body['stall_warnings'] = stall_warnings
if languages:
self.session.params['language'] = u','.join(map(str, languages))
self.body = urlencode_noplus(self.session.params)
self.body['language'] = u','.join(map(str, languages))
self.session.params = {'delimited': 'length'}
self.host = 'stream.twitter.com'
self._start(async)

def sitestream(self, follow, stall_warnings=False,
with_='user', replies=False, async=False):
self.parameters = {}
self.body = {}
if self.running:
raise TweepError('Stream object already connected!')
self.url = '/%s/site.json' % STREAM_VERSION
self.parameters['follow'] = u','.join(map(six.text_type, follow))
self.parameters['delimited'] = 'length'
self.body['follow'] = u','.join(map(six.text_type, follow))
self.body['delimited'] = 'length'
if stall_warnings:
self.parameters['stall_warnings'] = stall_warnings
self.body['stall_warnings'] = stall_warnings
if with_:
self.parameters['with'] = with_
self.body['with'] = with_
if replies:
self.parameters['replies'] = replies
self.body = urlencode_noplus(self.parameters)
self.body['replies'] = replies
self._start(async)

def disconnect(self):
Expand Down
6 changes: 0 additions & 6 deletions tweepy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,3 @@ def import_simplejson():
def list_to_csv(item_list):
if item_list:
return ','.join([str(i) for i in item_list])


def urlencode_noplus(query):
return '&'.join(['%s=%s' % (quote(str(k), ''), quote(str(v), ''))
for k, v in query.items()])

0 comments on commit 9916a54

Please sign in to comment.