forked from tomslee/airbnb-data-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airbnb_ws.py
133 lines (124 loc) · 5.71 KB
/
airbnb_ws.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/python3
# ============================================================================
# Tom Slee, 2013--2017.
#
# Functions for making and handling requests from the Airbnb web site
# TODO: Make this into a class
# ============================================================================
import logging
import sys
import random
import time
import requests
from airbnb_config import ABConfig
# Set up logging
logger = logging.getLogger()
def ws_request_with_repeats(config, url, params=None):
# Return None on failure
logger.debug(url)
for attempt_id in range(config.MAX_CONNECTION_ATTEMPTS):
try:
response = ws_request(config, url, attempt_id, params)
if response is None:
continue
elif response.status_code == requests.codes.ok:
return response
except (SystemExit, KeyboardInterrupt):
raise
except AttributeError:
logger.exception("AttributeError retrieving page")
except Exception as ex:
logger.error("Failed to retrieve web page " + url)
logger.exception("Exception retrieving page: " + str(type(ex)))
# Failed
return None
def ws_request(config, url, attempt_id, params=None):
"""
Individual web request: returns a response object or None on failure
"""
try:
# wait
sleep_time = config.REQUEST_SLEEP * random.random()
logger.debug("sleeping " + str(sleep_time)[:7] + " seconds...")
time.sleep(sleep_time) # be nice
timeout = config.HTTP_TIMEOUT
# If a list of user agent strings is supplied, use it
if len(config.USER_AGENT_LIST) > 0:
user_agent = random.choice(config.USER_AGENT_LIST)
headers = {"User-Agent": user_agent}
else:
headers = {'User-Agent': 'Mozilla/5.0'}
# If there is a list of proxies supplied, use it
http_proxy = None
logger.debug("Using " + str(len(config.HTTP_PROXY_LIST)) + " proxies.")
if len(config.HTTP_PROXY_LIST) > 0:
http_proxy = random.choice(config.HTTP_PROXY_LIST)
proxies = {
'http': http_proxy,
'https': http_proxy,
}
logger.debug("Requesting page through proxy " + http_proxy)
else:
proxies = None
# Now make the request
# cookie to avoid auto-redirect
cookies = dict(sticky_locale='en')
response = requests.get(url, params, timeout=timeout,
headers=headers, cookies=cookies, proxies=proxies)
if response.status_code == 503:
if http_proxy:
logger.warning("HTTP 503 error from web site: IP address {a} blocked"
.format(a=http_proxy))
if len(config.HTTP_PROXY_LIST) > 0:
# randomly remove the proxy from the list, with probability 50%
if random.choice([True, False]):
config.HTTP_PROXY_LIST.remove(http_proxy)
logger.warning(
"Removing {http_proxy} from proxy list; {n} of {p} remain."
.format( http_proxy=http_proxy,
n=len(config.HTTP_PROXY_LIST),
p=len(config.HTTP_PROXY_LIST_COMPLETE)))
else:
logger.warning(
"Not removing {http_proxy} from proxy list this time; still {n} of {p}."
.format( http_proxy=http_proxy,
n=len(config.HTTP_PROXY_LIST),
p=len(config.HTTP_PROXY_LIST_COMPLETE)))
if len(config.HTTP_PROXY_LIST) == 0:
# fill proxy list again, wait a long time, then restart
logger.warning("No proxies remain. Resetting proxy list and waiting {m} minutes."
.format(m=(config.RE_INIT_SLEEP_TIME / 60.0)))
config.HTTP_PROXY_LIST = list(config.HTTP_PROXY_LIST_COMPLETE)
time.sleep(config.RE_INIT_SLEEP_TIME)
config.REQUEST_SLEEP += 1.0
logger.warning("Adding one second to request sleep time. Now {s}"
.format(s=config.REQUEST_SLEEP))
else:
logger.warning("HTTP 503 error from web site: IP address blocked. Waiting {m} minutes."
.format(m=(config.RE_INIT_SLEEP_TIME / 60.0)))
time.sleep(config.RE_INIT_SLEEP_TIME)
config.REQUEST_SLEEP += 1.0
return response
except (SystemExit, KeyboardInterrupt):
raise
except requests.exceptions.ConnectionError:
# For requests error and exceptions, see
# http://docs.python-requests.org/en/latest/user/quickstart/
# errors-and-exceptions
logger.warning("Network request exception {a}: connectionError".format(a=attempt_id))
return None
except requests.exceptions.HTTPError:
logger.error("Network request exception {a}: invalid HTTP response".format(a=attempt_id))
return None
except requests.exceptions.Timeout:
logger.warning("Network request exception {a}: timeout".format(a=attempt_id))
return None
except requests.exceptions.TooManyRedirects:
logger.error("Network request exception {a}: too many redirects".format(a=attempt_id))
return None
except requests.exceptions.RequestException:
logger.error("Network request exception {a}: unidentified requests".format(a=attempt_id))
return None
except Exception as e:
logger.exception("Network request exception: type " + type(e).__name__)
return None