Skip to content

Commit

Permalink
Check HTTP response status before calling requests.Response.json()
Browse files Browse the repository at this point in the history
Decode JSON would fail if HTTP response doesn't have valid
json-encoded content.
  • Loading branch information
xxyzz committed Mar 7, 2023
1 parent 631bf65 commit e841283
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions mediawiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def query(self, titles: set[str]) -> None:
"titles": "|".join(titles),
},
)
if not result.ok:
return
data = result.json()
converts = defaultdict(list)
redirect_to_sections: dict[str, dict[str, str]] = defaultdict(dict)
Expand Down Expand Up @@ -168,6 +170,8 @@ def get_section_text(
"page": page,
},
)
if not r.ok:
return
result = r.json()
for section in result.get("parse", {}).get("sections", []):
if section["line"] in section_to_titles:
Expand Down Expand Up @@ -295,6 +299,8 @@ def query(self, page: str, from_disambiguation_title: str | None = None) -> None
from rapidfuzz.process import extractOne

result = self.session.get(self.wiki_api, params={"page": page})
if not result.ok:
return
data = result.json()
if "parse" in data:
data = data["parse"]
Expand Down Expand Up @@ -427,6 +433,8 @@ def query(self, items: list[str]) -> None:
"https://query.wikidata.org/sparql",
params={"query": query, "format": "json"},
)
if not result.ok:
return
for binding in result.json().get("results", {}).get("bindings"):
item_id = binding["item"]["value"].split("/")[-1]
map_url = binding.get("map", {}).get("value")
Expand Down

0 comments on commit e841283

Please sign in to comment.