-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClassesFuns.py
54 lines (51 loc) · 2.08 KB
/
ClassesFuns.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
###############################################################################
# FUNCTIONS AND CLASSES
import json, requests
from collections import OrderedDict
# function to send email to gmail
def send_email(fromaddr, pwd, toaddr, Subject, body):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
# sender and recipeints
fromaddr = fromaddr
toaddr = toaddr
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = ', '.join(toaddr)
msg['Subject'] = Subject
# email text
body = body
msg.attach(MIMEText(body, 'plain'))
# knit the email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, pwd)
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
# Class to:
# check if the get is successfull or not
# loads the json
class checkGet_and_loads:
# check if the get is successfull or not
def get(self, url, file):
# download the file
self.r = requests.get(url)
# check if file was downloaded with no errors
if self.r.status_code != 200:
emailBody = 'Sending email. Error with downloading the ' + file + ' file.'
send_email( fromaddr = emailFrom, pwd = password, toaddr = emailTo, Subject = emailSubject, body = emailBody )
print( 'Error: Unexpected response {}'.format(self.r) )
else: pass
# print( 'Not sending email. No errors found when downloading the ' + file + ' file.' )
##########################################
# loads the json file
def loads(self):# , url, file):
# download the file
# self.r = requests.get(url)
# loads the json file
to_loads = json.loads( self.r.text, object_pairs_hook = OrderedDict )
return( to_loads )
# /FUNCTIONS AND CLASSES
###############################################################################