Created
May 28, 2013 19:19
-
-
Save michaelhelmick/5665349 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from requests_oauthlib import OAuth1 | |
import json | |
ITER_CHUNK_SIZE = 512 | |
def iter_lines(response, chunk_size=ITER_CHUNK_SIZE, decode_unicode=None): | |
"""Iterates over the response data, one line at a time. When | |
stream=True is set on the request, this avoids reading the | |
content at once into memory for large responses. | |
""" | |
pending = None | |
for chunk in response.iter_content(chunk_size=chunk_size, | |
decode_unicode=decode_unicode): | |
if pending is not None: | |
chunk = pending + chunk | |
#lines = chunk.splitlines() | |
print 'chunk is', chunk | |
lines = chunk.split("\r\n") | |
print 'line is', lines | |
if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]: | |
pending = lines.pop() | |
else: | |
pending = None | |
for line in lines: | |
yield line | |
if pending is not None: | |
yield pending | |
auth = OAuth1(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET) | |
client = requests.Session() | |
client.auth = auth | |
client.stream = True | |
response = client.post('https://stream.twitter.com/1.1/statuses/filter.json', data={'track': '@mikehelmick'}) | |
for line in iter_lines(response): | |
if line: | |
print json.loads(line) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment