script/lib/iri_tweet/iri_tweet/stream.py
author ymh <ymh.work@gmail.com>
Thu, 10 Jan 2019 18:36:36 +0100
changeset 1497 14a9bed2e3cd
parent 957 e4d0094f097b
child 1498 e0b3ef3c07d0
permissions -rw-r--r--
Adapt recorder_stream to python 3 Improve twitter authentication management Use Oauth2 where possible Delete old script
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     2
'''
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     3
Created on Mar 22, 2012
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     4
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     5
@author: ymh
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     6
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     7
Module directly inspired by tweetstream
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     8
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     9
'''
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
    10
import json
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
    11
import select
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    12
import time
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
    13
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    14
import requests
884
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
    15
from requests.utils import stream_decode_response_unicode
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    16
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
    17
from . import USER_AGENT, AuthenticationError, ConnectionError
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    18
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    19
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    20
class BaseStream(object):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    21
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    22
    """A network connection to Twitters streaming API
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    23
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    24
    :param auth: requests auth object.
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    25
    :keyword catchup: Number of tweets from the past to get before switching to
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    26
      live stream.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    27
    :keyword raw: If True, return each tweet's raw data direct from the socket,
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    28
      without UTF8 decoding or parsing, rather than a parsed object. The
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    29
      default is False.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    30
    :keyword timeout: If non-None, set a timeout in seconds on the receiving
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    31
      socket. Certain types of network problems (e.g., disconnecting a VPN)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    32
      can cause the connection to hang, leading to indefinite blocking that
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    33
      requires kill -9 to resolve. Setting a timeout leads to an orderly
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    34
      shutdown in these cases. The default is None (i.e., no timeout).
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    35
    :keyword url: Endpoint URL for the object. Note: you should not
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    36
      need to edit this. It's present to make testing easier.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    37
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    38
    .. attribute:: connected
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    39
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    40
        True if the object is currently connected to the stream.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    41
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    42
    .. attribute:: url
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    43
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    44
        The URL to which the object is connected
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    45
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    46
    .. attribute:: starttime
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    47
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    48
        The timestamp, in seconds since the epoch, the object connected to the
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    49
        streaming api.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    50
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    51
    .. attribute:: count
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    52
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    53
        The number of tweets that have been returned by the object.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    54
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    55
    .. attribute:: rate
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    56
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    57
        The rate at which tweets have been returned from the object as a
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    58
        float. see also :attr: `rate_period`.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    59
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    60
    .. attribute:: rate_period
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    61
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    62
        The ammount of time to sample tweets to calculate tweet rate. By
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    63
        default 10 seconds. Changes to this attribute will not be reflected
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    64
        until the next time the rate is calculated. The rate of tweets vary
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    65
        with time of day etc. so it's usefull to set this to something
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    66
        sensible.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    67
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    68
    .. attribute:: user_agent
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    69
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    70
        User agent string that will be included in the request. NOTE: This can
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    71
        not be changed after the connection has been made. This property must
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    72
        thus be set before accessing the iterator. The default is set in
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    73
        :attr: `USER_AGENT`.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    74
    """
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    75
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    76
    def __init__(self, auth,
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
    77
                  raw=False, timeout=-1, url=None, compressed=False, logger=None):
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    78
        self._conn = None
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    79
        self._rate_ts = None
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    80
        self._rate_cnt = 0
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    81
        self._auth = auth
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    82
        self.raw_mode = raw
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    83
        self.timeout = timeout
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    84
        self._compressed = compressed
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    85
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    86
        self.rate_period = 10  # in seconds
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    87
        self.connected = False
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    88
        self.starttime = None
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    89
        self.count = 0
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    90
        self.rate = 0
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    91
        self.user_agent = USER_AGENT
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    92
        if url: self.url = url
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
    93
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    94
        self.muststop = False
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
    95
        self._logger = logger
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
    96
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    97
        self._iter = self.__iter__()
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
    98
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    99
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   100
    def __enter__(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   101
        return self
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   102
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   103
    def __exit__(self, *params):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   104
        self.close()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   105
        return False
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   106
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   107
    def _init_conn(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   108
        """Open the connection to the twitter server"""
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   109
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   110
        if self._logger : self._logger.debug("BaseStream Open the connection to the twitter server")
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   111
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   112
        headers = {'User-Agent': self.user_agent}
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   113
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   114
        if self._compressed:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   115
            headers['Accept-Encoding'] = "deflate, gzip"
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   116
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   117
        postdata = self._get_post_data() or {}
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   118
        postdata['stall_warnings'] = 'true'
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   119
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   120
        if self._logger : self._logger.debug("BaseStream init connection url " + repr(self.url))
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   121
        if self._logger : self._logger.debug("BaseStream init connection headers " + repr(headers))
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   122
        if self._logger : self._logger.debug("BaseStream init connection data " + repr(postdata))
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   123
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   124
        self._resp = requests.post(self.url, auth=self._auth, headers=headers, data=postdata, stream=True)
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   125
        if self._logger : self._logger.debug("BaseStream init connection " + repr(self._resp))
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   126
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   127
        self._resp.raise_for_status()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   128
        self.connected = True
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   129
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   130
        if not self._rate_ts:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   131
            self._rate_ts = time.time()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   132
        if not self.starttime:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   133
            self.starttime = time.time()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   134
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   135
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   136
    def _get_post_data(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   137
        """Subclasses that need to add post data to the request can override
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   138
        this method and return post data. The data should be in the format
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   139
        returned by urllib.urlencode."""
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   140
        return None
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   141
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   142
    def testmuststop(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   143
        if callable(self.muststop):
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   144
            return self.muststop() # pylint: disable=not-callable
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   145
        else:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   146
            return self.muststop
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   147
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   148
    def _update_rate(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   149
        rate_time = time.time() - self._rate_ts
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   150
        if not self._rate_ts or rate_time > self.rate_period:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   151
            self.rate = self._rate_cnt / rate_time
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   152
            self._rate_cnt = 0
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   153
            self._rate_ts = time.time()
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   154
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   155
    def __iter__(self):
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   156
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   157
        if self._logger : self._logger.debug("BaseStream __iter__")
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   158
        if not self.connected:
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   159
            if self._logger : self._logger.debug("BaseStream __iter__ not connected, connecting")
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   160
            self._init_conn()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   161
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   162
        if self._logger : self._logger.debug("BaseStream __iter__ connected")
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   163
        has_stopped = False
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   164
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   165
        # for line in self._iter_object():
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   166
        for line in self._resp.iter_lines():
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   167
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   168
            if not line:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   169
                continue
888
6fc6637d8403 update listener. add support for twitter regulation messages. update virtualenv
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 884
diff changeset
   170
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   171
            if self.testmuststop():
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   172
                has_stopped = True
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   173
                break
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   174
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   175
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   176
            if self._logger : self._logger.debug("BaseStream __iter__ line %s " % repr(line))
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   177
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   178
            text_in_tweet = False
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   179
            if (self.raw_mode):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   180
                tweet = line
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   181
                text_in_tweet = b'text' in tweet
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   182
            else:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   183
                try:
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   184
                    tweet = json.loads(line)
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   185
                except ValueError:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   186
                    self.close()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   187
                    raise ConnectionError("Got invalid data from twitter", details=line)
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   188
                text_in_tweet = 'text' in tweet
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   189
            if text_in_tweet:
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   190
                self.count += 1
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   191
                self._rate_cnt += 1
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   192
            self._update_rate()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   193
            yield tweet
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   194
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   195
        if has_stopped:
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   196
            raise StopIteration()
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   197
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   198
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   199
    def close(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   200
        """
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   201
        Close the connection to the streaming server.
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   202
        """
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   203
        self.connected = False
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   204
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   205
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   206
class FilterStream(BaseStream):
738
2497c7f38e0a correct. remove mutex and clear
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 693
diff changeset
   207
    url = "https://stream.twitter.com/1.1/statuses/filter.json"
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   208
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   209
    def __init__(self, auth, follow=None, locations=None,
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   210
                 track=None, url=None, raw=False, timeout=None, compressed=False, logger=None):
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   211
        self._follow = follow
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   212
        self._locations = locations
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   213
        self._track = track
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   214
        # remove follow, locations, track
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   215
        BaseStream.__init__(self, auth, url=url, raw=raw, timeout=timeout, compressed=compressed, logger=logger)
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   216
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   217
    def _get_post_data(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   218
        postdata = {}
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   219
        if self._follow: postdata["follow"] = ",".join([str(e) for e in self._follow])
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   220
        if self._locations: postdata["locations"] = ",".join(self._locations)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   221
        if self._track: postdata["track"] = ",".join(self._track)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   222
        return postdata
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   223
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   224
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   225
class SafeStreamWrapper(object):
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   226
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   227
    def __init__(self, base_stream, logger=None, error_cb=None, max_reconnects=-1, initial_tcp_wait=250, initial_http_wait=5000, max_wait=240000):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   228
        self._stream = base_stream
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   229
        self._logger = logger
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   230
        self._error_cb = error_cb
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   231
        self._max_reconnects = max_reconnects
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   232
        self._initial_tcp_wait = initial_tcp_wait
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   233
        self._initial_http_wait = initial_http_wait
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   234
        self._max_wait = max_wait
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   235
        self._retry_wait = 0
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   236
        self._retry_nb = 0
884
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
   237
        self._reconnects = 0
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   238
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   239
    def __post_process_error(self,e):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   240
        # Note: error_cb is not called on the last error since we
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   241
        # raise a ConnectionError instead
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   242
        if  callable(self._error_cb):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   243
            self._error_cb(e)
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   244
        if self._logger: self._logger.info("stream sleeping for %d ms " % self._retry_wait)
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   245
        time.sleep(float(self._retry_wait)/1000.0)
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   246
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   247
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   248
    def __process_tcp_error(self,e):
884
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
   249
        if self._logger: self._logger.debug("connection error type :" + repr(type(e)))
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
   250
        if self._logger: self._logger.debug("connection error :" + repr(e))
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   251
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   252
        self._reconnects += 1
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   253
        if self._max_reconnects >= 0 and self._reconnects > self._max_reconnects:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   254
            raise ConnectionError("Too many retries")
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   255
        if self._retry_wait < self._max_wait:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   256
            self._retry_wait += self._initial_tcp_wait
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   257
            if self._retry_wait > self._max_wait:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   258
                self._retry_wait = self._max_wait
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   259
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   260
        self.__post_process_error(e)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   261
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   262
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   263
    def __process_http_error(self,e):
884
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
   264
        if self._logger: self._logger.debug("http error type %s" % (repr(type(e))))
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   265
        if self._logger: self._logger.debug("http error on %s : %s" % (e.response.url,e.message))
884
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
   266
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   267
        if self._retry_wait < self._max_wait:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   268
            self._retry_wait = 2*self._retry_wait if self._retry_wait > 0 else self._initial_http_wait
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   269
            if self._retry_wait > self._max_wait:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   270
                self._retry_wait = self._max_wait
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   271
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   272
        self.__post_process_error(e)
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   273
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   274
    def __iter__(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   275
        while not self._stream.testmuststop():
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   276
            self._retry_nb += 1
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   277
            try:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   278
                if self._logger: self._logger.debug("inner loop")
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   279
                for tweet in self._stream:
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   280
                    if self._logger: self._logger.debug("tweet : " + repr(tweet))
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   281
                    self._reconnects = 0
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   282
                    self._retry_wait = 0
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   283
                    if not tweet.strip():
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   284
                        if self._logger: self._logger.debug("Empty Tweet received : PING")
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   285
                        continue
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   286
                    yield tweet
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   287
            except requests.exceptions.HTTPError as e:
884
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
   288
                if e.response.status_code == 401:
1497
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   289
                    if self._logger: self._logger.debug("SafeStreamWrapper Connection Error http error on %s : %s" % (e.response.url,e.strerror))
14a9bed2e3cd Adapt recorder_stream to python 3
ymh <ymh.work@gmail.com>
parents: 957
diff changeset
   290
                    raise AuthenticationError("Error connecting to %s : %s : %s - %s" % (e.response.url,e.strerror, repr(e.response.headers),repr(e.response.text)))
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   291
                if e.response.status_code > 200:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   292
                    self.__process_http_error(e)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   293
                else:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   294
                    self.__process_tcp_error(e)
884
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
   295
            except (ConnectionError, requests.exceptions.ConnectionError, requests.exceptions.Timeout, requests.exceptions.RequestException) as e:
07f1c6854df9 update virtualenv creation
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 883
diff changeset
   296
                self.__process_tcp_error(e)