Skip to content

Added [follow] to [Stream] to support Site Streams. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tweepy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,33 @@ def test(self):
allowed_param = ['lat', 'long', 'name', 'contained_within']
)

""" site stream info """
stream_info = bind_api(
path = '{stream_id}/info.json',
payload_type = 'json',
require_auth = True,
allowed_param = ['stream_id']
)

""" site stream add user """
add_users = bind_api(
path = '{stream_id}/add_user.json',
payload_type = None,
require_auth = True,
method = 'POST',
allowed_param = ['stream_id','user_id']
)

""" site stream remove user """
remove_users = bind_api(
path = '{stream_id}/remove_user.json',
payload_type = None,
require_auth = True,
method = 'POST',
allowed_param = ['stream_id','user_id']
)


""" Internal use only """
@staticmethod
def _pack_image(filename, max_size):
Expand Down
24 changes: 22 additions & 2 deletions tweepy/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def __init__(self, auth, listener, **options):
self.retry_time = options.get("retry_time", 10.0)
self.snooze_time = options.get("snooze_time", 5.0)
self.buffer_size = options.get("buffer_size", 1500)
self.method = 'POST'
if options.get("secure", True):
self.scheme = "https"
else:
Expand Down Expand Up @@ -111,10 +112,10 @@ def _run(self):
conn = httplib.HTTPConnection(self.host)
else:
conn = httplib.HTTPSConnection(self.host)
self.auth.apply_auth(url, 'POST', self.headers, self.parameters)
self.auth.apply_auth(url, self.method, self.headers, self.parameters)
conn.connect()
conn.sock.settimeout(self.timeout)
conn.request('POST', self.url, self.body, headers=self.headers)
conn.request(self.method, self.url, self.body, headers=self.headers)
resp = conn.getresponse()
if resp.status != 200:
if self.listener.on_error(resp.status) is False:
Expand Down Expand Up @@ -210,6 +211,7 @@ def retweet(self, async=False):

def sample(self, count=None, async=False):
self.parameters = {'delimited': 'length'}
self.method = 'GET'
if self.running:
raise TweepError('Stream object already connected!')
self.url = '/%s/statuses/sample.json?delimited=length' % STREAM_VERSION
Expand Down Expand Up @@ -246,3 +248,21 @@ def disconnect(self):
return
self.running = False

def follow(self, follow, async=False):
if self.running:
raise TweepError('Stream object already connected!')

self.scheme = "https"
self.parameters = {}
self.headers['Content-type'] = "application/x-www-form-urlencoded"
self.host = 'sitestream.twitter.com'
self.url = '/%s/site.json?delimited=length' % STREAM_VERSION

if follow:
self.parameters['follow'] = ','.join(map(str, follow))

self.body = urlencode_noplus(self.parameters)
self.parameters['delimited'] = 'length'
self._start(async)