-
Notifications
You must be signed in to change notification settings - Fork 0
/
Driver.py
65 lines (57 loc) · 2.42 KB
/
Driver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from bs4 import BeautifulSoup
import requests
import emoji
import pyperclip
# the url to fetch the national holidays from
URL = 'https://nationaldaycalendar.com/what-day-is-it/'
# the HTML class name for each of the events we're interested in
SEARCH_STRING = 'ultp-block-title'
class Driver:
def __init__(self):
self.browser = None
self.text = ''
# fetch the data and build the driver
def build(self):
request = requests.get(URL)
self.browser = BeautifulSoup(request.content, "html.parser")
# uses the emoji library to check each word in the holiday and return the emoji for it
# (param) holiday: str - the current holiday (ex: 'National Pizza Day')
def get_emojis(self, holiday):
emojis = ''
for word in holiday.split(' '):
lowercase = word.lower()
word_to_emoji = emoji.emojize(f':{lowercase}:', language='alias')
if emoji.is_emoji(word_to_emoji):
emojis += word_to_emoji
return emojis
# trims the raw html down to just the text of each holiday
# (param) holidays: str[] - a formatted list of all of today's national holidays
def trim_holiday_html(self, holidays):
formatted = []
for holiday in holidays:
holiday_text = holiday.text
pure_holiday = holiday_text.split('–')
formatted.append(pure_holiday[0])
return sorted(formatted, key=len)
# formats the holidays by length into a line separated text string
# (param) holidays: str[] - a list of all of today's national holidays
def format_holiday_text(self, holidays):
text = ''
formatted_holidays = self.trim_holiday_html(holidays)
for index, holiday in enumerate(formatted_holidays):
emojis = self.get_emojis(holiday)
if index != len(holidays) - 1:
text += f'- {holiday} {emojis}\n'
else:
text += f'- {holiday} {emojis}'
return text.title()
# searches the html request for the search string we specified
# (ex: the class that each holiday has)
def get_holidays(self):
data = self.browser.find_all("h3", class_=SEARCH_STRING)
self.text = self.format_holiday_text(data)
return self.text
# copies the contents of the events to the clipboard
def copy_to_clipboard(self):
pyperclip.copy(self.text)
return self.text