Pass user agent header
This commit is contained in:
parent
dcb0849d26
commit
a845ade3df
2 changed files with 19 additions and 7 deletions
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
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue