From d1728770c31ae33c82eaf5599f7454463d7e6d59 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 21 Feb 2024 22:49:40 +0000 Subject: [PATCH] Kind of there, much tidying to do! --- src/.~lock.20240221_file.docx# | 1 + src/formatter.py | 29 ++++++++++++++++--------- src/gitea_connector.py | 19 ++++++++++++++++- src/local_mode.py | 4 +++- src/owlboard_connector.py | 39 ++++++++++++++++++++-------------- 5 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 src/.~lock.20240221_file.docx# diff --git a/src/.~lock.20240221_file.docx# b/src/.~lock.20240221_file.docx# new file mode 100644 index 0000000..53c0680 --- /dev/null +++ b/src/.~lock.20240221_file.docx# @@ -0,0 +1 @@ +,fred.boniface,bedroom.ws.fjla.net,21.02.2024 22:41,file:///home/fred.boniface/.config/libreoffice/4; \ No newline at end of file diff --git a/src/formatter.py b/src/formatter.py index a2f176d..72ad722 100644 --- a/src/formatter.py +++ b/src/formatter.py @@ -1,4 +1,5 @@ import owlboard_connector +import sys def humanYaml(pis_list): additional_pis = '' @@ -7,17 +8,25 @@ def humanYaml(pis_list): if len(pis['services']) == 1: print(f"Only one valid service for {pis['pis']}") crs = [] - for stop in pis['services'][0]['stops']: - crs.append(owlboard_connector.convert_tiploc_to_crs(stop)) - additional_pis += f' - code: "{pis["pis"]}"\n' - additional_pis += f' stops: [{",".join(crs)}]\n' + print(pis) + try: + for stop in pis['services'][0]['stops']: + crs.append(owlboard_connector.convert_tiploc_to_crs(stop)) + additional_pis += f' - code: "{pis["pis"]}"\n' + additional_pis += f' stops: [{",".join(crs)}]\n' + except Exception as err: + print(err) elif len(pis['services']) > 1: print(f"More than one possible service for {pis['pis']}") - manual_review += f' - code: "{pis["pis"]}"\n' - for service in pis["services"]: - crs = [] - for stop in service['stops']: - crs.append(owlboard_connector.convert_tiploc_to_crs(stop)) - manual_review += f'OR stops:[{",".join(crs)}]\n' + print(pis) + try: + manual_review += f' - code: "{pis["pis"]}"\n' + for service in pis["services"]: + crs = [] + for stop in service['stops']: + crs.append(owlboard_connector.convert_tiploc_to_crs(stop)) + manual_review += f'OR stops:[{",".join(crs)}]\n' + except Exception as err: + print(err) return additional_pis + manual_review \ No newline at end of file diff --git a/src/gitea_connector.py b/src/gitea_connector.py index fc75f01..6b529e8 100644 --- a/src/gitea_connector.py +++ b/src/gitea_connector.py @@ -1,2 +1,19 @@ -import requests +import requests, os +from datetime import datetime +BASE_URL = "https://git.fjla.uk/" +USER = 'owlbot' +TOKEN = os.environ.get('DGP_GITEA_TOK') +HEADERS = { + 'Content-Type': 'application/json', + 'accept': 'application/json', + } + +''' +I need a way here to get the original file from the 'main' branch and +append the generated PIS codes. Then push to a new generated branch. + +Then a pull request should be created but can probably be done with actions. +In reality this program should just take in DOCX files and spit out formatted +PIS data to the repo, everything else can be handled at the repo level?? +''' \ No newline at end of file diff --git a/src/local_mode.py b/src/local_mode.py index a6771bc..655d1da 100644 --- a/src/local_mode.py +++ b/src/local_mode.py @@ -1,4 +1,4 @@ -import parse_docx, pis_find, owlboard_connector, formatter +import parse_docx, pis_find, owlboard_connector, formatter, gitea_connector import os, sys @@ -59,5 +59,7 @@ def start(): + + if __name__ == "__main__": start() \ No newline at end of file diff --git a/src/owlboard_connector.py b/src/owlboard_connector.py index 334a784..4e04b50 100644 --- a/src/owlboard_connector.py +++ b/src/owlboard_connector.py @@ -49,20 +49,27 @@ def get_services(headcode, date): return results def get_service_detail(trainUid, date): - print("Getting GWR service details: ", trainUid, ", ", date) - url = OB_TRN_BASE_URL + f"{date.isoformat()}/byTrainUid/{trainUid}" - print(url) - res = requests.get(url, headers=HEADERS) - if res.status_code == 200: - json_res = res.json() - if json_res: - svc_detail = { - 'stops': json_res['stops'], - 'existing_pis': json_res.get('pis', None), - 'vstp': json_res.get('vstp', False) - } - - return organise_svc(svc_detail) + try: + print("Getting GWR service details: ", trainUid, ", ", date) + url = OB_TRN_BASE_URL + f"{date.isoformat()}/byTrainUid/{trainUid}" + print(url) + res = requests.get(url, headers=HEADERS) + if res.status_code == 200: + json_res = res.json() + if json_res: + svc_detail = { + 'stops': json_res['stops'], + 'vstp': json_res.get('vstp', False) + } + organised = organise_svc(svc_detail) + print(organised) + return organised + else: + print("Service Not Found") + sys.exit() + except Exception as e: + print(e) + sys.exit() def organise_svc(input): stop_tiplocs = [] @@ -76,7 +83,7 @@ def organise_svc(input): if 'pis' in input and input['pis'].get('skipCount', 0) == 0: existingPis = True - return {'stops': stop_tiplocs, 'existingPis': existingPis, 'vstp': vstp} + return {'stops': stop_tiplocs, 'vstp': vstp} def convert_tiploc_to_crs(tiploc): res = requests.get(OB_TIP_BASE_URL + tiploc.upper(), headers=HEADERS) @@ -85,4 +92,4 @@ def convert_tiploc_to_crs(tiploc): if json_res: crs = json_res[0]['3ALPHA'] print(f"TIPLOC: {tiploc}, CRS: {crs}") - return crs + return crs.lower()