This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RMTracker.py
66 lines (54 loc) · 2.16 KB
/
RMTracker.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
#!/usr/bin/env python
# RM Tracker - RoyalMail tracking with PushBullet integration
# Copyright (C) 2016 Ritvars Timermanis (RichusX)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import dryscrape
import sys
import time
from pushbullet.pushbullet import PushBullet
if 'linux' in sys.platform:
dryscrape.start_xvfb()
# USER VARIABLES START
trackingNumber = 'YOUR_TRACKING_NUMBER'
apiKey = 'YOUR_API_KEY'
deviceId = 'YOUR_PUSHBULLET_DEVICE_ID'
refreshRate = 30 # how often to check the status in minutes
# USER VARIABLES END
p = PushBullet(apiKey)
prevStatus = ''
# set up a web scraping session
sess = dryscrape.Session(base_url = 'https://www.royalmail.com')
# we don't need images
sess.set_attribute('auto_load_images', False)
while True:
# visit the tracking page and lookup the tracking number
sess.visit('/track-your-item')
q = sess.at_xpath('//*[@name="tracking_number"]')
q.set(trackingNumber)
q.form().submit()
# extract the tracking status
try:
x = sess.at_xpath('//*[@class="status result-row padding20lr first"]')
status = x.text()
except AttributeError:
x = sess.at_xpath('//*[@class="status result-row padding20lr first last"]')
status = x.text()
if (status != prevStatus):
# push the status to PushBullet
# if you would like to push to more than one device then just copy the line below and change the device ID
p.pushNote(deviceId, 'RoyalMail Tracking', status)
prevStatus = status
# wait for 'refreshRate' minutes before checking again
time.sleep(refreshRate * 60)