Skip to content

Instantly share code, notes, and snippets.

@adampetrovic
Last active September 14, 2016 02:13
Show Gist options
  • Save adampetrovic/77d44b575738078046f778999e8a9c53 to your computer and use it in GitHub Desktop.
Save adampetrovic/77d44b575738078046f778999e8a9c53 to your computer and use it in GitHub Desktop.
StarTrack Email Updates
  • Python 3
  • Gmail by default
  • Install requests (pip install requests)
  • Create the directory '.search_cache' where you are running the script from
  • Update EMAIL_ADDRESS and EMAIL_PASSWORD
  • Usage: python startrack_check.py
  • reference can be your 'TL' tracking code, or SBS from Telstra
import sys
import os
import time
import smtplib
from email.mime.text import MIMEText
import requests
STARTRACK_BASE_URL = 'http://sttrackandtrace.startrack.com.au'
DEFAULT_SEARCH_CACHE_LOCATION = '.search_cache'
SMTP_HOST = 'smtp.gmail.com'
SMTP_PORT = 587
EMAIL_ADDRESS = 'your@gmail.com'
EMAIL_PASSWORD = '' # Generated from 'App Passwords' in settings
def check_updated_status(guid, events):
has_updated = False
path = os.path.join(DEFAULT_SEARCH_CACHE_LOCATION, guid)
if not os.path.isfile(path):
with open(path, 'w') as f:
for event in events:
f.write("{}\n".format(event.get('EventGuid')))
else:
seen_guids = [line.strip() for line in open(path)]
for event in events:
if event['EventGuid'] not in seen_guids:
has_updated = True
with open(path, 'a') as f:
f.write("{}\n".format(event['EventGuid']))
return has_updated
def send_email_update(reference):
text = MIMEText("""
StarTrack consignment {reference} has a new update.
Visit http://sttrackandtrace.startrack.com.au/{reference} to see the updated info.
""".format(reference=reference))
text['Subject'] = 'StarTrack delivery tracking update!'
text['From'] = EMAIL_ADDRESS
text['To'] = EMAIL_ADDRESS
s = smtplib.SMTP(SMTP_HOST, SMTP_PORT)
s.ehlo()
s.starttls()
s.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
s.sendmail(EMAIL_ADDRESS, [EMAIL_ADDRESS], text.as_string())
s.close()
def get_latest_guid(reference):
resp = requests.get(
url="{}/Consignment/GetConsignmentsBySearchCriteriaWithOptions/{}".format(
STARTRACK_BASE_URL,
reference
),
params={
'getArticleEventDetails': False,
'getArticleEvents': False,
'getConsignmentDetails': False,
'getCustomerReferences': False,
'getPodData': False,
}
)
resp.raise_for_status()
if len(resp.json()) == 0:
print("ERROR: No consignments found for the given reference: '{}'".format(reference))
sys.exit(1)
return resp.json()[0]['Guid']
def get_consignment_events(guid):
resp = requests.get(
url="{}/Consignment/GetConsignmentEventsByConsignmentGuid/{}".format(
STARTRACK_BASE_URL,
guid
),
)
resp.raise_for_status()
if len(resp.json()) == 0:
print("ERROR: No consignment events found for the given GUID: '{}'".format(guid))
sys.exit(1)
return resp.json()
def main(reference):
guid = get_latest_guid(reference)
events = get_consignment_events(guid)
if check_updated_status(guid, events):
print("INFO: New Events Detected. Sending Email Confirmation")
send_email_update(reference)
else:
print("INFO: No new events. Sleeping")
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: ./{} <reference>".format(sys.argv[0]))
sys.exit(1)
while True:
main(sys.argv[1])
time.sleep(60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment