Skip to content

Commit

Permalink
need a while loop so that client reconnects after disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
edsu committed Jul 2, 2021
1 parent e9fa3ca commit 9928dd3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
51 changes: 29 additions & 22 deletions twarc/client2.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ def sample(self, event=None, record_keepalive=False):
if self.metadata:
data = _append_metadata(data, resp.url)
yield data
self._check_for_disconnect(data)
if self._check_for_disconnect(data):
break

@requires_app_auth
def add_stream_rules(self, rules):
Expand Down Expand Up @@ -546,26 +547,30 @@ def stream(self, event=None, record_keep_alives=False):
"""
url = "https://api.twitter.com/2/tweets/search/stream"
params = expansions.EVERYTHING.copy()
resp = self.get(url, params=params, stream=True)
for line in resp.iter_lines():

# quit & close the stream if the event is set
if event and event.is_set():
log.info("stopping filter")
resp.close()
return

if line == b"":
log.info("keep-alive")
if record_keep_alives:
yield "keep-alive"
else:
data = json.loads(line.decode())
if self.metadata:
data = _append_metadata(data, resp.url)

yield data
self._check_for_disconnect(data)
while True:
log.info("Connecting to V2 stream")
resp = self.get(url, params=params, stream=True)
for line in resp.iter_lines():

# quit & close the stream if the event is set
if event and event.is_set():
log.info("stopping filter")
resp.close()
return

if line == b"":
log.info("keep-alive")
if record_keep_alives:
yield "keep-alive"
else:
data = json.loads(line.decode())
if self.metadata:
data = _append_metadata(data, resp.url)

yield data
if self._check_for_disconnect(data):
break

def _timeline(
self,
Expand Down Expand Up @@ -894,13 +899,15 @@ def id_exists(user):

def _check_for_disconnect(self, data):
"""
Look for disconnect errors in a response, and reconnect if found.
Look for disconnect errors in a response, and reconnect if found. The
function returns True if a disconnect was found and False otherwise.
"""
for error in data.get("errors", []):
if error.get("disconnect_type") == "OperationalDisconnect":
log.info("Received operational disconnect message, reconnecting")
self.connect()
break
return True
return False


def _ts(dt):
Expand Down
2 changes: 1 addition & 1 deletion twarc/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = "2.3.5"
version = "2.3.6"

0 comments on commit 9928dd3

Please sign in to comment.