-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcore.py
More file actions
172 lines (120 loc) · 5.64 KB
/
core.py
File metadata and controls
172 lines (120 loc) · 5.64 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
166
167
168
169
170
171
172
from .http import HTTPClient
from .exceptions import SearchIDNotProvided
from .models import SerpResults
class Client(HTTPClient):
"""A class that handles API requests to SerpApi in a user–friendly manner.
:param api_key: The API Key to use for SerpApi.com.
Please provide ``api_key`` when instantiating this class. We recommend storing this in an environment variable, like so:
.. code-block:: bash
$ export SERPAPI_KEY=YOUR_API_KEY
.. code-block:: python
import os
import serpapi
serpapi = serpapi.Client(api_key=os.environ["SERPAPI_KEY"])
"""
DASHBOARD_URL = "https://serpapi.com/dashboard"
def __init__(self, *, api_key=None, timeout=None):
super().__init__(api_key=api_key, timeout=timeout)
def __repr__(self):
return "<SerpApi Client>"
def search(self, params: dict = None, **kwargs):
"""Fetch a page of results from SerpApi. Returns a :class:`SerpResults <serpapi.client.SerpResults>` object, or unicode text (*e.g.* if ``'output': 'html'`` was passed).
The following three calls are equivalent:
.. code-block:: python
>>> s = serpapi.search(q="Coffee", location="Austin, Texas, United States")
.. code-block:: python
>>> params = {"q": "Coffee", "location": "Austin, Texas, United States"}
>>> s = serpapi.search(**params)
.. code-block:: python
>>> params = {"q": "Coffee", "location": "Austin, Texas, United States"}
>>> s = serpapi.search(params)
:param q: typically, this is the parameter for the search engine query.
:param engine: the search engine to use. Defaults to ``google``.
:param output: the output format desired (``html`` or ``json``). Defaults to ``json``.
:param api_key: the API Key to use for SerpApi.com.
:param **: any additional parameters to pass to the API.
**Learn more**: https://serpapi.com/search-api
"""
if params is None:
params = {}
# These are arguments that should be passed to the underlying requests.request call.
request_kwargs = {}
for key in ["timeout", "proxies", "verify", "stream", "cert"]:
if key in kwargs:
request_kwargs[key] = kwargs.pop(key)
if kwargs:
params.update(kwargs)
r = self.request("GET", "/search", params=params, **request_kwargs)
return SerpResults.from_http_response(r, client=self)
def search_archive(self, params: dict = None, **kwargs):
"""Get a result from the SerpApi Search Archive API.
:param search_id: the Search ID of the search to retrieve from the archive.
:param api_key: the API Key to use for SerpApi.com.
:param output: the output format desired (``html`` or ``json``). Defaults to ``json``.
:param **: any additional parameters to pass to the API.
**Learn more**: https://serpapi.com/search-archive-api
"""
if params is None:
params = {}
# These are arguments that should be passed to the underlying requests.request call.
request_kwargs = {}
for key in ["timeout", "proxies", "verify", "stream", "cert"]:
if key in kwargs:
request_kwargs[key] = kwargs.pop(key)
if kwargs:
params.update(kwargs)
try:
search_id = params["search_id"]
except KeyError:
raise SearchIDNotProvided(
f"Please provide 'search_id', found here: { self.DASHBOARD_URL }"
)
r = self.request("GET", f"/searches/{ search_id }", params=params, **request_kwargs)
return SerpResults.from_http_response(r, client=self)
def locations(self, params: dict = None, **kwargs):
"""Get a list of supported Google locations.
:param q: restricts your search to locations that contain the supplied string.
:param limit: limits the number of locations returned.
:param **: any additional parameters to pass to the API.
**Learn more**: https://serpapi.com/locations-api
"""
if params is None:
params = {}
# These are arguments that should be passed to the underlying requests.request call.
request_kwargs = {}
for key in ["timeout", "proxies", "verify", "stream", "cert"]:
if key in kwargs:
request_kwargs[key] = kwargs.pop(key)
if kwargs:
params.update(kwargs)
r = self.request(
"GET",
"/locations.json",
params=params,
assert_200=True,
**request_kwargs,
)
return r.json()
def account(self, params: dict = None, **kwargs):
"""Get SerpApi account information.
:param api_key: the API Key to use for SerpApi.com.
:param **: any additional parameters to pass to the API.
**Learn more**: https://serpapi.com/account-api
"""
if params is None:
params = {}
# These are arguments that should be passed to the underlying requests.request call.
request_kwargs = {}
for key in ["timeout", "proxies", "verify", "stream", "cert"]:
if key in kwargs:
request_kwargs[key] = kwargs.pop(key)
if kwargs:
params.update(kwargs)
r = self.request("GET", "/account.json", params=params, assert_200=True, **request_kwargs)
return r.json()
# An un-authenticated client instance.
_client = Client()
search = _client.search
search_archive = _client.search_archive
locations = _client.locations
account = _client.account