This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wikipedia | |
from bs4 import BeautifulSoup, Tag | |
import datetime | |
import urllib.parse | |
from ankisync2.apkg import Apkg | |
from ankisync2.anki20 import db | |
if __name__ == "__main__": | |
tdelta = datetime.timedelta(milliseconds=500) | |
apkg = Apkg("casp_plus_003_terms.apkg") | |
soup = BeautifulSoup() | |
anchor_tag = soup.new_tag("a") | |
front_of_card = "" | |
back_of_card = "" | |
wiki_search = "" | |
wiki_page = None | |
summary_of_card = "" | |
print("# CASP+ 003 Terms") | |
print("") | |
for note in db.Notes.filter(): | |
for k, v in note.data.items(): | |
if v == "Front": | |
front_of_card = k | |
if v == "Back": | |
back_of_card = k | |
if bool(BeautifulSoup(back_of_card, "html.parser").find()): | |
wiki_search = BeautifulSoup(back_of_card, "html.parser").contents[0] | |
else: | |
wiki_search = back_of_card.replace('/', '') | |
wikipedia.set_rate_limiting(True, min_wait=tdelta) | |
answer_wiki = wikipedia.search(wiki_search, results=1) | |
try: | |
wiki_page = wikipedia.page(title=answer_wiki, auto_suggest=False, redirect=True) | |
anchor_tag.string = back_of_card | |
anchor_tag["href"] = wiki_page.url | |
summary_of_card = wiki_page.summary | |
except wikipedia.exceptions.DisambiguationError as e: | |
anchor_tag.string = back_of_card | |
google_search_string = urllib.parse.quote_plus(back_of_card) | |
anchor_tag["href"] = f"https://www.google.com/search?q={google_search_string}" | |
except wikipedia.exceptions.PageError as e: None | |
print(f"## {front_of_card}") | |
print(f"{str(anchor_tag)}") | |
print(f"{summary_of_card}") | |
apkg.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ankisync2 == 0.3.2 | |
wikipedia == 1.4.0 | |
beautifulsoup4 == 4.9.3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the main repository that leverages this script. https://github.com/trussworks/casp_plus_anki_decks