MCPcopy Create free account
hub / github.com/internetarchive/openlibrary / query

Method query

openlibrary/api.py:174–209  ·  view source on GitHub ↗

Query Open Library. Open Library always limits the result to 1000 items due to performance issues. Pass limit=False to fetch all matching results by making multiple requests to the server. Please note that an iterator is returned instead of list when limit=False is

(self, q=None, **kw)

Source from the content-addressed store, hash-verified

172 return self._call_write("new", query, comment, action)
173
174 def query(self, q=None, **kw):
175 """Query Open Library.
176
177 Open Library always limits the result to 1000 items due to
178 performance issues. Pass limit=False to fetch all matching
179 results by making multiple requests to the server. Please note
180 that an iterator is returned instead of list when limit=False is
181 passed.::
182
183 >>> ol.query({'type': '/type/type', 'limit': 2}) #doctest: +SKIP
184 [{'key': '/type/property'}, {'key': '/type/type'}]
185
186 >>> ol.query(type='/type/type', limit=2) #doctest: +SKIP
187 [{'key': '/type/property'}, {'key': '/type/type'}]
188 """
189 q = dict(q or {})
190 q.update(kw)
191 q = marshal(q)
192
193 def unlimited_query(q):
194 q["limit"] = 1000
195 q.setdefault("offset", 0)
196 q.setdefault("sort", "key")
197
198 while True:
199 result = self.query(q)
200 yield from result
201 if len(result) < 1000:
202 break
203 q["offset"] += len(result)
204
205 if "limit" in q and q["limit"] is False:
206 return unlimited_query(q)
207 else:
208 response = self._request("/query.json", params={"query": json.dumps(q)})
209 return unmarshal(response.json())
210
211 def search(self, query, limit=10, offset=0, fields: list[str] | None = None):
212 return self._request(

Callers 15

mainFunction · 0.95
unlimited_queryMethod · 0.95
upgrade_011Method · 0.45
upgrade_012Method · 0.45
upgrade_013Method · 0.45
upgrade_014Method · 0.45
read_schemaMethod · 0.45
findFunction · 0.45
queryFunction · 0.45
fetch_affected_keysFunction · 0.45

Calls 6

_requestMethod · 0.95
marshalFunction · 0.85
unmarshalFunction · 0.85
dumpsMethod · 0.80
updateMethod · 0.45
jsonMethod · 0.45

Tested by

no test coverage detected