From 58fdf9bf6f23eb6095c17ffb0131e9f899fecdfb Mon Sep 17 00:00:00 2001 From: Robert Bradley Date: Wed, 1 Nov 2023 07:20:13 +0000 Subject: [PATCH] fix: #378 update East Northamptionshire to North Northamptonshire --- .../features/validate_council_outputs.feature | 2 +- uk_bin_collection/tests/input.json | 6 +- .../councils/EastNorthamptonshireCouncil.py | 118 ------------------ .../councils/NorthNorthamptonshireCouncil.py | 68 ++++++++++ 4 files changed, 72 insertions(+), 122 deletions(-) delete mode 100644 uk_bin_collection/uk_bin_collection/councils/EastNorthamptonshireCouncil.py create mode 100644 uk_bin_collection/uk_bin_collection/councils/NorthNorthamptonshireCouncil.py diff --git a/uk_bin_collection/tests/features/validate_council_outputs.feature b/uk_bin_collection/tests/features/validate_council_outputs.feature index 2d427cb871..5ff5dd128b 100644 --- a/uk_bin_collection/tests/features/validate_council_outputs.feature +++ b/uk_bin_collection/tests/features/validate_council_outputs.feature @@ -37,7 +37,6 @@ Feature: Test each council output matches expected results in /outputs | EastDevonDC | | EastleighBoroughCouncil | | EastLindseyDistrictCouncil | - | EastNorthamptonshireCouncil | | EastRidingCouncil | | EastSuffolkCouncil | | ErewashBoroughCouncil | @@ -67,6 +66,7 @@ Feature: Test each council output matches expected results in /outputs | NorthLanarkshireCouncil | | NorthLincolnshireCouncil | | NorthNorfolkDistrictCouncil | + | NorthNorthamptonshireCouncil | | NorthSomersetCouncil | | NorthTynesideCouncil | | NorthumberlandCouncil | diff --git a/uk_bin_collection/tests/input.json b/uk_bin_collection/tests/input.json index 6fa8f1f9ba..bf6983330c 100644 --- a/uk_bin_collection/tests/input.json +++ b/uk_bin_collection/tests/input.json @@ -186,11 +186,11 @@ "wiki_name": "East Lindsey District Council", "wiki_note": "Pass the house name/number in the house number parameter, wrapped in double quotes" }, - "EastNorthamptonshireCouncil": { + "NorthNorthamptonshireCouncil": { "SKIP_GET_URL": "SKIP_GET_URL", "uprn": "100031021317", - "url": "https://kbccollectiveapi-coll-api.e4ff.pro-eu-west-1.openshiftapps.com/wc-info/", - "wiki_name": "East Northamptonshire Council" + "url": "https://cms.northnorthants.gov.uk/bin-collection-search/calendarevents/100031021318/2023-10-17/2023-10-01", + "wiki_name": "North Northamptonshire Council" }, "EastRidingCouncil": { "SKIP_GET_URL": "SKIP_GET_URL", diff --git a/uk_bin_collection/uk_bin_collection/councils/EastNorthamptonshireCouncil.py b/uk_bin_collection/uk_bin_collection/councils/EastNorthamptonshireCouncil.py deleted file mode 100644 index accff11a97..0000000000 --- a/uk_bin_collection/uk_bin_collection/councils/EastNorthamptonshireCouncil.py +++ /dev/null @@ -1,118 +0,0 @@ -import hashlib -import math -import time -from datetime import timedelta - -from uk_bin_collection.uk_bin_collection.common import * -from uk_bin_collection.uk_bin_collection.get_bin_data import \ - AbstractGetBinDataClass - - -def ct(e: str) -> int: - """ - Mimic ct() function in main.cbc0dd8a.js - :rtype: int - :param e: Day name - :return: Day index - """ - if e == "MON": - return 0 - elif e == "TUE": - return 1 - elif e == "WED": - return 2 - elif e == "THU": - return 3 - elif e == "FRI": - return 4 - return -1 - - -def ft() -> dict: - """ - Mimic ft() function in main.cbc0dd8a.js - :rtype: dict - :return: Weeks and days - """ - e = datetime.now() - t = datetime(2022, 4, 20) - return { - "weeks": math.floor(((e - t).total_seconds() * 10) / 1e3 / 86400 / 7 % 2), - "days": math.floor(((e - t).total_seconds() * 10) / 1e3 / 86400 % 7), - } - - -def st(e: int, t: int, n: str) -> str: - """ - Mimic st() function in main.cbc0dd8a.js - :rtype: str - :return: Date - """ - d = datetime.now() + timedelta(days=(7 * t + (e - n))) - return d.strftime(date_format) - - -class CouncilClass(AbstractGetBinDataClass): - """ - Concrete classes have to implement all abstract operations of the - base class. They can also override some operations with a default - implementation. - """ - - def parse_data(self, page: str, **kwargs) -> dict: - data = {"bins": []} - uprn = kwargs.get("uprn") - check_uprn(uprn) - - headers = { - "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)", - "origin": "https://kbccollectiveapi-coll-api.e4ff.pro-eu-west-1.openshiftapps.com", - "referer": "https://kbccollectiveapi-coll-api.e4ff.pro-eu-west-1.openshiftapps.com/", - } - requests.packages.urllib3.disable_warnings() - # Check council website workings haven't changed - response = requests.get( - f"https://kbccollectiveapi-coll-api.e4ff.pro-eu-west-1.openshiftapps.com/wc-info/static/js/main.cbc0dd8a.js", - headers=headers, - ) - if ( - response.status_code != 200 - or hashlib.sha256(response.text.encode("utf-8")).hexdigest() - != "2f357c24b043c31c0157c234323c401238842c1d00f00f16c7ca3e569a0ab3cd" - ): - raise ValueError( - "Council website has changed, parser needs updating. Please open issue on GitHub." - ) - # Get variables for workings - response = requests.get( - f"https://api.northnorthants.gov.uk/test/wc-info/{uprn}?r={time.time() * 1000}", - headers=headers, - ) - if response.status_code != 200: - raise ValueError("No bin data found for provided UPRN.") - - json_response = json.loads(response.text) - sov = json_response["sov"] - day = json_response["day"] - schedule = json_response["schedule"] - # Mimic workings in main.cbc0dd8a.js - if sov == "ENC" or sov == "BCW": - n = ft() - r = 1 - if ct(day) > n["days"]: - r = 0 - for o in range(0, 10): - week = (n["weeks"] + o + r) % 2 - if (week == 0 and "B" == schedule) or (week != 0 and "B" != schedule): - bin_type = "General" - else: - bin_type = "Recycling" - collection_data = { - "type": bin_type, - "collectionDate": st(ct(day), o + r, n["days"]).replace( - ",", "" - ), - } - data["bins"].append(collection_data) - - return data diff --git a/uk_bin_collection/uk_bin_collection/councils/NorthNorthamptonshireCouncil.py b/uk_bin_collection/uk_bin_collection/councils/NorthNorthamptonshireCouncil.py new file mode 100644 index 0000000000..7941e8cbbd --- /dev/null +++ b/uk_bin_collection/uk_bin_collection/councils/NorthNorthamptonshireCouncil.py @@ -0,0 +1,68 @@ +import hashlib +import math +import time +from datetime import datetime as dtm, timedelta + +from uk_bin_collection.uk_bin_collection.common import * +from uk_bin_collection.uk_bin_collection.get_bin_data import \ + AbstractGetBinDataClass + + +def myFunc(e): + return e['start'] + +class CouncilClass(AbstractGetBinDataClass): + """ + Concrete classes have to implement all abstract operations of the + base class. They can also override some operations with a default + implementation. + """ + + def parse_data(self, page: str, **kwargs) -> dict: + data = {"bins": []} + uprn = kwargs.get("uprn") + check_uprn(uprn) + today = int(datetime.now().timestamp())*1000 + dateforurl = datetime.now().strftime("%Y-%m-%d") + dateforurl2 = (datetime.now() + timedelta(days=42)).strftime("%Y-%m-%d") + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)", + } + requests.packages.urllib3.disable_warnings() + + # Get variables for workings + response = requests.get( + f"https://cms.northnorthants.gov.uk/bin-collection-search/calendarevents/{uprn}/{dateforurl}/{dateforurl2}", + headers=headers, + ) + if response.status_code != 200: + raise ValueError("No bin data found for provided UPRN..") + + json_response = json.loads(response.text) + + output_dict = [x for x in json_response if int(''.join(filter(str.isdigit, x['start']))) >= today] + + output_json = output_dict + output_json.sort(key=myFunc) + + i = 0 + while i < len(output_json): + sov = output_json[i]['title'].lower() + if 'recycling' in sov: + bin_type = "Recycling" + elif 'garden' in sov: + bin_type = "Garden" + elif 'refuse' in sov: + bin_type = "General" + else: + bin_type = "Unknown" + dateofbin = int(''.join(filter(str.isdigit, output_json[i]['start']))) + day = dtm.fromtimestamp(dateofbin/1000) + collection_data = { + "type": bin_type, + "collectionDate": day.strftime(date_format), + } + data["bins"].append(collection_data) + i += 1 + + return data \ No newline at end of file