Compare commits

...

2 commits

3 changed files with 35 additions and 20 deletions

29
app.py
View file

@ -960,30 +960,33 @@ def browse_facets() -> Response:
return jsonify(params=params, facets=facets, prop_labels=find_more_props)
def get_db_items(params):
def get_db_items(params: list[tuple[str, str]]):
"""Get items for browse page based on criteria."""
q = Item.query.filter_by(is_artwork=True) # type: ignore
for pid, qid in params:
q = q.join(Triple, Item.item_id == Triple.subject_id, aliased=True).filter(
Triple.predicate_id == pid[1:], Triple.object_id == qid[1:]
t = aliased(Triple)
q = q.join(t, Item.item_id == t.subject_id).filter(
t.predicate_id == int(pid[1:]),
t.object_id == int(qid[1:]),
)
return q
def get_db_facets(params):
def get_db_facets(params: list[tuple[str, str]]):
t = aliased(Triple)
q = database.session.query(t.predicate_id, func.count().label("count"), t.object_id)
q = database.session.query(
t.predicate_id, func.count().label("count"), t.object_id
)
facet_limit = 18
for pid, qid in params:
q = q.join( # type: ignore
Triple, t.subject_id == Triple.subject_id, aliased=True
).filter(
Triple.predicate_id == pid[1:],
Triple.object_id == qid[1:],
t.predicate_id != pid[1:],
t.object_id != qid[1:],
t2 = aliased(Triple)
q = q.join(t2, t.subject_id == t2.subject_id).filter(
t2.predicate_id == int(pid[1:]),
t2.object_id == int(qid[1:]),
t.predicate_id != int(pid[1:]),
t.object_id != int(qid[1:]),
)
q = q.group_by(t.predicate_id, t.object_id)

7
depicts/__init__.py Normal file
View file

@ -0,0 +1,7 @@
user_agent = (
"depicts/0.1 (https://git.4angle.com/edward/depicts; edward@4angle.com)"
)
def user_agent_headers() -> dict[str, str]:
"""User-Agent headers."""
return {"User-Agent": user_agent}

View file

@ -7,7 +7,7 @@ import typing
import requests
from . import utils
from . import utils, user_agent_headers
from .type import CallParams, Entity
wikidata_url = "https://www.wikidata.org/w/api.php"
@ -28,7 +28,7 @@ def api_call(params: CallParams, api_url: str = wikidata_url) -> requests.Respon
**params,
}
r = requests.get(api_url, params=call_params, timeout=5)
r = requests.get(api_url, params=call_params, timeout=5, headers=user_agent_headers())
return r
@ -39,7 +39,7 @@ def api_post(params: CallParams, api_url: str = wikidata_url) -> requests.Respon
**params,
}
r = requests.post(api_url, data=call_params, timeout=5)
r = requests.post(api_url, data=call_params, timeout=5, headers=user_agent_headers())
return r
@ -99,10 +99,15 @@ def get_entity_with_cache(qid: str, refresh: bool = False) -> Entity | None:
filename = f"cache/{qid}.json"
entity: Entity | None
if not refresh and os.path.exists(filename):
entity = json.load(open(filename))
else:
entity = get_entity(qid, redirects=True)
json.dump(entity, open(filename, "w"), indent=2)
try:
entity = json.load(open(filename))
except json.decoder.JSONDecodeError:
pass
else:
return entity
entity = get_entity(qid, redirects=True)
json.dump(entity, open(filename, "w"), indent=2)
return entity
return entity