-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanycrawler.py
More file actions
165 lines (138 loc) · 5.14 KB
/
Copy pathanycrawler.py
File metadata and controls
165 lines (138 loc) · 5.14 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from __future__ import annotations
import json
from typing import Any, Dict, Optional
from urllib import error as urllib_error
from urllib import request as urllib_request
def _normalize_base_url(base_url: str) -> str:
return base_url.rstrip("/")
def _parse_optional_number(value: Optional[str]) -> Optional[int]:
if value in (None, ""):
return None
try:
return int(value)
except ValueError:
try:
return int(float(value))
except ValueError:
return None
def _build_meta(status: int, headers: Any) -> Dict[str, Any]:
return {
"status": status,
"requestId": headers.get("x-request-id"),
"creditsReserved": _parse_optional_number(headers.get("x-credits-reserved")),
"creditsUsed": _parse_optional_number(headers.get("x-credits-used")),
"browserMsUsed": _parse_optional_number(headers.get("x-browser-ms-used")),
}
def _parse_json(text: str) -> Any:
if not text:
return None
return json.loads(text)
class AnyCrawlerError(Exception):
def __init__(
self,
message: str,
*,
status: int,
error_code: Optional[str],
data: Any,
meta: Dict[str, Any],
) -> None:
super().__init__(message)
self.status = status
self.error_code = error_code
self.error_message = message
self.data = data
self.meta = meta
class AnyCrawlerClient:
def __init__(
self,
api_key: str,
*,
base_url: str = "https://api.anycrawler.com",
timeout: float = 60.0,
headers: Optional[Dict[str, str]] = None,
) -> None:
if not api_key:
raise ValueError("AnyCrawlerClient requires an api_key.")
self.api_key = api_key
self.base_url = _normalize_base_url(base_url)
self.timeout = timeout
self.default_headers = headers or {}
def get_account_usage(self) -> Dict[str, Any]:
return self._request("GET", "/v1/account/usage")
def crawl_page(self, payload: Dict[str, Any]) -> Dict[str, Any]:
return self._request("POST", "/v1/crawl/page", payload)
def crawl_screenshot(self, payload: Dict[str, Any]) -> Dict[str, Any]:
return self._request("POST", "/v1/crawl/screenshot", payload)
def search(self, payload: Dict[str, Any]) -> Dict[str, Any]:
return self._request("POST", "/v1/search", payload)
def search_page(self, payload: Dict[str, Any]) -> Dict[str, Any]:
return self._request("POST", "/v1/search/page", payload)
def search_news(self, payload: Dict[str, Any]) -> Dict[str, Any]:
return self._request("POST", "/v1/search/news", payload)
def _request(
self,
method: str,
pathname: str,
payload: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
headers = {
"Accept": "application/json",
"Authorization": f"Bearer {self.api_key}",
**self.default_headers,
}
data: Optional[bytes] = None
if payload is not None:
headers["Content-Type"] = "application/json"
data = json.dumps(payload).encode("utf-8")
request = urllib_request.Request(
f"{self.base_url}{pathname}",
data=data,
headers=headers,
method=method,
)
try:
with urllib_request.urlopen(request, timeout=self.timeout) as response:
status = response.getcode()
raw_text = response.read().decode("utf-8")
parsed = _parse_json(raw_text)
meta = _build_meta(status, response.headers)
return {
"data": parsed,
"meta": meta,
}
except urllib_error.HTTPError as exc:
raw_text = exc.read().decode("utf-8", errors="replace")
parsed = _parse_json(raw_text)
meta = _build_meta(exc.code, exc.headers)
body = parsed if isinstance(parsed, dict) else {}
error_code = body.get("error_code") if isinstance(body.get("error_code"), str) else None
error_message = (
body.get("error_message")
if isinstance(body.get("error_message"), str)
else body.get("message")
if isinstance(body.get("message"), str)
else f"AnyCrawler request failed with status {exc.code}."
)
raise AnyCrawlerError(
error_message,
status=exc.code,
error_code=error_code,
data=parsed,
meta=meta,
) from exc
except urllib_error.URLError as exc:
meta = {
"status": 0,
"requestId": None,
"creditsReserved": None,
"creditsUsed": None,
"browserMsUsed": None,
}
raise AnyCrawlerError(
f"AnyCrawler request failed before receiving an HTTP response: {exc.reason}",
status=0,
error_code=None,
data=None,
meta=meta,
) from exc