-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_crmit_dump.py
executable file
·76 lines (65 loc) · 2.35 KB
/
make_crmit_dump.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
# 0 * * * * python3 /home/alex/crmit_dump/make_crmit_dump.py
import urllib.request
import datetime
import os
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
base_dir = '/home/alex/crmit_dump'
dumps_dir = '{}/dumps'.format(base_dir)
os.makedirs(dumps_dir, exist_ok=True)
now_string = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
filename = '{}/{}.json'.format(dumps_dir, now_string)
write_file = open(filename, 'w')
credentials_file = open('{}/credentials.txt'.format(base_dir), 'r')
lines = credentials_file.read().splitlines()
access_token = lines[1]
mail_user = lines[3]
mail_password = lines[5]
credentials_file.close()
def send_email(subject, text):
try:
mail_receivers = ['qwert2603@mail.ru']
msg = MIMEMultipart()
msg['From'] = mail_user
msg['To'] = COMMASPACE.join(mail_receivers)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
with open(filename, "rb") as fil:
part = MIMEApplication(
fil.read(),
Name=basename(filename)
)
part['Content-Disposition'] = 'attachment; filename="{}"'.format(basename(filename))
msg.attach(part)
server = smtplib.SMTP_SSL('smtp.mail.ru', 465)
server.ehlo()
server.login(mail_user, mail_password)
server.sendmail(mail_user, mail_receivers, msg.as_string())
server.close()
return True
except Exception as e:
write_file.write(str(e))
print(e)
email_error_file = open(filename + '_email_error', 'w')
email_error_file.write(str(e))
email_error_file.close()
return False
try:
url = 'http://crm.cmit22.ru/api/v1.1.0/dump?access_token={}'.format(access_token)
response = urllib.request.urlopen(url)
response_string = response.read().decode("utf-8")
write_file.write(response_string)
b = send_email('crmit dump success', 'dump of {}'.format(now_string))
if b: print('ok')
except Exception as e:
write_file.write(str(e))
write_file.close()
send_email('crmit dump error', 'failed dump of {}'.format(now_string))
print('not ok')
print(e)
write_file.close()