Changed get_wikipedia_article to be more robust
Will now fall back to wiki if local copy isn't avail
This commit is contained in:
1 parent
f01728adb7
commit
b229f5816e
3 files changed
+250
-5
No files matched your search
@@ -40,11 +40,28 @@ def _wiki_mirror_url(wikipedia_url: str) -> str:
|
||||
|
||||
|
||||
def fetch_wikipedia_article(wikipedia_url: str) -> str:
|
||||
"""Fetch a Wikipedia article, using the local Kiwix mirror if WIKI_LINK is set."""
|
||||
"""Fetch a Wikipedia article, using the local Kiwix mirror if WIKI_LINK is set.
|
||||
Falls back to live Wikipedia if the mirror doesn't have the article.
|
||||
Raises requests.HTTPError if neither source has it."""
|
||||
wikipedia_url = wikipedia_url.replace(" ", "_")
|
||||
url = _wiki_mirror_url(wikipedia_url) if os.environ.get("WIKI_LINK") else wikipedia_url
|
||||
response = requests.get(url, timeout=10)
|
||||
response.raise_for_status()
|
||||
|
||||
urls_to_try = []
|
||||
if os.environ.get("WIKI_LINK"):
|
||||
urls_to_try.append(_wiki_mirror_url(wikipedia_url))
|
||||
urls_to_try.append(wikipedia_url)
|
||||
|
||||
response = None
|
||||
for url in urls_to_try:
|
||||
r = requests.get(url, timeout=10)
|
||||
if r.status_code == 404:
|
||||
print(f" Not found at {url}, trying next source...")
|
||||
continue
|
||||
r.raise_for_status()
|
||||
response = r
|
||||
break
|
||||
|
||||
if response is None:
|
||||
raise requests.HTTPError(f"Article not found in any source: {wikipedia_url}")
|
||||
|
||||
soup = BeautifulSoup(response.text, "html.parser")
|
||||
|
||||
@@ -164,7 +181,11 @@ if __name__ == "__main__":
|
||||
continue
|
||||
|
||||
print(f"[{i}/{total}] Generating entry for {specie['common_name']}...")
|
||||
description = prompt_llm(specie["wikipedia_url"])
|
||||
try:
|
||||
description = prompt_llm(specie["wikipedia_url"])
|
||||
except requests.HTTPError:
|
||||
print(f" No article found, skipping.")
|
||||
continue
|
||||
description = re.sub(r"[^A-Za-z0-9.,!?'\-: ]", "", description)
|
||||
print(f" {description}")
|
||||
writer.writerow([taxon_id, specie["common_name"], description])
|
||||
|
||||
Reference in new issue
Block a user