-
-
Save rafa2000/4ef2439b7be876f06303a04cf58b9669 to your computer and use it in GitHub Desktop.
Get Word of the Day from dictionary.com https://imgur.com/a/YicwTP6
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
#!/usr/bin/env python3 | |
import urllib3 | |
from bs4 import BeautifulSoup | |
from os import path | |
class bcolors: | |
HEADER = '\033[95m' | |
CYAN = '\033[36m' | |
BLUE = '\033[34m' | |
OKBLUE = '\033[94m' | |
OKGREEN = '\033[92m' | |
WARNING = '\033[93m' | |
FAIL = '\033[91m' | |
ENDC = '\033[0m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
ITALIC = '\033[3m' | |
DIM = '\033[2m' | |
http = urllib3.PoolManager() | |
try: | |
page_url = "https://www.dictionary.com/e/word-of-the-day/" | |
page = http.request('GET', page_url).data | |
soup = BeautifulSoup(page, "html.parser") | |
wotd_div = soup.find("div", class_="wotd-item-headword") | |
wotd_word = wotd_div.find("div", class_="wotd-item-headword__word").find("h1").text | |
wotd_pos_blocks = wotd_div.find("div", class_="wotd-item-headword__pos").find_all("p") | |
wotd_type = wotd_pos_blocks[0].find("span", class_="luna-pos").text | |
wotd_explanation = wotd_pos_blocks[1].text | |
wotd_pronunciation = wotd_div.find("div", class_="wotd-item-headword__pronunciation").find("div").text.strip() | |
line1 = f"{bcolors.CYAN}{bcolors.BOLD}{wotd_word}{bcolors.ENDC} {wotd_pronunciation}: {bcolors.DIM}{bcolors.ITALIC}{wotd_type}{bcolors.ENDC}" | |
line2 = f"{wotd_explanation}" | |
with open(path.join(path.expanduser("~"), ".wotd"), "w") as f: | |
f.write(f" {line1}\n {line2}\n") | |
except Exception as e: # Word of the day stays the same if something fails, e.g. no internet | |
print("failed to get word of the day: ", e) | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated for 'urllib3' on Python 3