Skip to content

Instantly share code, notes, and snippets.

@rogeruiz
Created June 11, 2021 00:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogeruiz/bfa2e756372e83f3e45ec968bf3b8da2 to your computer and use it in GitHub Desktop.
Save rogeruiz/bfa2e756372e83f3e45ec968bf3b8da2 to your computer and use it in GitHub Desktop.
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()
ankisync2 == 0.3.2
wikipedia == 1.4.0
beautifulsoup4 == 4.9.3
@rogeruiz
Copy link
Author

This is helpful for pulling Wikipedia page links and summaries by searching the contents from the "back" of a given card. Hope this helps someone else too.

@rogeruiz
Copy link
Author

rogeruiz commented Jun 11, 2021

Run this with:

# Create the directories we'll need for processing from MD to APKG.
mkdir -p input output

# Create the input/deck.md file.
pip install -r requirements.txt
python ./create_markdown_from_cards.py 2>/dev/null > input/deck.md

# Edit the results from the script.
$EDITOR input/deck.md

# Install tooling for Markdown to Anki Decks processing.
pip install markdown-anki-decks

# Create the new .apkg file.
mdankideck input output

@rogeruiz
Copy link
Author

This is the main repository that leverages this script. https://github.com/trussworks/casp_plus_anki_decks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment