Skip to content

Commit

Permalink
Merge "[IMPR] use Site.search() in get_item_with_prop_value"
Browse files Browse the repository at this point in the history
  • Loading branch information
xqt authored and Gerrit Code Review committed Nov 27, 2024
2 parents beb62c3 + 4a0bdfc commit db149a8
Showing 1 changed file with 15 additions and 29 deletions.
44 changes: 15 additions & 29 deletions scripts/create_isbn_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,6 @@

import pywikibot # API interface to Wikidata
from pywikibot.config import verbose_output as verbose
from pywikibot.data import api
from pywikibot.tools import first_upper


Expand Down Expand Up @@ -813,7 +812,8 @@ def get_item_list(item_name: str,
def get_item_with_prop_value(prop: str, propval: str) -> set[str]:
"""Get list of items that have a property/value statement.
.. seealso:: :api:`Search`
.. seealso:: :meth:`Site.search()
<pywikibot.site._generators.GeneratorsMixin.search>`
:param prop: Property ID
:param propval: Property value
Expand All @@ -823,33 +823,19 @@ def get_item_with_prop_value(prop: str, propval: str) -> set[str]:
pywikibot.debug(f'Search statement: {srsearch}')
item_name_canon = unidecode(propval).casefold()
item_list = set()
# TODO: use APISite.search instead?
params = {
'action': 'query', # Statement search
'list': 'search',
'srsearch': srsearch,
'srwhat': 'text',
'format': 'json',
'srlimit': 50, # Should be reasonable value
}
request = api.Request(site=repo, parameters=params)
result = request.submit()
# https://www.wikidata.org/w/api.php?action=query&list=search&srwhat=text&srsearch=P212:978-94-028-1317-3
# https://www.wikidata.org/w/index.php?search=P212:978-94-028-1317-3

if 'query' in result and 'search' in result['query']:
# Loop though items
for row in result['query']['search']:
qnumber = row['title']
item = get_item_page(qnumber)

if prop not in item.claims:
continue

for seq in item.claims[prop]:
if unidecode(seq.getTarget()).casefold() == item_name_canon:
item_list.add(item) # Found match
break

# Loop though items
for row in repo.search(srsearch, where='text', total=50):
qnumber = row['title']
item = get_item_page(qnumber)

if prop not in item.claims:
continue

for seq in item.claims[prop]:
if unidecode(seq.getTarget()).casefold() == item_name_canon:
item_list.add(item) # Found match
break

pywikibot.log(item_list)
return item_list
Expand Down

0 comments on commit db149a8

Please sign in to comment.