Compare commits

..

No commits in common. "591ffa60559bdc5247cd2c3a1f70aedb2189a429" and "dcb0849d2615f0b3d94b168c67307152003e016e" have entirely different histories.

3 changed files with 20 additions and 35 deletions

29
app.py
View file

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

View file

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