/
main.py
73 lines (59 loc) · 2.58 KB
/
main.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
66
67
68
69
70
71
72
73
from bs4 import BeautifulSoup as bs
import configparser
import pendulum
import requests
import time
import twilio_api
class CompTrain:
def __init__(self):
self.config = configparser.ConfigParser()
self.raw_list = []
self.parsed_list = []
self.class_url = 'http://comptrain.co/class/programming/'
self.individual_url = 'http://comptrain.co/individuals/home/'
self.found_WOD = False
def check_date_change(self, today):
while self.found_WOD:
if pendulum.today().format('%A %m.%d.%y') != today:
print('Today is a new day! Let us check for a new WOD')
self.found_WOD = False
self.wod_is_posted(self.class_url)
else:
print("Tommorrow's WOD has been posted and checked. Waiting for a new day to check again.")
time.sleep(10) # one hour is 3600 seconds
def get_current_dates(self):
"""Provides a formatted date string to compare scraped HTML tags."""
today = pendulum.today().format('%A %m.%d.%y')
tomorrow = pendulum.tomorrow().format('%A %m.%d.%y')
return {'today': today, 'tomorrow': tomorrow}
def scrape_programming(self, url):
"""Scrapes desired website for posted WODs based on dates.
:param url: str:
"""
headers = {'User-Agent': 'Mozilla/5.0'}
response = requests.get(url, headers=headers)
html = bs(response.text, "html.parser")
for tag in html.find_all('h1'):
self.raw_list.append(tag)
for dates in self.raw_list:
clean_dates = (bs(str(dates), "html.parser").text).strip()
self.parsed_list.append(clean_dates)
def wod_is_posted(self, url):
while not self.found_WOD:
self.scrape_programming(url)
date = self.get_current_dates()
if date['tomorrow'] in self.parsed_list:
self.config.read('config.ini')
body = f"Found tomorrow's ({date['tomorrow']}) WOD at {url}"
print(body)
for r, value in list(self.config.items('to')):
print(value)
twilio_api.send_message(self.config['from']['pa'], value, body)
self.found_WOD = True
self.check_date_change(date['today'])
else:
print(f"{pendulum.time.now(false)}: {pendulum.time.now(False)}: Tomorrow's WOD ({date['tomorrow']}) has not been posted yet.")
time.sleep(5)
if __name__ == "__main__":
test = CompTrain()
test.wod_is_posted(test.class_url)