Skip to content
This repository has been archived by the owner on Jul 8, 2024. It is now read-only.

Commit

Permalink
0.0.7: Fixed issue with --near and --within parameters, add the bette…
Browse files Browse the repository at this point in the history
…r exception handling
  • Loading branch information
Mottl committed Nov 9, 2018
1 parent dd0924b commit 4f50867
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
40 changes: 27 additions & 13 deletions GetOldTweets3/manager/TweetCriteria.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class TweetCriteria:

def __init__(self):
self.maxTweets = 0
self.topTweets = False
self.within = "15mi"

def setUsername(self, username):
"""Set username(s) of tweets author(s)
Expand All @@ -27,30 +29,42 @@ def setSince(self, since):
"""Set a lower bound date in UTC
Parameters
----------
since : str
`since' parameter should be in one the following formats:
yyyy-mm-dd
yyyy-mm-dd HH:MM
yyyy-mm-dd HH:MM:SS
since : str,
format: "yyyy-mm-dd"
"""
self.since = since
return self

def setUntil(self, until):
"""Set an upper bound date in UTC
"""Set an upper bound date in UTC (not included in results)
Parameters
----------
until : str
`until' parameter should be in one the following formats:
yyyy-mm-dd
yyyy-mm-dd HH:MM
yyyy-mm-dd HH:MM:SS
until : str,
format: "yyyy-mm-dd"
"""
self.until = until
return self

def setNear(self, near):
"""Set location to search nearby
Parameters
----------
near : str,
for example "Berlin, Germany"
"""
self.near = near
return self

def setWithin(self, within):
"""Set the radius for search by location
Parameters
----------
within : str,
for example "15mi"
"""
self.within = within
return self

def setQuerySearch(self, querySearch):
"""Set a text to be searched for
Parameters
Expand Down
35 changes: 24 additions & 11 deletions GetOldTweets3/manager/TweetManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,20 @@ def getJsonReponse(tweetCriteria, refreshCursor, cookieJar, proxy, useragent=Non
"""Invoke an HTTP query to Twitter.
Should not be used as an API function. A static method.
"""
url = ("https://twitter.com/i/search/timeline?f=tweets&vertical=news&q=%s&src=typd&%s"
"&include_available_features=1&include_entities=1&max_position=%s"
"&reset_error_state=false")
url = "https://twitter.com/i/search/timeline?"

if not tweetCriteria.topTweets:
url += "f=tweets&"

url += ("vertical=news&q=%s&src=typd&%s"
"&include_available_features=1&include_entities=1&max_position=%s"
"&reset_error_state=false")

urlGetData = ''

if hasattr(tweetCriteria, 'querySearch'):
urlGetData += tweetCriteria.querySearch

if hasattr(tweetCriteria, 'username'):
if not hasattr(tweetCriteria.username, '__iter__'):
tweetCriteria.username = [tweetCriteria.username]
Expand All @@ -151,15 +160,15 @@ def getJsonReponse(tweetCriteria, refreshCursor, cookieJar, proxy, useragent=Non
if usernames:
urlGetData += ' OR'.join(usernames)

if hasattr(tweetCriteria, 'near') and hasattr(tweetCriteria, 'within'):
urlGetData += ' near:%s within:%s' % (tweetCriteria.near, tweetCriteria.within)

if hasattr(tweetCriteria, 'since'):
urlGetData += ' since:' + tweetCriteria.since

if hasattr(tweetCriteria, 'until'):
urlGetData += ' until:' + tweetCriteria.until

if hasattr(tweetCriteria, 'querySearch'):
urlGetData += ' ' + tweetCriteria.querySearch

if hasattr(tweetCriteria, 'lang'):
urlLang = 'l=' + tweetCriteria.lang + '&'
else:
Expand All @@ -186,16 +195,20 @@ def getJsonReponse(tweetCriteria, refreshCursor, cookieJar, proxy, useragent=Non
if debug:
print(url)
print('\n'.join(h[0]+': '+h[1] for h in headers))

try:
response = opener.open(url)
jsonResponse = response.read()
except:
print("Twitter weird response. Try to open in browser: https://twitter.com/search?q=%s&src=typd" % urllib.parse.quote(urlGetData))
print("Unexpected error:", sys.exc_info()[0])
except Exception as e:
print("An error occured during an HTTP request:", str(e))
print("Try to open in browser: https://twitter.com/search?q=%s&src=typd" % urllib.parse.quote(urlGetData))
sys.exit()
return

s_json = jsonResponse.decode()
try:
s_json = jsonResponse.decode()
except:
print("Invalid response from Twitter")
sys.exit()

try:
dataJson = json.loads(s_json)
Expand Down

0 comments on commit 4f50867

Please sign in to comment.