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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
#
# The internetarchive module is a Python/CLI interface to Archive.org.
#
# Copyright (C) 2012-2024 Internet Archive
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
internetarchive.search
~~~~~~~~~~~~~~~~~~~~~~
This module provides objects for interacting with the Archive.org
search engine.
:copyright: (C) 2012-2024 by Internet Archive.
:license: AGPL 3, see LICENSE for more details.
"""
import itertools
from logging import getLogger
from requests.exceptions import ReadTimeout
from internetarchive.auth import S3Auth
log = getLogger(__name__)
class Search:
"""This class represents an archive.org item search. You can use
this class to search for Archive.org items using the advanced search
engine.
Usage::
>>> from internetarchive.session import ArchiveSession
>>> from internetarchive.search import Search
>>> s = ArchiveSession()
>>> search = Search(s, '(uploader:jake@archive.org)')
>>> for result in search:
... print(result['identifier'])
"""
def __init__(self, archive_session, query,
fields=None,
sorts=None,
params=None,
full_text_search=None,
dsl_fts=None,
request_kwargs=None,
max_retries=None):
params = params or {}
self.session = archive_session
self.dsl_fts = False if not dsl_fts else True
if self.dsl_fts or full_text_search:
self.fts = True
else:
self.fts = False
self.query = query
if self.fts and not self.dsl_fts:
self.query = f'!L {self.query}'
self.fields = fields or []
self.sorts = sorts or []
self.request_kwargs = request_kwargs or {}
self._num_found = None
self.fts_url = f'{self.session.protocol}//be-api.us.archive.org/ia-pub-fts-api'
self.scrape_url = f'{self.session.protocol}//{self.session.host}/services/search/v1/scrape'
self.search_url = f'{self.session.protocol}//{self.session.host}/advancedsearch.php'
if self.session.access_key and self.session.secret_key:
self.auth = S3Auth(self.session.access_key, self.session.secret_key)
else:
self.auth = None
self.max_retries = max_retries if max_retries is not None else 5
# Initialize params.
default_params = {'q': self.query}
if 'page' not in params:
if 'rows' in params:
params['page'] = 1
else:
default_params['count'] = 10000
else:
default_params['output'] = 'json'
# In the beta endpoint 'scope' was called 'index'.
# Let's support both for a while.
if 'index' in params:
params['scope'] = params['index']
del params['index']
self.params = default_params.copy()
self.params.update(params)
# Set timeout.
if 'timeout' not in self.request_kwargs:
self.request_kwargs['timeout'] = 300
# Set retries.
self.session.mount_http_adapter(max_retries=self.max_retries)
def __repr__(self):
return f'Search(query={self.query!r})'
def __iter__(self):
return self.iter_as_results()
def _advanced_search(self):
# Always return identifier.
if 'identifier' not in self.fields:
self.fields.append('identifier')
for k, v in enumerate(self.fields):
self.params[f'fl[{k}]'] = v
for i, field in enumerate(self.sorts):
self.params[f'sort[{i}]'] = field
self.params['output'] = 'json'
r = self.session.get(self.search_url,
params=self.params,
auth=self.auth,
**self.request_kwargs)
j = r.json()
num_found = int(j.get('response', {}).get('numFound', 0))
if not self._num_found:
self._num_found = num_found
if j.get('error'):
yield j
yield from j.get('response', {}).get('docs', [])
def _scrape(self):
if self.fields:
self.params['fields'] = ','.join(self.fields)
if self.sorts:
self.params['sorts'] = ','.join(self.sorts)
i = 0
num_found = None
while True:
r = self.session.post(self.scrape_url,
params=self.params,
auth=self.auth,
**self.request_kwargs)
j = r.json()
if j.get('error'):
yield j
if not num_found:
num_found = int(j.get('total') or '0')
if not self._num_found:
self._num_found = num_found
self._handle_scrape_error(j)
self.params['cursor'] = j.get('cursor')
for item in j['items']:
i += 1
yield item
if 'cursor' not in j:
if i != num_found:
raise ReadTimeout('The server failed to return results in the'
f' allotted amount of time for {r.request.url}')
break
def _full_text_search(self):
d = {
'q': self.query,
'size': '10000',
'from': '0',
'scroll': 'true',
}
if 'scope' in self.params:
d['scope'] = self.params['scope']
if 'size' in self.params:
d['scroll'] = False
d['size'] = self.params['size']
while True:
r = self.session.post(self.fts_url,
json=d,
auth=self.auth,
**self.request_kwargs)
j = r.json()
scroll_id = j.get('_scroll_id')
hits = j.get('hits', {}).get('hits')
if not hits:
return
yield from hits
if not hits or d['scroll'] is False:
break
d['scroll_id'] = scroll_id
def _make_results_generator(self):
if self.fts:
return self._full_text_search()
if 'user_aggs' in self.params:
return self._user_aggs()
elif 'page' in self.params:
return self._advanced_search()
else:
return self._scrape()
def _user_aggs(self):
"""Experimental support for user aggregations.
"""
del self.params['count'] # advanced search will error if this param is present!
self.params['page'] = '1'
self.params['rows'] = '1'
self.params['output'] = 'json'
r = self.session.get(self.search_url,
params=self.params,
auth=self.auth,
**self.request_kwargs)
j = r.json()
if j.get('error'):
yield j
for agg in j.get('response', {}).get('aggregations', {}).items():
yield {agg[0]: agg[1]}
@property
def num_found(self):
if not self._num_found:
if not self.fts and 'page' in self.params:
p = self.params.copy()
p['output'] = 'json'
r = self.session.get(self.search_url,
params=p,
auth=self.auth,
**self.request_kwargs)
j = r.json()
num_found = int(j.get('response', {}).get('numFound', 0))
if not self._num_found:
self._num_found = num_found
elif not self.fts:
p = self.params.copy()
p['total_only'] = 'true'
r = self.session.post(self.scrape_url,
params=p,
auth=self.auth,
**self.request_kwargs)
j = r.json()
self._handle_scrape_error(j)
self._num_found = j.get('total')
else:
self.params['q'] = self.query
r = self.session.get(self.fts_url,
params=self.params,
auth=self.auth,
**self.request_kwargs)
j = r.json()
self._num_found = j.get('hits', {}).get('total')
return self._num_found
def _handle_scrape_error(self, j):
if 'error' in j:
if all(s in j['error'].lower() for s in ['invalid', 'secret']):
if not j['error'].endswith('.'):
j['error'] += '.'
raise ValueError(f"{j['error']} Try running 'ia configure' and retrying.")
raise ValueError(j.get('error'))
def _get_item_from_search_result(self, search_result):
return self.session.get_item(search_result['identifier'])
def iter_as_results(self):
return SearchIterator(self, self._make_results_generator())
def iter_as_items(self):
_map = map(self._get_item_from_search_result, self._make_results_generator())
return SearchIterator(self, _map)
def __len__(self):
return self.num_found
class SearchIterator:
"""This class is an iterator wrapper for search results.
It provides access to the underlying Search, and supports
len() (since that is known initially)."""
def __init__(self, search, iterator):
self.search = search
self.iterator = iterator
def __len__(self):
return self.search.num_found
def __next__(self):
return next(self.iterator)
def __iter__(self):
return self
def __repr__(self):
return f'{self.__class__.__name__}({self.search!r}, {self.iterator!r})'
|