Skip to content

Commit

Permalink
update query.py: refactor the querying
Browse files Browse the repository at this point in the history
This change ensures that nothing is done if the response does not have status_code 200.
Previously .json() method was called upon an empty response.

Fixes #93
  • Loading branch information
taspinar committed Feb 25, 2018
1 parent 674cc7e commit 5977423
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions twitterscraper/query.py
Expand Up @@ -32,21 +32,20 @@ def query_single_page(url, html_response=True, retry=10):

try:
response = requests.get(url, headers=headers)
if html_response:
html = response.text
if resonse.status_code == 200:
if html_response:
html = response.text
tweets = list(Tweet.from_html(html))
return tweets, "TWEET-{}-{}".format(tweets[-1].id, tweets[0].id)
else:
json_resp = response.json()
html = json_resp['items_html']
tweets = list(Tweet.from_html(html))
return tweets, json_resp['min_position']
else:
json_resp = response.json()
html = json_resp['items_html']

tweets = list(Tweet.from_html(html))

if not tweets:
return [], None

if not html_response:
return tweets, json_resp['min_position']

return tweets, "TWEET-{}-{}".format(tweets[-1].id, tweets[0].id)
except requests.exceptions.HTTPError as e:
logging.exception('HTTPError {} while requesting "{}"'.format(
e, url))
Expand Down

0 comments on commit 5977423

Please sign in to comment.