raspbytes 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- raspbytes/__init__.py +87 -0
- raspbytes/_core/__init__.py +1 -0
- raspbytes/_core/constants.py +38 -0
- raspbytes/_core/http.py +160 -0
- raspbytes/_core/transport.py +175 -0
- raspbytes/_core/types.py +11 -0
- raspbytes/_core/version.py +8 -0
- raspbytes/client.py +88 -0
- raspbytes/config.py +122 -0
- raspbytes/countries.py +278 -0
- raspbytes/exceptions.py +115 -0
- raspbytes/managed_scrapers.py +4 -0
- raspbytes/models.py +89 -0
- raspbytes/py.typed +1 -0
- raspbytes/resources/__init__.py +17 -0
- raspbytes/resources/_base.py +75 -0
- raspbytes/resources/_polling.py +73 -0
- raspbytes/resources/browser.py +185 -0
- raspbytes/resources/proxies.py +49 -0
- raspbytes/resources/scraper/__init__.py +45 -0
- raspbytes/resources/scraper/_generated/__init__.py +1 -0
- raspbytes/resources/scraper/_generated/managed.py +190 -0
- raspbytes/resources/scraper/_generated/wire.py +632 -0
- raspbytes/resources/scraper/_managed.py +14 -0
- raspbytes/resources/scraper/_request.py +128 -0
- raspbytes/resources/scraper/builder.py +199 -0
- raspbytes/resources/scraper/hub.py +168 -0
- raspbytes/resources/scraper/monitoring.py +132 -0
- raspbytes/resources/scraper/runs.py +202 -0
- raspbytes/resources/scraper/schedules.py +172 -0
- raspbytes/resources/scraper/storage.py +155 -0
- raspbytes/resources/scraper/tasks.py +134 -0
- raspbytes/resources/scraper/types.py +96 -0
- raspbytes/resources/unblocker.py +131 -0
- raspbytes/scraper_types.py +4 -0
- raspbytes-1.1.0.dist-info/METADATA +297 -0
- raspbytes-1.1.0.dist-info/RECORD +39 -0
- raspbytes-1.1.0.dist-info/WHEEL +4 -0
- raspbytes-1.1.0.dist-info/licenses/LICENSE +25 -0
raspbytes/countries.py
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
"""ISO country names for display. Discover availability through each product API."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from types import MappingProxyType
|
|
8
|
+
|
|
9
|
+
from .exceptions import ConfigurationError
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True)
|
|
13
|
+
class Country:
|
|
14
|
+
code: str
|
|
15
|
+
name: str
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
COUNTRIES = MappingProxyType(
|
|
19
|
+
{
|
|
20
|
+
"AD": Country("AD", "Andorra"),
|
|
21
|
+
"AE": Country("AE", "United Arab Emirates"),
|
|
22
|
+
"AF": Country("AF", "Afghanistan"),
|
|
23
|
+
"AG": Country("AG", "Antigua & Barbuda"),
|
|
24
|
+
"AI": Country("AI", "Anguilla"),
|
|
25
|
+
"AL": Country("AL", "Albania"),
|
|
26
|
+
"AM": Country("AM", "Armenia"),
|
|
27
|
+
"AO": Country("AO", "Angola"),
|
|
28
|
+
"AQ": Country("AQ", "Antarctica"),
|
|
29
|
+
"AR": Country("AR", "Argentina"),
|
|
30
|
+
"AS": Country("AS", "American Samoa"),
|
|
31
|
+
"AT": Country("AT", "Austria"),
|
|
32
|
+
"AU": Country("AU", "Australia"),
|
|
33
|
+
"AW": Country("AW", "Aruba"),
|
|
34
|
+
"AX": Country("AX", "Åland Islands"),
|
|
35
|
+
"AZ": Country("AZ", "Azerbaijan"),
|
|
36
|
+
"BA": Country("BA", "Bosnia & Herzegovina"),
|
|
37
|
+
"BB": Country("BB", "Barbados"),
|
|
38
|
+
"BD": Country("BD", "Bangladesh"),
|
|
39
|
+
"BE": Country("BE", "Belgium"),
|
|
40
|
+
"BF": Country("BF", "Burkina Faso"),
|
|
41
|
+
"BG": Country("BG", "Bulgaria"),
|
|
42
|
+
"BH": Country("BH", "Bahrain"),
|
|
43
|
+
"BI": Country("BI", "Burundi"),
|
|
44
|
+
"BJ": Country("BJ", "Benin"),
|
|
45
|
+
"BL": Country("BL", "St. Barthélemy"),
|
|
46
|
+
"BM": Country("BM", "Bermuda"),
|
|
47
|
+
"BN": Country("BN", "Brunei"),
|
|
48
|
+
"BO": Country("BO", "Bolivia"),
|
|
49
|
+
"BQ": Country("BQ", "Caribbean Netherlands"),
|
|
50
|
+
"BR": Country("BR", "Brazil"),
|
|
51
|
+
"BS": Country("BS", "Bahamas"),
|
|
52
|
+
"BT": Country("BT", "Bhutan"),
|
|
53
|
+
"BV": Country("BV", "Bouvet Island"),
|
|
54
|
+
"BW": Country("BW", "Botswana"),
|
|
55
|
+
"BY": Country("BY", "Belarus"),
|
|
56
|
+
"BZ": Country("BZ", "Belize"),
|
|
57
|
+
"CA": Country("CA", "Canada"),
|
|
58
|
+
"CC": Country("CC", "Cocos (Keeling) Islands"),
|
|
59
|
+
"CD": Country("CD", "Congo - Kinshasa"),
|
|
60
|
+
"CF": Country("CF", "Central African Republic"),
|
|
61
|
+
"CG": Country("CG", "Congo - Brazzaville"),
|
|
62
|
+
"CH": Country("CH", "Switzerland"),
|
|
63
|
+
"CI": Country("CI", "Côte d’Ivoire"),
|
|
64
|
+
"CK": Country("CK", "Cook Islands"),
|
|
65
|
+
"CL": Country("CL", "Chile"),
|
|
66
|
+
"CM": Country("CM", "Cameroon"),
|
|
67
|
+
"CN": Country("CN", "China"),
|
|
68
|
+
"CO": Country("CO", "Colombia"),
|
|
69
|
+
"CR": Country("CR", "Costa Rica"),
|
|
70
|
+
"CU": Country("CU", "Cuba"),
|
|
71
|
+
"CV": Country("CV", "Cape Verde"),
|
|
72
|
+
"CW": Country("CW", "Curaçao"),
|
|
73
|
+
"CX": Country("CX", "Christmas Island"),
|
|
74
|
+
"CY": Country("CY", "Cyprus"),
|
|
75
|
+
"CZ": Country("CZ", "Czechia"),
|
|
76
|
+
"DE": Country("DE", "Germany"),
|
|
77
|
+
"DJ": Country("DJ", "Djibouti"),
|
|
78
|
+
"DK": Country("DK", "Denmark"),
|
|
79
|
+
"DM": Country("DM", "Dominica"),
|
|
80
|
+
"DO": Country("DO", "Dominican Republic"),
|
|
81
|
+
"DZ": Country("DZ", "Algeria"),
|
|
82
|
+
"EC": Country("EC", "Ecuador"),
|
|
83
|
+
"EE": Country("EE", "Estonia"),
|
|
84
|
+
"EG": Country("EG", "Egypt"),
|
|
85
|
+
"EH": Country("EH", "Western Sahara"),
|
|
86
|
+
"ER": Country("ER", "Eritrea"),
|
|
87
|
+
"ES": Country("ES", "Spain"),
|
|
88
|
+
"ET": Country("ET", "Ethiopia"),
|
|
89
|
+
"FI": Country("FI", "Finland"),
|
|
90
|
+
"FJ": Country("FJ", "Fiji"),
|
|
91
|
+
"FK": Country("FK", "Falkland Islands (Islas Malvinas)"),
|
|
92
|
+
"FM": Country("FM", "Micronesia"),
|
|
93
|
+
"FO": Country("FO", "Faroe Islands"),
|
|
94
|
+
"FR": Country("FR", "France"),
|
|
95
|
+
"GA": Country("GA", "Gabon"),
|
|
96
|
+
"GB": Country("GB", "United Kingdom"),
|
|
97
|
+
"GD": Country("GD", "Grenada"),
|
|
98
|
+
"GE": Country("GE", "Georgia"),
|
|
99
|
+
"GF": Country("GF", "French Guiana"),
|
|
100
|
+
"GG": Country("GG", "Guernsey"),
|
|
101
|
+
"GH": Country("GH", "Ghana"),
|
|
102
|
+
"GI": Country("GI", "Gibraltar"),
|
|
103
|
+
"GL": Country("GL", "Greenland"),
|
|
104
|
+
"GM": Country("GM", "Gambia"),
|
|
105
|
+
"GN": Country("GN", "Guinea"),
|
|
106
|
+
"GP": Country("GP", "Guadeloupe"),
|
|
107
|
+
"GQ": Country("GQ", "Equatorial Guinea"),
|
|
108
|
+
"GR": Country("GR", "Greece"),
|
|
109
|
+
"GS": Country("GS", "South Georgia & South Sandwich Islands"),
|
|
110
|
+
"GT": Country("GT", "Guatemala"),
|
|
111
|
+
"GU": Country("GU", "Guam"),
|
|
112
|
+
"GW": Country("GW", "Guinea-Bissau"),
|
|
113
|
+
"GY": Country("GY", "Guyana"),
|
|
114
|
+
"HK": Country("HK", "Hong Kong"),
|
|
115
|
+
"HM": Country("HM", "Heard & McDonald Islands"),
|
|
116
|
+
"HN": Country("HN", "Honduras"),
|
|
117
|
+
"HR": Country("HR", "Croatia"),
|
|
118
|
+
"HT": Country("HT", "Haiti"),
|
|
119
|
+
"HU": Country("HU", "Hungary"),
|
|
120
|
+
"ID": Country("ID", "Indonesia"),
|
|
121
|
+
"IE": Country("IE", "Ireland"),
|
|
122
|
+
"IL": Country("IL", "Israel"),
|
|
123
|
+
"IM": Country("IM", "Isle of Man"),
|
|
124
|
+
"IN": Country("IN", "India"),
|
|
125
|
+
"IO": Country("IO", "British Indian Ocean Territory"),
|
|
126
|
+
"IQ": Country("IQ", "Iraq"),
|
|
127
|
+
"IR": Country("IR", "Iran"),
|
|
128
|
+
"IS": Country("IS", "Iceland"),
|
|
129
|
+
"IT": Country("IT", "Italy"),
|
|
130
|
+
"JE": Country("JE", "Jersey"),
|
|
131
|
+
"JM": Country("JM", "Jamaica"),
|
|
132
|
+
"JO": Country("JO", "Jordan"),
|
|
133
|
+
"JP": Country("JP", "Japan"),
|
|
134
|
+
"KE": Country("KE", "Kenya"),
|
|
135
|
+
"KG": Country("KG", "Kyrgyzstan"),
|
|
136
|
+
"KH": Country("KH", "Cambodia"),
|
|
137
|
+
"KI": Country("KI", "Kiribati"),
|
|
138
|
+
"KM": Country("KM", "Comoros"),
|
|
139
|
+
"KN": Country("KN", "St. Kitts & Nevis"),
|
|
140
|
+
"KP": Country("KP", "North Korea"),
|
|
141
|
+
"KR": Country("KR", "South Korea"),
|
|
142
|
+
"KW": Country("KW", "Kuwait"),
|
|
143
|
+
"KY": Country("KY", "Cayman Islands"),
|
|
144
|
+
"KZ": Country("KZ", "Kazakhstan"),
|
|
145
|
+
"LA": Country("LA", "Laos"),
|
|
146
|
+
"LB": Country("LB", "Lebanon"),
|
|
147
|
+
"LC": Country("LC", "St. Lucia"),
|
|
148
|
+
"LI": Country("LI", "Liechtenstein"),
|
|
149
|
+
"LK": Country("LK", "Sri Lanka"),
|
|
150
|
+
"LR": Country("LR", "Liberia"),
|
|
151
|
+
"LS": Country("LS", "Lesotho"),
|
|
152
|
+
"LT": Country("LT", "Lithuania"),
|
|
153
|
+
"LU": Country("LU", "Luxembourg"),
|
|
154
|
+
"LV": Country("LV", "Latvia"),
|
|
155
|
+
"LY": Country("LY", "Libya"),
|
|
156
|
+
"MA": Country("MA", "Morocco"),
|
|
157
|
+
"MC": Country("MC", "Monaco"),
|
|
158
|
+
"MD": Country("MD", "Moldova"),
|
|
159
|
+
"ME": Country("ME", "Montenegro"),
|
|
160
|
+
"MF": Country("MF", "St. Martin"),
|
|
161
|
+
"MG": Country("MG", "Madagascar"),
|
|
162
|
+
"MH": Country("MH", "Marshall Islands"),
|
|
163
|
+
"MK": Country("MK", "North Macedonia"),
|
|
164
|
+
"ML": Country("ML", "Mali"),
|
|
165
|
+
"MM": Country("MM", "Myanmar (Burma)"),
|
|
166
|
+
"MN": Country("MN", "Mongolia"),
|
|
167
|
+
"MO": Country("MO", "Macao"),
|
|
168
|
+
"MP": Country("MP", "Northern Mariana Islands"),
|
|
169
|
+
"MQ": Country("MQ", "Martinique"),
|
|
170
|
+
"MR": Country("MR", "Mauritania"),
|
|
171
|
+
"MS": Country("MS", "Montserrat"),
|
|
172
|
+
"MT": Country("MT", "Malta"),
|
|
173
|
+
"MU": Country("MU", "Mauritius"),
|
|
174
|
+
"MV": Country("MV", "Maldives"),
|
|
175
|
+
"MW": Country("MW", "Malawi"),
|
|
176
|
+
"MX": Country("MX", "Mexico"),
|
|
177
|
+
"MY": Country("MY", "Malaysia"),
|
|
178
|
+
"MZ": Country("MZ", "Mozambique"),
|
|
179
|
+
"NA": Country("NA", "Namibia"),
|
|
180
|
+
"NC": Country("NC", "New Caledonia"),
|
|
181
|
+
"NE": Country("NE", "Niger"),
|
|
182
|
+
"NF": Country("NF", "Norfolk Island"),
|
|
183
|
+
"NG": Country("NG", "Nigeria"),
|
|
184
|
+
"NI": Country("NI", "Nicaragua"),
|
|
185
|
+
"NL": Country("NL", "Netherlands"),
|
|
186
|
+
"NO": Country("NO", "Norway"),
|
|
187
|
+
"NP": Country("NP", "Nepal"),
|
|
188
|
+
"NR": Country("NR", "Nauru"),
|
|
189
|
+
"NU": Country("NU", "Niue"),
|
|
190
|
+
"NZ": Country("NZ", "New Zealand"),
|
|
191
|
+
"OM": Country("OM", "Oman"),
|
|
192
|
+
"PA": Country("PA", "Panama"),
|
|
193
|
+
"PE": Country("PE", "Peru"),
|
|
194
|
+
"PF": Country("PF", "French Polynesia"),
|
|
195
|
+
"PG": Country("PG", "Papua New Guinea"),
|
|
196
|
+
"PH": Country("PH", "Philippines"),
|
|
197
|
+
"PK": Country("PK", "Pakistan"),
|
|
198
|
+
"PL": Country("PL", "Poland"),
|
|
199
|
+
"PM": Country("PM", "St. Pierre & Miquelon"),
|
|
200
|
+
"PN": Country("PN", "Pitcairn Islands"),
|
|
201
|
+
"PR": Country("PR", "Puerto Rico"),
|
|
202
|
+
"PS": Country("PS", "Palestine"),
|
|
203
|
+
"PT": Country("PT", "Portugal"),
|
|
204
|
+
"PW": Country("PW", "Palau"),
|
|
205
|
+
"PY": Country("PY", "Paraguay"),
|
|
206
|
+
"QA": Country("QA", "Qatar"),
|
|
207
|
+
"RE": Country("RE", "Réunion"),
|
|
208
|
+
"RO": Country("RO", "Romania"),
|
|
209
|
+
"RS": Country("RS", "Serbia"),
|
|
210
|
+
"RU": Country("RU", "Russia"),
|
|
211
|
+
"RW": Country("RW", "Rwanda"),
|
|
212
|
+
"SA": Country("SA", "Saudi Arabia"),
|
|
213
|
+
"SB": Country("SB", "Solomon Islands"),
|
|
214
|
+
"SC": Country("SC", "Seychelles"),
|
|
215
|
+
"SD": Country("SD", "Sudan"),
|
|
216
|
+
"SE": Country("SE", "Sweden"),
|
|
217
|
+
"SG": Country("SG", "Singapore"),
|
|
218
|
+
"SH": Country("SH", "St. Helena"),
|
|
219
|
+
"SI": Country("SI", "Slovenia"),
|
|
220
|
+
"SJ": Country("SJ", "Svalbard & Jan Mayen"),
|
|
221
|
+
"SK": Country("SK", "Slovakia"),
|
|
222
|
+
"SL": Country("SL", "Sierra Leone"),
|
|
223
|
+
"SM": Country("SM", "San Marino"),
|
|
224
|
+
"SN": Country("SN", "Senegal"),
|
|
225
|
+
"SO": Country("SO", "Somalia"),
|
|
226
|
+
"SR": Country("SR", "Suriname"),
|
|
227
|
+
"SS": Country("SS", "South Sudan"),
|
|
228
|
+
"ST": Country("ST", "São Tomé & Príncipe"),
|
|
229
|
+
"SV": Country("SV", "El Salvador"),
|
|
230
|
+
"SX": Country("SX", "Sint Maarten"),
|
|
231
|
+
"SY": Country("SY", "Syria"),
|
|
232
|
+
"SZ": Country("SZ", "Eswatini"),
|
|
233
|
+
"TC": Country("TC", "Turks & Caicos Islands"),
|
|
234
|
+
"TD": Country("TD", "Chad"),
|
|
235
|
+
"TF": Country("TF", "French Southern Territories"),
|
|
236
|
+
"TG": Country("TG", "Togo"),
|
|
237
|
+
"TH": Country("TH", "Thailand"),
|
|
238
|
+
"TJ": Country("TJ", "Tajikistan"),
|
|
239
|
+
"TK": Country("TK", "Tokelau"),
|
|
240
|
+
"TL": Country("TL", "Timor-Leste"),
|
|
241
|
+
"TM": Country("TM", "Turkmenistan"),
|
|
242
|
+
"TN": Country("TN", "Tunisia"),
|
|
243
|
+
"TO": Country("TO", "Tonga"),
|
|
244
|
+
"TR": Country("TR", "Türkiye"),
|
|
245
|
+
"TT": Country("TT", "Trinidad & Tobago"),
|
|
246
|
+
"TV": Country("TV", "Tuvalu"),
|
|
247
|
+
"TW": Country("TW", "Taiwan"),
|
|
248
|
+
"TZ": Country("TZ", "Tanzania"),
|
|
249
|
+
"UA": Country("UA", "Ukraine"),
|
|
250
|
+
"UG": Country("UG", "Uganda"),
|
|
251
|
+
"UM": Country("UM", "U.S. Outlying Islands"),
|
|
252
|
+
"US": Country("US", "United States"),
|
|
253
|
+
"UY": Country("UY", "Uruguay"),
|
|
254
|
+
"UZ": Country("UZ", "Uzbekistan"),
|
|
255
|
+
"VA": Country("VA", "Vatican City"),
|
|
256
|
+
"VC": Country("VC", "St. Vincent & Grenadines"),
|
|
257
|
+
"VE": Country("VE", "Venezuela"),
|
|
258
|
+
"VG": Country("VG", "British Virgin Islands"),
|
|
259
|
+
"VI": Country("VI", "U.S. Virgin Islands"),
|
|
260
|
+
"VN": Country("VN", "Vietnam"),
|
|
261
|
+
"VU": Country("VU", "Vanuatu"),
|
|
262
|
+
"WF": Country("WF", "Wallis & Futuna"),
|
|
263
|
+
"WS": Country("WS", "Samoa"),
|
|
264
|
+
"YE": Country("YE", "Yemen"),
|
|
265
|
+
"YT": Country("YT", "Mayotte"),
|
|
266
|
+
"ZA": Country("ZA", "South Africa"),
|
|
267
|
+
"ZM": Country("ZM", "Zambia"),
|
|
268
|
+
"ZW": Country("ZW", "Zimbabwe"),
|
|
269
|
+
}
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def country_code(country: str | Country) -> str:
|
|
274
|
+
"""Return a normalized two-letter code for the API."""
|
|
275
|
+
code = (country.code if isinstance(country, Country) else country).strip()
|
|
276
|
+
if not re.fullmatch(r"[A-Za-z]{2}", code):
|
|
277
|
+
raise ConfigurationError("country must be a two-letter country code")
|
|
278
|
+
return code.lower()
|
raspbytes/exceptions.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class RaspbytesError(Exception):
|
|
8
|
+
"""Base class for all SDK errors."""
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ConfigurationError(RaspbytesError):
|
|
12
|
+
"""The SDK was configured with an invalid value."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class APIConnectionError(RaspbytesError):
|
|
16
|
+
"""The API could not be reached."""
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class APITimeoutError(APIConnectionError):
|
|
20
|
+
"""The request exceeded its configured timeout."""
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class APIError(RaspbytesError):
|
|
24
|
+
"""A non-success response returned by the Raspbytes API."""
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
message: str,
|
|
29
|
+
*,
|
|
30
|
+
status_code: int,
|
|
31
|
+
code: str | None = None,
|
|
32
|
+
request_id: str | None = None,
|
|
33
|
+
body: Any = None,
|
|
34
|
+
headers: Mapping[str, str] | None = None,
|
|
35
|
+
) -> None:
|
|
36
|
+
super().__init__(message)
|
|
37
|
+
self.message = message
|
|
38
|
+
self.status_code = status_code
|
|
39
|
+
self.code = code
|
|
40
|
+
self.request_id = request_id
|
|
41
|
+
self.body = body
|
|
42
|
+
self.headers = headers or {}
|
|
43
|
+
|
|
44
|
+
def __str__(self) -> str:
|
|
45
|
+
details = [f"status={self.status_code}"]
|
|
46
|
+
if self.code:
|
|
47
|
+
details.append(f"code={self.code}")
|
|
48
|
+
if self.request_id:
|
|
49
|
+
details.append(f"request_id={self.request_id}")
|
|
50
|
+
return f"{self.message} ({', '.join(details)})"
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class BadRequestError(APIError):
|
|
54
|
+
pass
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class AuthenticationError(APIError):
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class PaymentRequiredError(APIError):
|
|
62
|
+
pass
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class PermissionDeniedError(APIError):
|
|
66
|
+
pass
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class NotFoundError(APIError):
|
|
70
|
+
pass
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class ProxyAuthenticationError(APIError):
|
|
74
|
+
pass
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class UpstreamRequestError(APIError):
|
|
78
|
+
pass
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class RateLimitError(APIError):
|
|
82
|
+
pass
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class ServerError(APIError):
|
|
86
|
+
pass
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class PollTimeoutError(RaspbytesError):
|
|
90
|
+
"""A polling helper did not reach a terminal state in time."""
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
class TerminalStateError(RaspbytesError):
|
|
94
|
+
"""An asynchronous resource entered an unsuccessful terminal state."""
|
|
95
|
+
|
|
96
|
+
def __init__(self, resource_id: str, status: str, body: Any = None) -> None:
|
|
97
|
+
super().__init__(f"{resource_id} entered terminal state {status!r}")
|
|
98
|
+
self.resource_id = resource_id
|
|
99
|
+
self.status = status
|
|
100
|
+
self.body = body
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
class ScraperRunError(TerminalStateError):
|
|
104
|
+
"""A failed or cancelled Scraper run with its structured API error."""
|
|
105
|
+
|
|
106
|
+
def __init__(self, resource_id: str, status: str, body: Mapping[str, Any]) -> None:
|
|
107
|
+
super().__init__(resource_id, status, body)
|
|
108
|
+
error = body.get("error")
|
|
109
|
+
fields = error if isinstance(error, dict) else {}
|
|
110
|
+
self.request_id = resource_id
|
|
111
|
+
self.code = fields.get("code")
|
|
112
|
+
self.message = fields.get("message") or str(self)
|
|
113
|
+
self.details = fields.get("details")
|
|
114
|
+
self.retryable = fields.get("retryable") is True
|
|
115
|
+
self.args = (self.message,)
|
raspbytes/models.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from collections.abc import Mapping
|
|
4
|
+
from dataclasses import asdict, dataclass, field, is_dataclass
|
|
5
|
+
from http import HTTPMethod
|
|
6
|
+
from typing import Any, cast
|
|
7
|
+
|
|
8
|
+
from .exceptions import ConfigurationError
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _compact(value: Any) -> Any:
|
|
12
|
+
"""Convert request models to JSON-ready dictionaries and omit unset values."""
|
|
13
|
+
if is_dataclass(value) and not isinstance(value, type):
|
|
14
|
+
value = asdict(value)
|
|
15
|
+
if isinstance(value, Mapping):
|
|
16
|
+
return {str(key): _compact(item) for key, item in value.items() if item is not None}
|
|
17
|
+
if isinstance(value, (list, tuple)):
|
|
18
|
+
return [_compact(item) for item in value]
|
|
19
|
+
return value
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class RequestModel:
|
|
23
|
+
def to_dict(self) -> dict[str, Any]:
|
|
24
|
+
return cast(dict[str, Any], _compact(self))
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class ProxyConnectionParams(RequestModel):
|
|
29
|
+
proxy_type: str
|
|
30
|
+
session_type: str = "rotating"
|
|
31
|
+
country: str | None = None
|
|
32
|
+
protocol_selection: str = "http_https"
|
|
33
|
+
protocol: str | None = None
|
|
34
|
+
session_id: str | None = None
|
|
35
|
+
lifetime_minutes: int | None = None
|
|
36
|
+
state: str | None = None
|
|
37
|
+
city: str | None = None
|
|
38
|
+
asn: str | int | None = None
|
|
39
|
+
|
|
40
|
+
def __post_init__(self) -> None:
|
|
41
|
+
if self.state and self.city:
|
|
42
|
+
raise ConfigurationError("state and city cannot be used together")
|
|
43
|
+
if self.lifetime_minutes is not None and self.session_type != "sticky":
|
|
44
|
+
raise ConfigurationError("lifetime_minutes is only valid for sticky sessions")
|
|
45
|
+
if self.protocol_selection not in {"all", "http_https", "socks5"}:
|
|
46
|
+
raise ConfigurationError("protocol_selection must be all, http_https, or socks5")
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True)
|
|
50
|
+
class BrowserProxy(RequestModel):
|
|
51
|
+
type: str
|
|
52
|
+
country: str | None = None
|
|
53
|
+
sticky: bool = False
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class BrowserSessionParams(RequestModel):
|
|
58
|
+
browser: str
|
|
59
|
+
browser_version: str = "stable"
|
|
60
|
+
environment_profile: str = "desktop-balanced"
|
|
61
|
+
proxy: BrowserProxy | Mapping[str, Any] | None = None
|
|
62
|
+
session_timeout_seconds: int | None = None
|
|
63
|
+
|
|
64
|
+
def __post_init__(self) -> None:
|
|
65
|
+
if self.browser not in {"chromium", "firefox"}:
|
|
66
|
+
raise ConfigurationError("browser must be chromium or firefox")
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass(frozen=True)
|
|
70
|
+
class UnblockRequest(RequestModel):
|
|
71
|
+
url: str
|
|
72
|
+
method: str = HTTPMethod.GET.value
|
|
73
|
+
strategy: str = "adaptive"
|
|
74
|
+
headers: Mapping[str, str] | None = None
|
|
75
|
+
body: Any = None
|
|
76
|
+
cookies: Mapping[str, str] | None = None
|
|
77
|
+
proxy: Mapping[str, Any] | None = None
|
|
78
|
+
session: Mapping[str, Any] | None = None
|
|
79
|
+
extra: Mapping[str, Any] = field(default_factory=dict, repr=False)
|
|
80
|
+
|
|
81
|
+
def __post_init__(self) -> None:
|
|
82
|
+
if self.strategy not in {"standard", "adaptive", "premium"}:
|
|
83
|
+
raise ConfigurationError("strategy must be standard, adaptive, or premium")
|
|
84
|
+
|
|
85
|
+
def to_dict(self) -> dict[str, Any]:
|
|
86
|
+
result = cast(dict[str, Any], _compact(self))
|
|
87
|
+
extra = result.pop("extra", {})
|
|
88
|
+
result.update(extra)
|
|
89
|
+
return result
|
raspbytes/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"""Product-specific synchronous and asynchronous API resources."""
|
|
2
|
+
|
|
3
|
+
from .browser import AsyncBrowserResource, BrowserResource
|
|
4
|
+
from .proxies import AsyncProxiesResource, ProxiesResource
|
|
5
|
+
from .scraper import AsyncScraperResource, ScraperResource
|
|
6
|
+
from .unblocker import AsyncUnblockerResource, UnblockerResource
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"AsyncBrowserResource",
|
|
10
|
+
"AsyncProxiesResource",
|
|
11
|
+
"AsyncScraperResource",
|
|
12
|
+
"AsyncUnblockerResource",
|
|
13
|
+
"BrowserResource",
|
|
14
|
+
"ProxiesResource",
|
|
15
|
+
"ScraperResource",
|
|
16
|
+
"UnblockerResource",
|
|
17
|
+
]
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"""Shared resource primitives."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Mapping
|
|
6
|
+
from http import HTTPMethod
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from .._core.transport import AsyncTransport, SyncTransport
|
|
10
|
+
from .._core.types import Query, ResponseData
|
|
11
|
+
from ..config import BaseConfig, _resolve_config
|
|
12
|
+
from ..exceptions import ConfigurationError
|
|
13
|
+
from ..models import RequestModel
|
|
14
|
+
|
|
15
|
+
Payload = RequestModel | Mapping[str, Any]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def prepare_payload(value: Payload | None, fields: Mapping[str, Any]) -> dict[str, Any]:
|
|
19
|
+
if value is not None and fields:
|
|
20
|
+
raise ConfigurationError("pass either a request model/mapping or keyword fields, not both")
|
|
21
|
+
if value is None:
|
|
22
|
+
return {key: item for key, item in fields.items() if item is not None}
|
|
23
|
+
if isinstance(value, RequestModel):
|
|
24
|
+
return value.to_dict()
|
|
25
|
+
return {key: item for key, item in value.items() if item is not None}
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class SyncResource:
|
|
29
|
+
def __init__(self, transport: SyncTransport, config: BaseConfig | None = None) -> None:
|
|
30
|
+
self._transport = transport
|
|
31
|
+
self._request_config = _resolve_config(
|
|
32
|
+
config or BaseConfig(), fallback=transport._resolved_config
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
def _get(self, path: str, *, params: Query | None = None) -> ResponseData:
|
|
36
|
+
return self._transport.request(
|
|
37
|
+
HTTPMethod.GET, path, params=params, config=self._request_config
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
def _post(self, path: str, *, json: Mapping[str, Any] | None = None) -> ResponseData:
|
|
41
|
+
return self._transport.request(
|
|
42
|
+
HTTPMethod.POST, path, json=json, config=self._request_config
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
def _put(self, path: str, *, json: Mapping[str, Any] | None = None) -> ResponseData:
|
|
46
|
+
return self._transport.request(HTTPMethod.PUT, path, json=json, config=self._request_config)
|
|
47
|
+
|
|
48
|
+
def _delete(self, path: str) -> ResponseData:
|
|
49
|
+
return self._transport.request(HTTPMethod.DELETE, path, config=self._request_config)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class AsyncResource:
|
|
53
|
+
def __init__(self, transport: AsyncTransport, config: BaseConfig | None = None) -> None:
|
|
54
|
+
self._transport = transport
|
|
55
|
+
self._request_config = _resolve_config(
|
|
56
|
+
config or BaseConfig(), fallback=transport._resolved_config
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
async def _get(self, path: str, *, params: Query | None = None) -> ResponseData:
|
|
60
|
+
return await self._transport.request(
|
|
61
|
+
HTTPMethod.GET, path, params=params, config=self._request_config
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
async def _post(self, path: str, *, json: Mapping[str, Any] | None = None) -> ResponseData:
|
|
65
|
+
return await self._transport.request(
|
|
66
|
+
HTTPMethod.POST, path, json=json, config=self._request_config
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
async def _put(self, path: str, *, json: Mapping[str, Any] | None = None) -> ResponseData:
|
|
70
|
+
return await self._transport.request(
|
|
71
|
+
HTTPMethod.PUT, path, json=json, config=self._request_config
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
async def _delete(self, path: str) -> ResponseData:
|
|
75
|
+
return await self._transport.request(HTTPMethod.DELETE, path, config=self._request_config)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"""Polling loops shared by long-running product operations."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import time
|
|
7
|
+
from collections.abc import Awaitable, Callable, Collection
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from .._core.constants import ResponseField
|
|
11
|
+
from .._core.types import ResponseData
|
|
12
|
+
from ..exceptions import ConfigurationError, PollTimeoutError, TerminalStateError
|
|
13
|
+
|
|
14
|
+
ObjectResponse = dict[str, Any]
|
|
15
|
+
SuccessPredicate = Callable[[ObjectResponse], bool]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _as_object(data: ResponseData) -> ObjectResponse:
|
|
19
|
+
if not isinstance(data, dict):
|
|
20
|
+
raise ConfigurationError("expected an object response from the API")
|
|
21
|
+
return data
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def poll(
|
|
25
|
+
fetch: Callable[[], ResponseData],
|
|
26
|
+
*,
|
|
27
|
+
resource_id: str,
|
|
28
|
+
is_complete: SuccessPredicate,
|
|
29
|
+
failed_states: Collection[str],
|
|
30
|
+
timeout: float,
|
|
31
|
+
poll_interval: float,
|
|
32
|
+
timeout_message: str,
|
|
33
|
+
) -> ObjectResponse:
|
|
34
|
+
deadline = time.monotonic() + timeout
|
|
35
|
+
while True:
|
|
36
|
+
resource = _as_object(fetch())
|
|
37
|
+
status = str(resource.get(ResponseField.STATUS, ""))
|
|
38
|
+
if is_complete(resource):
|
|
39
|
+
return resource
|
|
40
|
+
if status in failed_states:
|
|
41
|
+
raise TerminalStateError(resource_id, status, resource)
|
|
42
|
+
remaining = deadline - time.monotonic()
|
|
43
|
+
if remaining <= 0:
|
|
44
|
+
raise PollTimeoutError(timeout_message)
|
|
45
|
+
time.sleep(min(poll_interval, remaining))
|
|
46
|
+
if time.monotonic() >= deadline:
|
|
47
|
+
raise PollTimeoutError(timeout_message)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
async def poll_async(
|
|
51
|
+
fetch: Callable[[], Awaitable[ResponseData]],
|
|
52
|
+
*,
|
|
53
|
+
resource_id: str,
|
|
54
|
+
is_complete: SuccessPredicate,
|
|
55
|
+
failed_states: Collection[str],
|
|
56
|
+
timeout: float,
|
|
57
|
+
poll_interval: float,
|
|
58
|
+
timeout_message: str,
|
|
59
|
+
) -> ObjectResponse:
|
|
60
|
+
deadline = time.monotonic() + timeout
|
|
61
|
+
while True:
|
|
62
|
+
resource = _as_object(await fetch())
|
|
63
|
+
status = str(resource.get(ResponseField.STATUS, ""))
|
|
64
|
+
if is_complete(resource):
|
|
65
|
+
return resource
|
|
66
|
+
if status in failed_states:
|
|
67
|
+
raise TerminalStateError(resource_id, status, resource)
|
|
68
|
+
remaining = deadline - time.monotonic()
|
|
69
|
+
if remaining <= 0:
|
|
70
|
+
raise PollTimeoutError(timeout_message)
|
|
71
|
+
await asyncio.sleep(min(poll_interval, remaining))
|
|
72
|
+
if time.monotonic() >= deadline:
|
|
73
|
+
raise PollTimeoutError(timeout_message)
|