From 4d3f7ce342c2562cb1dd73c9081581c18a73e524 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Wed, 21 Feb 2024 13:44:27 +0000 Subject: [PATCH] Even more meating --- src/{file.docx => 20240221_file.docx} | Bin src/local_mode.py | 6 ++- src/owlboard_connector.py | 52 ++++++++++++++++++++++++-- src/parse_docx.py | 2 + src/pis_find.py | 14 +++---- 5 files changed, 63 insertions(+), 11 deletions(-) rename src/{file.docx => 20240221_file.docx} (100%) diff --git a/src/file.docx b/src/20240221_file.docx similarity index 100% rename from src/file.docx rename to src/20240221_file.docx diff --git a/src/local_mode.py b/src/local_mode.py index 518ac7c..674a53a 100644 --- a/src/local_mode.py +++ b/src/local_mode.py @@ -29,7 +29,11 @@ def start(): print("No DOCX files found") print(f"Found {len(results)} PIS Codes in documents") - pis_find.run(results) + missing_pis = pis_find.run(results) + for code in missing_pis: + services = owlboard_connector.get_services(code['headcode'], code['date']) + print(services) + if __name__ == "__main__": diff --git a/src/owlboard_connector.py b/src/owlboard_connector.py index 2b21404..1342fb2 100644 --- a/src/owlboard_connector.py +++ b/src/owlboard_connector.py @@ -7,8 +7,11 @@ import requests, os -OB_BASE_URL = "https://owlboard.info/api/v2/pis/byCode/" -OB_TEST_URL = OB_BASE_URL + "5001" +#OB_PIS_BASE_URL = "https://owlboard.info/api/v2/pis/byCode/" +#OB_TRN_BASE_URL = "https://owlboard.info/api/v2/timetable/train/" +OB_PIS_BASE_URL = "http://localhost:8460/api/v2/pis/byCode/" +OB_TRN_BASE_URL = "http://localhost:8460/api/v2/timetable/train/" +OB_TEST_URL = OB_PIS_BASE_URL + "5001" UUID = os.environ.get('DGP_OB_UUID') HEADERS = { 'user-agent': 'owlboard-diagram-parser', @@ -31,7 +34,7 @@ def check_connection(): def find_pis_code(code): print("Searching for PIS Code: ", code) - url = OB_BASE_URL + code + url = OB_PIS_BASE_URL + code res = requests.get(url, headers=HEADERS) if res.status_code == 200: json_response = res.json() @@ -42,3 +45,46 @@ def find_pis_code(code): else: print("Unable to reach OwlBoard. STATUS: ", res.status_code) return True + +def get_services(headcode, date): + print("Finding GWR service: ", headcode, ", ", date) + results = [] + url = OB_TRN_BASE_URL + f"now/headcode/{headcode.lower()}" + print(url) + res = requests.get(url, headers=HEADERS) + if res.status_code == 200: + json_res = res.json() + for item in json_res: + if item['operator'] == 'GW': + results.append(item) + print(f"Found {len(results)} valid GWR Service") + 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 and isinstance(dict, json_res): + svc_detail = { + 'stops': json_res['stops'], + 'existing_pis': json_res['pis'], + 'vstp': json_res['vstp'] + } + + return organise_svc(svc_detail) + +def organise_svc(input): + stop_tiplocs = [] + vstp = input['vstp'] + + for stop in input['stops']: + if stop['isPublic']: + stop_tiplocs.append(stop['tiploc']) + + if input['pis']['skipCount'] == 0: + existingPis = True + + return {'stops': stop_tiplocs, 'vstp': vstp, 'existingPis': existingPis} \ No newline at end of file diff --git a/src/parse_docx.py b/src/parse_docx.py index cd14bf0..fc7a313 100644 --- a/src/parse_docx.py +++ b/src/parse_docx.py @@ -1,5 +1,6 @@ ### This uses the 'python-docx-2023' module from docx import Document +from datetime import datetime import re ### This can parse each table. What needs to happen next @@ -37,6 +38,7 @@ def extract_tables(file_path): pis_and_headcode = match_pis_and_headcode(data) if pis_and_headcode: pis_and_headcode['source_file'] = file_path + pis_and_headcode['date'] = datetime.strptime(file_path.split('_')[0], "%Y%m%d") pis_info.append(pis_and_headcode) return(pis_info) diff --git a/src/pis_find.py b/src/pis_find.py index 33e4512..8b1c621 100644 --- a/src/pis_find.py +++ b/src/pis_find.py @@ -1,13 +1,13 @@ import owlboard_connector -import requests +import requests, sys def run(data_list): deduplicated_data = dedup(data_list) print(f"Removed {len(data_list) - len(deduplicated_data)} duplicate codes") print(f"Searching for {len(deduplicated_data)} PIS codes") missing_data = find_missing(deduplicated_data) - print(f"{missing_data} missing PIS codes in OwlBoard data") - + print(f"{len(missing_data)} missing PIS codes in OwlBoard data") + return missing_data def dedup(data_list): unique_dicts = {d['pis']: d for d in data_list}.values() @@ -17,8 +17,8 @@ def dedup(data_list): ## AUTH REQUIRED!!! Move to owlboard_connector.py def find_missing(data_list): - #BASEURL = 'http://localhost:8460/api/v2/pis/byCode/' - BASEURL = 'https://owlboard.info/api/v2/pis/byCode/' + BASEURL = 'http://localhost:8460/api/v2/pis/byCode/' + #BASEURL = 'https://owlboard.info/api/v2/pis/byCode/' missing_data = [] for item in data_list: @@ -27,5 +27,5 @@ def find_missing(data_list): pis_code_res = owlboard_connector.find_pis_code(pis_code) if not pis_code_res: print("PIS Code ", pis_code, " not found in existing data") - missing_data.append(pis_code) - print(missing_data) \ No newline at end of file + missing_data.append(item) + return missing_data \ No newline at end of file