Skip to content

Instantly share code, notes, and snippets.

@j0yu
Last active June 15, 2021 12:34
Show Gist options
  • Select an option

  • Save j0yu/294d81222b9082ca4d828e31501fe594 to your computer and use it in GitHub Desktop.

Select an option

Save j0yu/294d81222b9082ca4d828e31501fe594 to your computer and use it in GitHub Desktop.
Fetch quote of the day from Wikiquote
"""Python 3.6 and above only."""
import datetime
import re
import requests # https://docs.python-requests.org/
def get_quote(html=True, date=None):
date = date or datetime.date.today()
prefix = "https://en.wikiquote.org/wiki/Wikiquote:Quote_of_the_day"
response = requests.get(f"{prefix}/{date:%B_%d,_%Y}") # June_15,_2021
response.raise_for_status()
matched_table = re.search(
r'<table style="text-align:center; width:100%">.+?</table>',
response.text.replace("\n", " "),
)
if matched_table:
untagged = re.sub(r"<[^>]+?>", "", matched_table.group())
return re.sub("(~[^~]+?~).*?$", r"<i>\1</i>", untagged) if html else untagged
raise ValueError("Not able to find table containing quote of the day")
if __name__ == "__main__":
print(get_quote(html=False))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment