-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
60 lines (53 loc) · 1.88 KB
/
monitor.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
def send_email (message, subject, recipients):
import smtplib
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
fromaddr = 'email@yandex.ru'
server = SMTP_SSL('smtp.yandex.ru:465')
msg = MIMEText(message)
msg['Subject']=subject
msg['To'] = ",".join(recipients)
msg['From'] = "email@yandex.ru"
server.ehlo()
server.login('email@yandex.ru', 'superpassword')
server.sendmail(fromaddr, recipients, msg.as_string())
server.quit()
if __name__ == '__main__':
from urllib2 import urlopen, Request
from bs4 import BeautifulSoup
import pickle
import time
import os
from random import random
time.sleep(37*random())
req = Request("http://www.allurebox.ru/subscriptions", headers={ 'User-Agent': 'Mozilla/5.0' })
f=urlopen(req)
page=BeautifulSoup(f)
alldivs = page.findAll('div')
targetdivs=[]
for div in alldivs:
try:
if (div["class"]==["twelve","columns","alpha","omega","border", "box"]):
targetdivs.append(div)
except KeyError:
continue
currentLen = len(targetdivs)
print "CurrentLen = ", currentLen
f = file("allure_status", "r")
oldLen = pickle.load(f)
f.close()
print "oldLen = ", oldLen
if oldLen != currentLen:
print "NEW VALUE, composing email!!!"
message = """Attention \n \n New item discovered on Allure[Box] website
\n Old Number of Boxed: %i, NEW Number of Boxes: %i
\n Please visit http://www.allurebox.ru/subscriptions
"""%(oldLen, currentLen )
subject = "Allure[Box] monitor: new item"
recipients = ['apetrin@mail.ru', 'marie.tsh@gmail.com']
print "Sending emails!"
send_email (message, subject, recipients)
print "Email sent successfully!"
f = file("allure_status", "w")
oldLen = pickle.dump(currentLen,f)
f.close()