diff --git a/app.py b/app.py index 7d28899..81c2eef 100755 --- a/app.py +++ b/app.py @@ -960,33 +960,30 @@ def browse_facets() -> Response: 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.""" q = Item.query.filter_by(is_artwork=True) # type: ignore - for pid, qid in params: - 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:]), + q = q.join(Triple, Item.item_id == Triple.subject_id, aliased=True).filter( + Triple.predicate_id == pid[1:], Triple.object_id == qid[1:] ) return q -def get_db_facets(params: list[tuple[str, str]]): + +def get_db_facets(params): 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: - 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.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:], ) q = q.group_by(t.predicate_id, t.object_id) diff --git a/depicts/__init__.py b/depicts/__init__.py deleted file mode 100644 index 2d3cae9..0000000 --- a/depicts/__init__.py +++ /dev/null @@ -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} diff --git a/depicts/mediawiki.py b/depicts/mediawiki.py index 48fc35b..4331bc7 100644 --- a/depicts/mediawiki.py +++ b/depicts/mediawiki.py @@ -7,7 +7,7 @@ import typing import requests -from . import utils, user_agent_headers +from . import utils 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, headers=user_agent_headers()) + r = requests.get(api_url, params=call_params, timeout=5) 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, headers=user_agent_headers()) + r = requests.post(api_url, data=call_params, timeout=5) return r @@ -99,15 +99,10 @@ 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): - 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 + entity = json.load(open(filename)) + else: + entity = get_entity(qid, redirects=True) + json.dump(entity, open(filename, "w"), indent=2) return entity