binder.py 9.25 KB
Newer Older
1
# Tweepy
2
3
# Copyright 2009-2010 Joshua Roesslein
# See LICENSE for details.
4

Aaron Hill's avatar
Aaron Hill committed
5
import requests
Josh Roesslein's avatar
Josh Roesslein committed
6
import urllib
7
import time
8
import re
Mike's avatar
Mike committed
9
10
from StringIO import StringIO
import gzip
11

Josh Roesslein's avatar
Josh Roesslein committed
12
from tweepy.error import TweepError
13
from tweepy.utils import convert_to_utf8_str
14
from tweepy.models import Model
15

16
17
re_path_template = re.compile('{\w+}')

Josh Roesslein's avatar
Josh Roesslein committed
18

19
20
21
22
23
24
25
26
27
28
29
def bind_api(**config):

    class APIMethod(object):

        path = config['path']
        payload_type = config.get('payload_type', None)
        payload_list = config.get('payload_list', False)
        allowed_param = config.get('allowed_param', [])
        method = config.get('method', 'GET')
        require_auth = config.get('require_auth', False)
        search_api = config.get('search_api', False)
30
        use_cache = config.get('use_cache', True)
Aaron Hill's avatar
Aaron Hill committed
31
        session = requests.Session()
32
33
34
35
36
37
38
39
40
41
42
43

        def __init__(self, api, args, kargs):
            # If authentication is required and no credentials
            # are provided, throw an error.
            if self.require_auth and not api.auth:
                raise TweepError('Authentication required!')

            self.api = api
            self.post_data = kargs.pop('post_data', None)
            self.retry_count = kargs.pop('retry_count', api.retry_count)
            self.retry_delay = kargs.pop('retry_delay', api.retry_delay)
            self.retry_errors = kargs.pop('retry_errors', api.retry_errors)
44
            self.wait_on_rate_limit = kargs.pop('wait_on_rate_limit', api.wait_on_rate_limit)
Aaron Hill's avatar
Aaron Hill committed
45
            self.wait_on_rate_limit_notify = kargs.pop('wait_on_rate_limit_notify', api.wait_on_rate_limit_notify)
46
            self.parser = kargs.pop('parser', api.parser)
Aaron Hill's avatar
Aaron Hill committed
47
            self.session.headers = kargs.pop('headers', {})
48
49
50
51
52
53
54
55
56
57
            self.build_parameters(args, kargs)

            # Pick correct URL root to use
            if self.search_api:
                self.api_root = api.search_root
            else:
                self.api_root = api.api_root

            # Perform any path variable substitution
            self.build_path()
58

59
60
            if self.search_api:
                self.host = api.search_host
61
            else:
62
63
                self.host = api.host

64
65
66
            # Manually set Host header to fix an issue in python 2.5
            # or older where Host is set including the 443 port.
            # This causes Twitter to issue 301 redirect.
Pablo Castellano's avatar
Pablo Castellano committed
67
            # See Issue https://github.com/tweepy/tweepy/issues/12
Aaron Hill's avatar
Aaron Hill committed
68
69

            self.session.headers['Host'] = self.host
70
71
72
            # Monitoring rate limits
            self._remaining_calls = None
            self._reset_time = None
73

74
        def build_parameters(self, args, kargs):
Aaron Hill's avatar
Aaron Hill committed
75
            self.session.params = {}
76
            for idx, arg in enumerate(args):
77
78
                if arg is None:
                    continue
79
                try:
Aaron Hill's avatar
Aaron Hill committed
80
                    self.session.params[self.allowed_param[idx]] = convert_to_utf8_str(arg)
81
82
83
84
85
86
                except IndexError:
                    raise TweepError('Too many parameters supplied!')

            for k, arg in kargs.items():
                if arg is None:
                    continue
Aaron Hill's avatar
Aaron Hill committed
87
                if k in self.session.params:
88
89
                    raise TweepError('Multiple values for parameter %s supplied!' % k)

Aaron Hill's avatar
Aaron Hill committed
90
                self.session.params[k] = convert_to_utf8_str(arg)
91
92
93
94
95

        def build_path(self):
            for variable in re_path_template.findall(self.path):
                name = variable.strip('{}')

Aaron Hill's avatar
Aaron Hill committed
96
                if name == 'user' and 'user' not in self.session.params and self.api.auth:
97
98
99
100
                    # No 'user' parameter provided, fetch it from Auth instead.
                    value = self.api.auth.get_username()
                else:
                    try:
Aaron Hill's avatar
Aaron Hill committed
101
                        value = urllib.quote(self.session.params[name])
102
103
                    except KeyError:
                        raise TweepError('No parameter value found for path variable: %s' % name)
Aaron Hill's avatar
Aaron Hill committed
104
                    del self.session.params[name]
105

106
107
                self.path = self.path.replace(variable, value)

108
        def execute(self):
Joshua Roesslein's avatar
Joshua Roesslein committed
109
110
            self.api.cached_result = False

111
112
            # Build the request URL
            url = self.api_root + self.path
Joshua Roesslein's avatar
Joshua Roesslein committed
113
            full_url = 'https://' + self.host + url
114
115
116

            # Query the cache if one is available
            # and this request uses a GET method.
117
            if self.use_cache and self.api.cache and self.method == 'GET':
118
                cache_result = self.api.cache.get(url)
119
120
121
122
123
                # if cache result found and not expired, return it
                if cache_result:
                    # must restore api reference
                    if isinstance(cache_result, list):
                        for result in cache_result:
124
125
                            if isinstance(result, Model):
                                result._api = self.api
126
                    else:
127
128
                        if isinstance(cache_result, Model):
                            cache_result._api = self.api
Joshua Roesslein's avatar
Joshua Roesslein committed
129
                    self.api.cached_result = True
130
131
132
133
134
135
                    return cache_result

            # Continue attempting request until successful
            # or maximum number of retries is reached.
            retries_performed = 0
            while retries_performed < self.retry_count + 1:
136
137
138
139
140
                # handle running out of api calls
                if self.wait_on_rate_limit and self._reset_time is not None and \
                   self._remaining_calls is not None and self._remaining_calls < 1:
                    sleep_time = self._reset_time - int(time.time())
                    if sleep_time > 0:
141
142
143
144
                        if self.wait_on_rate_limit_notify:
                            print "Max retries reached. Sleeping for: " + str(sleep_time)
                        time.sleep(sleep_time + 5) # sleep for few extra sec

145
146
                # Apply authentication
                if self.api.auth:
Aaron Hill's avatar
Aaron Hill committed
147
                    auth = self.api.auth.apply_auth()
148

Mike's avatar
Mike committed
149
150
                # Request compression if configured
                if self.api.compression:
Aaron Hill's avatar
Aaron Hill committed
151
                    self.session.headers['Accept-encoding'] = 'gzip'
Mike's avatar
Mike committed
152

153
154
                # Execute request
                try:
Aaron Hill's avatar
Aaron Hill committed
155
156
                    resp = self.session.request(self.method, full_url,
                            data=self.post_data, timeout=self.api.timeout,
Aaron Hill's avatar
Aaron Hill committed
157
                            auth=auth, proxies=self.api.proxy)
Aaron Hill's avatar
Aaron Hill committed
158
                except Exception, e:
159
                    raise TweepError('Failed to send request: %s' % e)
160
                rem_calls = resp.headers.get('x-rate-limit-remaining')
161
                if rem_calls is not None:
162
                    self._remaining_calls = int(rem_calls)
163
164
                elif isinstance(self._remaining_calls, int):
                    self._remaining_calls -= 1
165
                reset_time = resp.headers.get('x-rate-limit-reset')
166
                if reset_time is not None:
167
                    self._reset_time = int(reset_time)
168
                if self.wait_on_rate_limit and self._remaining_calls == 0 and (resp.status == 429 or resp.status == 420): # if ran out of calls before waiting switching retry last call
169
                    continue
170
                retry_delay = self.retry_delay
171
                # Exit request loop if non-retry error code
Aaron Hill's avatar
Aaron Hill committed
172
                if resp.status_code == 200:
173
                    break
Aaron Hill's avatar
Aaron Hill committed
174
175
176
177
                elif (resp.status_code == 429 or resp.status_code == 420) and self.wait_on_rate_limit:
                    if 'retry-after' in resp.headers:
                        retry_delay = float(resp.headers['retry-after'])
                elif self.retry_errors and resp.status_code not in self.retry_errors:
178
                    break
179

180
                # Sleep before retrying request again
181
                time.sleep(retry_delay)
182
                retries_performed += 1
Josh Roesslein's avatar
Josh Roesslein committed
183

184
185
            # If an error was returned, throw an exception
            self.api.last_response = resp
186
            if resp.status_code and not 200 <= resp.status_code < 300:
187
                try:
Aaron Hill's avatar
Aaron Hill committed
188
                    error_msg = self.parser.parse_error(resp.text)
189
                except Exception:
Aaron Hill's avatar
Aaron Hill committed
190
                    error_msg = "Twitter error response: status code = %s" % resp.status_code
191
                raise TweepError(error_msg, resp)
192
193

            # Parse the response payload
Aaron Hill's avatar
Aaron Hill committed
194
195
            body = resp.text
            if resp.headers.get('Content-Encoding', '') == 'gzip':
Mike's avatar
Mike committed
196
197
198
                try:
                    zipper = gzip.GzipFile(fileobj=StringIO(body))
                    body = zipper.read()
199
                except Exception as e:
Mike's avatar
Mike committed
200
                    raise TweepError('Failed to decompress data: %s' % e)
201
202
            
            result = self.parser.parse(self, body)
203
204

            # Store result into cache if one is available.
205
            if self.use_cache and self.api.cache and self.method == 'GET' and result:
206
                self.api.cache.store(url, result)
207

208
            return result
Josh Roesslein's avatar
Josh Roesslein committed
209

Aaron Hill's avatar
Aaron Hill committed
210
    def _call(api, *args, **kargs):
211
212

        method = APIMethod(api, args, kargs)
Aaron Hill's avatar
Aaron Hill committed
213
        if kargs.get('create'):
214
215
216
            return method
        else:
            return method.execute()
Josh Roesslein's avatar
Josh Roesslein committed
217

218
    # Set pagination mode
219
    if 'cursor' in APIMethod.allowed_param:
220
        _call.pagination_mode = 'cursor'
221
222
223
    elif 'max_id' in APIMethod.allowed_param and \
         'since_id' in APIMethod.allowed_param:
        _call.pagination_mode = 'id'
224
    elif 'page' in APIMethod.allowed_param:
225
        _call.pagination_mode = 'page'
226

Josh Roesslein's avatar
Josh Roesslein committed
227
    return _call