Prepare for timetable data

This commit is contained in:
Fred Boniface 2023-05-26 11:23:50 +01:00
parent dd15c3ba77
commit e142143fad
1 changed files with 68 additions and 0 deletions

68
src/timetable.py Normal file
View File

@ -0,0 +1,68 @@
# db-manager - Builds and manages an OwlBoard database instance - To be run on a
# cron schedule
# Copyright (C) 2023 Frederick Boniface
# 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
# https://git.fjla.uk/OwlBoard/db-manager/src/branch/main/LICENSE
#Imports
import os
import requests
import logger as log
import zlib
import json
import mongo
import time
# This module downloads a single TOCs Schedule data
TOC_Code = "EF" # Business code for GWR
fullDataUrl = f"https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_{TOC_Code}_TOC_FULL_DAILY&day=toc-full"
updateDataUrl = f"https://publicdatafeeds.networkrail.co.uk/ntrod/CifFileAuthenticate?type=CIF_{TOC_Code}_TOC_UPDATE_DAILY&day=toc-update-{day}"
CORPUS_USER = os.getenv('OWL_LDB_CORPUSUSER')
CORPUS_PASS = os.getenv('OWL_LDB_CORPUSPASS')
# Determine state of current Timetable Database
def isUpdateRequired():
timetableLength = mongo.getLength("timetable")
log.out(f"timetable.isUpdateRequired: timetable collection contains {timetableLength} documents")
timetableUpdateDate = mongo.metaUpdateTime("timetable")
if (not timetableLength or int(time.time()) > timetableUpdateDate + 172800):
log.out(f"timetable.isUpdateRequired: timetable collection requires rebuild")
return "full"
if (int(time.time()) > (timetableUpdateDate + 86400)):
log.out(f"timetable.isUpdateRequired: timetable collection requires update")
return "update"
return False
def getTimetable(full = False):
download_url = full_data_url if full else update_data_url
response = requests.get(downloadUrl, auth=(CORPUS_USER, CORPUS_PASS))
mongo.incrementCounter("schedule_api")
return response
def parseTimetable(data):
## Parse the update file
return
def runUpdate():
required = isUpdateRequired()
if (required == "full"):
log.out("timetable.runUpdate: Fetching full timetable data")
data = getTimetable(full = True)
elif (required == "update"):
log.out("timetable.runUpdate: Fetching todays timetable update")
data = getTimetable()
else:
log.out("timetable.runUpdate: timetable update is not needed")
return "done"
## Check how the downloaded data is displayed
## Check what happens if there is no update