-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposti_notifier.py
63 lines (51 loc) · 1.71 KB
/
posti_notifier.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
import urllib2
import smtplib
import hashlib
import time
import sys
from bs4 import BeautifulSoup
from ConfigParser import SafeConfigParser
if len(sys.argv) <= 1:
print "Usage: python posti_notifier.py <tracking code>"
sys.exit(0)
parser = SafeConfigParser()
parser.read("config.ini")
# Get tracking code from parameters
trackingCode = sys.argv[1]
timeToSleep = 300
# Open requested url
url = "http://www.posti.fi/itemtracking/posti/search_by_shipment_id?lang=fi&ShipmentId=" + trackingCode
data = urllib2.urlopen(url)
html = data.read()
soup = BeautifulSoup(html)
details_old = soup.find_all("div", {"id": "shipment-event-table-cell"})
# Main loop
while True:
data = urllib2.urlopen(url)
html = data.read()
soup = BeautifulSoup(html)
details = soup.find_all("div", {"id": "shipment-event-table-cell"})
if(details != details_old):
print "Found a change, mailing"
# Construct the message
status = ""
for event in details:
status = status + "\n" + event.get_text() + "\n --------- \n"
# Initialize SMTP
server = smtplib.SMTP(parser.get('email', 'server'))
username = parser.get('email', 'username')
password = parser.get('email', 'password')
server.ehlo()
server.starttls()
server.login(username, password)
recipient = parser.get('email', 'recipient')
sender = '[email protected]'
message = 'From: "posti-notifier" <%s> \nTo: %s\nSubject: New status for package %s\n\n %s' % (sender, recipient, trackingCode, status.encode('utf-8'))
# Send message
server.sendmail(sender,recipient,message);
# Disconnect
server.quit()
details_old = details
# Sleep and fade away
print "Sleeping for %d seconds" % (timeToSleep)
time.sleep(timeToSleep)