diff --git a/.gitignore b/.gitignore index 3f0479e..d8e0c14 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ env_conf +include # ---> Python # Byte-compiled / optimized / DLL files diff --git a/requirements.txt b/requirements.txt index 13d45de..db182cc 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,7 @@ +certifi==2024.2.2 +charset-normalizer==3.3.2 +idna==3.6 lxml==5.1.0 python-docx-2023==0.2.17 +requests==2.31.0 +urllib3==2.2.1 diff --git a/src/0222 THO SC CNDR.docx b/src/file.docx similarity index 100% rename from src/0222 THO SC CNDR.docx rename to src/file.docx diff --git a/src/gitea_connector.py b/src/gitea_connector.py index e69de29..fc75f01 100644 --- a/src/gitea_connector.py +++ b/src/gitea_connector.py @@ -0,0 +1,2 @@ +import requests + diff --git a/src/local_mode.py b/src/local_mode.py index 328be48..518ac7c 100644 --- a/src/local_mode.py +++ b/src/local_mode.py @@ -1,9 +1,15 @@ -import parse_docx, pis_find +import parse_docx, pis_find, owlboard_connector -import os +import os, sys def start(): - print("Local mode activated") + print("Running OwlBoard Diagram Parser in local mode") + if not owlboard_connector.check_connection(): + print("Exiting") + sys.exit(1) + #if not gitea_connector.check_connection(): + # print("Exiting") + # sys.exit(1) working_directory = os.getcwd() print("Working directory: ", working_directory) diff --git a/src/owlboard_connector.py b/src/owlboard_connector.py index ae87934..2b21404 100644 --- a/src/owlboard_connector.py +++ b/src/owlboard_connector.py @@ -2,31 +2,43 @@ ### AUTHENTICATION MUST BE COMPLETED, REGISTERING FOR THE API IF NECCESSARY ### THIS NEGATES THE ABILITY TO USE LOCAL MODE - MAILBOX MODE ONLY AS -### MAILBOX ACCESS IS NEEDED FOR REGISTRATION +### MAILBOX ACCESS IS NEEDED FOR REGISTRATION... DON'T BE A FUCKING IDIOT... I CAN JUST PASS +### A UUID IN TO THE PROGRAM!! TWAT. -from imap_connector import IMAPConnector +import requests, os -imap_connector = IMAPConnector() +OB_BASE_URL = "https://owlboard.info/api/v2/pis/byCode/" +OB_TEST_URL = OB_BASE_URL + "5001" +UUID = os.environ.get('DGP_OB_UUID') +HEADERS = { + 'user-agent': 'owlboard-diagram-parser', + 'uuid': UUID +} -def check_registration(): - ## First check env var `DGP_OB_UUID` - ## Then run a test query, if not unauthorized return TRUE - ## else FALSE. - uuid = os.environ.get('DGP_OB_UUID') - if not uuid: +def check_connection(): + if not UUID: + print("'DGP_OB_UUID' must be set in the environment") return False + + res = requests.get(OB_TEST_URL, headers=HEADERS) + if res.status_code == 401: + print("Error - Unauthorised. The UUID is not valid. STATUS: ", res.status_code, "UUID: ", UUID) + return False + elif res.status_code != 200: + print("Error - Unable to reach OwlBoard. STATUS: ", res.status_code) + return False + return True - ### Now run test query and if return not 200 return false. - return - -def register(): - ## Send registration Req - ## Keep checking IMAP until reg email received - ## Submit Reg confirmation - ## Save UUID to env var `DGP_OB_UUID` - return - -## INIT -is_registered = check_registration() -if not is_registered: - register() \ No newline at end of file +def find_pis_code(code): + print("Searching for PIS Code: ", code) + url = OB_BASE_URL + code + res = requests.get(url, headers=HEADERS) + if res.status_code == 200: + json_response = res.json() + if json_response and isinstance(json_response, list): + return json_response + else: + return False + else: + print("Unable to reach OwlBoard. STATUS: ", res.status_code) + return True diff --git a/src/parse_docx.py b/src/parse_docx.py index fa021e7..cd14bf0 100644 --- a/src/parse_docx.py +++ b/src/parse_docx.py @@ -9,6 +9,13 @@ import re ### and find a service with valid stopping pattern, ### then the PIS codes can be generated for review. +### I think that I need to match each page. I need to search each page for the +### days of week the diagram is valid for, eg FSX, FO. Then I need to find the +### dates the diagram is valid for. Then use that to search for trains. + +### Alternatively, I could ensure that only daily diagrams are sent and the date +### is contained within the body/subject of the email. + PIS_PATTERN = re.compile(r'PIS code\s*:\s*(\d{4})') HEADCODE_PATTERN = re.compile(r'(\d{1}[A-Z]\d{2})') diff --git a/src/pis_find.py b/src/pis_find.py index f81c431..33e4512 100644 --- a/src/pis_find.py +++ b/src/pis_find.py @@ -1,3 +1,4 @@ +import owlboard_connector import requests def run(data_list): @@ -16,18 +17,15 @@ 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: pis_code = item.get('pis') if pis_code: - url = BASEURL + pis_code - response = requests.get(url) - if response.status_code == 200: - json_response = response.json() - if json_response and isinstance(json_response, list): - missing_data.append(item) - else: - print(f"Request failed for PIS {pis_code}. Status: {response.status_code}") \ No newline at end of file + 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