From e91eb187e370f781c54eda13f610538eb80c085d Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Tue, 6 Jun 2023 13:11:54 +0100 Subject: [PATCH] Improve code modularity --- src/corpus.py | 4 +--- src/helpers.py | 21 +++++++++++++++++++-- src/timetable.py | 6 ++---- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/corpus.py b/src/corpus.py index b8a0aeb..94dfca1 100644 --- a/src/corpus.py +++ b/src/corpus.py @@ -7,8 +7,6 @@ import json import datetime import mongo, helpers -CORPUS_URL = "https://publicdatafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS" - #Fetch Configuration CORPUS_USER = os.getenv('OWL_LDB_CORPUSUSER') CORPUS_PASS = os.getenv('OWL_LDB_CORPUSPASS') @@ -44,7 +42,7 @@ def isUpdateRequired(): def fetch(): log.out("corpus.fetch: Fetching CORPUS Data from Network Rail", "INFO") - response = requests.get(CORPUS_URL, auth=(CORPUS_USER, CORPUS_PASS)) + response = requests.get(helpers.corpus_data_url, auth=(CORPUS_USER, CORPUS_PASS)) mongo.incrementCounter("corpus_api") log.out("corpus.fetch: Decompressing & parsing CORPUS data", "INFO") parsed = json.loads(zlib.decompress(response.content, 16+zlib.MAX_WBITS).decode()) diff --git a/src/helpers.py b/src/helpers.py index f5f568e..a97337a 100644 --- a/src/helpers.py +++ b/src/helpers.py @@ -1,7 +1,24 @@ -import time +import time, os +## Environment Options +env_corpus_user = os.getenv('OWL_LDB_CORPUSUSER') +env_corpus_pass = os.getenv('OWL_LDB_CORPUSPASS') + +## Timetable Data Options +required_toc_codes = ["EF"] + +## PIS Data Options +pis_file_paths = [ + "/app/data/pis/gwr.yaml" +] + +## Upstream Data URLs +corpus_data_url = "https://publicdatafeeds.networkrail.co.uk/ntrod/SupportingFileAuthenticate?type=CORPUS" + +## Time Constants one_day_in_seconds = 84600 -two_weeks_in_seconds = 1209600 +two_day_in_seconds = one_day_in_seconds * 2 +two_weeks_in_seconds = one_day_in_seconds * 14 def getAgeInSeconds(updateTimeInSeconds :int): now = int(time.time()) diff --git a/src/timetable.py b/src/timetable.py index 98127f3..1b5201b 100644 --- a/src/timetable.py +++ b/src/timetable.py @@ -32,8 +32,6 @@ now = datetime.now() yesterday = now - timedelta(days=1) yesterdayDay = yesterday.strftime("%a").lower() todayDay = now.strftime("%a").lower() -oneDayinSecs = 86400 -twoDayinSecs = 86400 * 2 isAfter0800 = (int(now.strftime("%H")) >= 8) filePath = "cif_data" TOC_Code = "EF" # Business code for GWR @@ -52,10 +50,10 @@ def isUpdateRequired(): timetableDataAge = helpers.getAgeInSeconds(timetableUpdateTime) readable_age = str(timedelta(seconds=timetableDataAge)) log.out(f"timetable.isUpdateRequired: Timetable data age: {readable_age}", "INFO") - if (timetableDataAge >= twoDayinSecs and isAfter0800) or REBUILD: + if (timetableDataAge >= helpers.two_day_in_seconds and isAfter0800) or REBUILD: log.out(f"timetable.isUpdateRequired: timetable collection requires rebuild", "INFO") return "full" - if (timetableDataAge >= oneDayinSecs and isAfter0800): + if (timetableDataAge >= helpers.one_day_in_seconds and isAfter0800): log.out(f"timetable.isUpdateRequired: timetable collection requires update", "INFO") return "update" return False