def on_connect(self): log("Twitter stream connected") if self._stop_signal: return False if self._first_response: self._first_response = False if self._first_response_callback is not None: self._first_response_callback(True) return not self._stop_signal
def on_error(self, status): log("Received Twitter API error: " + str(status)) if self._stop_signal: return False if self._first_response: self._first_response = False if self._first_response_callback is not None: self._first_response_callback(status) return not self._stop_signal
def update_keywords_tracking(self, keywords_tracking): log("Updating keywords tracking to: " + ", ".join(keywords_tracking)) self._keywords_tracking = keywords_tracking log("Update keywords tracking locked: " + str(self._update_keywords_tracking_locked)) if self._update_keywords_tracking_locked: if time.time() - self._last_update_keywords_tracking_locked > self.max_update_keywords_tracking_lock_time: log("Killing update keywords tracking lock, took too long") self._update_keywords_tracking_locked = False if self._new_streamthread is not None: self._new_streamthread.stop() self._new_streamthread = None else: return self._update_keywords_tracking_locked = True self._last_update_keywords_tracking_locked = time.time() connect_diff = time.time() - self._last_connect if self.reconnect_interval > connect_diff: sleep_time = self.reconnect_interval - connect_diff log("Sleeping for " + str(sleep_time) + " seconds before updating keywords tracking") time.sleep(sleep_time) self._new_streamthread = StreamThread(self._auth, self._keywords_tracking, self._tweet_callback, self._on_stream_first_response) self._last_connect = time.time() self._new_streamthread.start()
def run(self): log("Starting Twitter stream") while True: if self._stop_signal: log("Twitter stream stopped") break try: log("Connecting to Twitter stream") self._stream.filter(track=self._keywords_tracking) except Exception as e: log("Connection to Twitter stream lost: " + str(e)) if self._streamhandler._first_response and not self._stop_signal: self._streamhandler._first_response = False if self._first_response_callback is not None: self._first_response_callback(False)
def stop(self): log("Stopping Twitter stream") self._stop_signal = True self._streamhandler.stop()