-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_progs.py
109 lines (96 loc) · 3.95 KB
/
find_progs.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
import mariadb
import sys
import time
from datetime import date, datetime, timedelta, timezone
from urllib.request import urlopen
import cfg
import dateutil.parser
import pytz
# Config
tothLength = 92
defaultCart = "200043"
scheduleApiUrl = "https://upload.cambridge105.co.uk/ScheduleApi"
dateToday = date.today()
dateTomorrow = dateToday + timedelta(days=1)
dateStr = dateTomorrow.strftime("%Y-%m-%d")
midnightTomorrow = datetime.combine(dateTomorrow, datetime.min.time()).astimezone(pytz.utc)
unix_ms_midnight = (midnightTomorrow.timestamp() * 1000)
# Connect to MariaDB Platform
try:
conn = mariadb.connect(
user=cfg.dbUser,
password=cfg.dbPassword,
host=cfg.dbHost,
port=cfg.dbPort,
database=cfg.dbDatabase
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
# Get Cursor
cur = conn.cursor()
# ========== Functions ==================
# Converts schedule API to a dict of the next three schedule items indexed by time since midnight:
# In: {"PID":"brian-oreilly","Name":"Brian O'Reilly","StartTime":"2021-12-22 06:00:00","EndTime":"2021-12-22 07:00:00","Type":null},{"PID":"breakfast","Name":"Cambridge Breakfast with Julian & Lucy","StartTime":"2021-12-22 07:00:00","EndTime":"2021-12-22 09:30:00","Type":null}
# Out: {21599908: 'brian-oreilly|breakfast|alex-elbro', 25199908: 'breakfast|alex-elbro|neil-whiteside'...
def jsonToSchedule(jsonObj):
numScheduleItems = len(jsonObj)
print("ScheduleItems=" + str(numScheduleItems))
scheduleObj = {}
for i in range(numScheduleItems):
# only get schedule items tomorrow
if (jsonObj[i]['StartTime'][0:10] == dateStr):
scheduleTripleStr = str(jsonObj[i]['PID'] or '')
if (i+1 < numScheduleItems):
scheduleTripleStr += "|" + str(jsonObj[(i+1)]['PID'] or '')
if (i+2) < numScheduleItems:
scheduleTripleStr += "|" + str(jsonObj[(i+2)]['PID'] or '')
ms_since_midnight = convertDateTimeToMsSinceMidnight(jsonObj[i]['StartTime'])
scheduleObj[ms_since_midnight] = scheduleTripleStr
return scheduleObj
def convertDateTimeToMsSinceMidnight(dt_str):
tz_London = pytz.timezone('Europe/London')
dt = dateutil.parser.isoparse(dt_str)
print (dt)
dt_3am = datetime(dt.year, dt.month, dt.day, 3, 0, 0) # We use 3am because of clocks going back
dt_3am_aware = tz_London.localize(dt_3am)
dt_delta = dt - dt_3am_aware
dt_since_midnight = dt_delta + timedelta(hours=3)
msecs_since_midnight = dt_since_midnight.total_seconds() * 1000
return int(msecs_since_midnight - (tothLength*1000))
def getCartNumberForPromo(title):
sqlq = "SELECT NUMBER FROM CART WHERE GROUP_NAME='TOTHPROMO' AND TITLE LIKE '" + title + "' LIMIT 1;"
cur.execute(sqlq)
row = cur.fetchone()
if row is None:
return False
else:
print ("Cart found for " + title + ": " + str(row[0]))
return row[0]
def overwriteToth(cartNumber,progTime):
sqlQuery = "UPDATE LOG_LINES SET CART_NUMBER='" + str(cartNumber) + "' WHERE LOG_NAME='" + dateStr + "-TOTH' AND START_TIME='" + str(progTime) + "' AND CART_NUMBER='" + defaultCart + "' LIMIT 1;"
affected_rows = cur.execute(sqlQuery)
if affected_rows is None:
return False
elif affected_rows > 0:
return True
return False
# Fetch schedule as JSON from API
scheduleResponse = urlopen(scheduleApiUrl)
jsonObj = json.loads(scheduleResponse.read())
scheduleObj = jsonToSchedule(jsonObj)
# Iterate the schedule object checking for TOTHPROMO carts matching the group of three items
for progTime in scheduleObj:
nextThree = scheduleObj[progTime]
nextCart = getCartNumberForPromo("[Next]"+nextThree)
notNextCart = getCartNumberForPromo(nextThree)
if nextCart != False:
overwriteToth(nextCart, progTime)
if notNextCart != False:
overwriteToth (notNextCart, progTime)
overwriteToth (notNextCart, (progTime - 3600000))
overwriteToth (notNextCart, (progTime - 7200000))
overwriteToth (notNextCart, (progTime - 10800000))
cur.close()
conn.close()