-
Notifications
You must be signed in to change notification settings - Fork 0
/
getmyworkout.py
executable file
·135 lines (114 loc) · 4.9 KB
/
getmyworkout.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import bs4
import requests
import smtplib
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from datetime import datetime
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from contextlib import redirect_stdout
# CHANGE if needed
from_address = "[email protected]"
to_address = "[email protected]"
local_path = "file:///Users/your/local/path/here"
actual_path = "/Users/local/path/here"
# Should not need to change
url = "https://www.thequadguy.com/dailypump"
filename = "workout"
delimiter = "_"
now = str(datetime.today().strftime("%m-%d-%Y"))
extension = ".html"
log_file = now + ".txt"
with open(log_file, 'w') as f:
try:
s = requests.Session()
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_0) '
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
'Referer': 'https://www.sentrylogin.com/sentry/scripts/CSS_flat_Kady.css',
'Origin': 'https://www.thequadguy.com',
}
params = (
('Sentry_ID', '1234'),
('e', '[email protected]'),
('p', 'test'),
('psist', '0'),
('ip', '127.0.0.1'),
('ms', '1234'),
)
url = "https://www.thequadguy.com/dailypump"
s.post('https://www.sentrylogin.com/sentry/NoSockets/loginActionAJAX.asp',
headers=headers, params=params)
get_page = s.get(url)
get_page.raise_for_status() # if error it will stop the program
print("Checked: {0}".format(url), file=f)
# Parse text
get_html = bs4.BeautifulSoup(get_page.text, 'html.parser')
get_class = bs4.BeautifulSoup.find(get_html, id="block-yui_3_17_2_1_1554898039337_10815")
get_header = bs4.BeautifulSoup.find(get_class, "h1").text.strip()
print("Workout: {0}".format(get_header), file=f)
get_header = get_header.replace("/", "-")
# Initialize file names
file_extension = filename + delimiter + now + delimiter + get_header + extension
file_noextension = filename + delimiter + now + delimiter + get_header
workout_extension = file_noextension + ".png"
attach = [actual_path + workout_extension, actual_path + log_file]
# Create html file if it doesn't exist for the day
try:
init_file = open(file_extension, "r")
init_file.close()
except IOError:
init_file = open(file_extension, "a+")
init_file.close()
# Write data obtained from html id
file = open(file_extension, "w")
file.write(str(get_class))
file.close()
# Initialize first Chrome browser to get window size info
driver = webdriver.Chrome()
driver.maximize_window()
driver.get(local_path + file_extension)
height = driver.execute_script("return Math.max( document.body.scrollHeight, document.body.offsetHeight, "
"document.documentElement.clientHeight, "
"document.documentElement.scrollHeight, "
"document.documentElement.offsetHeight )")
driver.quit()
print("\nGot window height: {0}".format(height), file=f)
# Initialize second Chrome browser to take screenshot
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument(f"--window-size=900,{height}")
chrome_options.add_argument("--hide-scrollbars")
driver = webdriver.Chrome(options=chrome_options)
driver.get(local_path + file_extension)
driver.save_screenshot(file_noextension + ".png")
driver.quit()
print("Got screenshot: {0}".format(file_noextension + ".png"), file=f)
print("Sent email to: {0}".format(to_address), file=f)
except Exception as e:
print("Error:", e, file=f)
def send_email():
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(from_address, "gmail_password_here")
# s.set_debuglevel(1)
msg = MIMEMultipart()
sender = from_address
msg['Subject'] = "Workout - {0} - {1}".format(now, get_header)
msg['To'] = to_address
for each_file_path in attach:
try:
file_name = each_file_path.split("/")[-1]
part = MIMEBase('application', "octet-stream")
part.set_payload(open(each_file_path, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment', filename=file_name)
msg.attach(part)
except Exception as file_exception:
print("Could not attach file:", file_exception)
msg.attach(MIMEText("", 'html'))
s.sendmail(sender, to_address, msg.as_string())
s.quit()
send_email()