Last active
June 15, 2021 12:34
-
-
Save j0yu/294d81222b9082ca4d828e31501fe594 to your computer and use it in GitHub Desktop.
Fetch quote of the day from Wikiquote
This file contains hidden or 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
| """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