Compare commits
2 commits
dcb0849d26
...
591ffa6055
| Author | SHA1 | Date | |
|---|---|---|---|
| 591ffa6055 | |||
| a845ade3df |
3 changed files with 35 additions and 20 deletions
29
app.py
29
app.py
|
|
@ -960,30 +960,33 @@ 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):
|
def get_db_items(params: list[tuple[str, str]]):
|
||||||
"""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:
|
||||||
q = q.join(Triple, Item.item_id == Triple.subject_id, aliased=True).filter(
|
t = aliased(Triple)
|
||||||
Triple.predicate_id == pid[1:], Triple.object_id == qid[1:]
|
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
|
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(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
|
facet_limit = 18
|
||||||
|
|
||||||
for pid, qid in params:
|
for pid, qid in params:
|
||||||
q = q.join( # type: ignore
|
t2 = aliased(Triple)
|
||||||
Triple, t.subject_id == Triple.subject_id, aliased=True
|
q = q.join(t2, t.subject_id == t2.subject_id).filter(
|
||||||
).filter(
|
t2.predicate_id == int(pid[1:]),
|
||||||
Triple.predicate_id == pid[1:],
|
t2.object_id == int(qid[1:]),
|
||||||
Triple.object_id == qid[1:],
|
t.predicate_id != int(pid[1:]),
|
||||||
t.predicate_id != pid[1:],
|
t.object_id != int(qid[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)
|
||||||
|
|
|
||||||
7
depicts/__init__.py
Normal file
7
depicts/__init__.py
Normal 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}
|
||||||
|
|
@ -7,7 +7,7 @@ import typing
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from . import utils
|
from . import utils, user_agent_headers
|
||||||
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)
|
r = requests.get(api_url, params=call_params, timeout=5, headers=user_agent_headers())
|
||||||
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)
|
r = requests.post(api_url, data=call_params, timeout=5, headers=user_agent_headers())
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -99,10 +99,15 @@ 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))
|
||||||
|
except json.decoder.JSONDecodeError:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
|
return entity
|
||||||
entity = get_entity(qid, redirects=True)
|
entity = get_entity(qid, redirects=True)
|
||||||
json.dump(entity, open(filename, "w"), indent=2)
|
json.dump(entity, open(filename, "w"), indent=2)
|
||||||
|
return entity
|
||||||
|
|
||||||
return entity
|
return entity
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue