Update telford_gov_uk.py to move the date parser, modify the URL parameters and remove the type map following review.

Update the documentation to reflect the removal of the type map.
This commit is contained in:
Alistair Clare
2023-01-05 22:42:17 +00:00
parent af7086ad88
commit 06fd72eec7
2 changed files with 42 additions and 42 deletions

View File

@@ -15,16 +15,8 @@ TEST_CASES = {
}
API_URLS = {
"address_search": "https://dac.telford.gov.uk/BinDayFinder/Find/PostcodeSearch?postcode={postcode}",
"collection": "https://dac.telford.gov.uk/BinDayFinder/Find/PropertySearch?uprn={uprn}",
}
# Map the names to something readable
NAMES = {
"Red Top Container": "General (Red)",
"Purple / Blue Containers": "Recycling (Purple/Blue)",
"Green Container": "Garden (Green)",
"Silver Containers": "Food (Silver)",
"address_search": "https://dac.telford.gov.uk/BinDayFinder/Find/PostcodeSearch",
"collection": "https://dac.telford.gov.uk/BinDayFinder/Find/PropertySearch",
}
# Map the names to icons
@@ -38,25 +30,6 @@ ICON_MAP = {
# Path to the images provided by the council for the containers
IMAGEPATH = "https://dac.telford.gov.uk/BinDayFinder/Content/BinIcons/"
# Function to parse the data as the council JSON API returns no year for the collections
# and so it needs to be calculated to format the date correctly
def parsedate(datestring):
today = datetime.date.today()
year = today.year
datestring = re.sub(r'(\d)(st|nd|rd|th)', r'\1', datestring)
date = datetime.datetime.strptime(datestring, "%A %d %B").date()
# Calculate the year. As we only get collections 2 weeks in advance we can assume the current
# year unless the month is January in December where it will be next year
if (date.month == 1) and (today.month == 12):
year=year+1
date = date.replace(year=year)
return date
class Source:
def __init__(self, post_code=None, name_number=None, uprn=None):
self._post_code = post_code
@@ -66,10 +39,15 @@ class Source:
def fetch(self):
if not self._uprn:
# look up the UPRN for the address
q = str(API_URLS["address_search"]).format(
postcode=self._post_code)
params = {
"postcode": self._post_code
}
r = requests.get(
API_URLS["address_search"],
params=params
)
r = requests.get(q)
r.raise_for_status()
# Required to parse the returned JSON
@@ -85,11 +63,13 @@ class Source:
# Get the collection information
q = str(API_URLS["collection"]).format(
uprn=self._uprn
params = {"uprn": self._uprn}
r = requests.get(
API_URLS["collection"],
params=params
)
r = requests.get(q)
r.raise_for_status()
x = json.loads(r.json())
@@ -99,11 +79,31 @@ class Source:
if collections:
for collection in collections:
# Parse the data as the council JSON API returns no year for the collections
# and so it needs to be calculated to format the date correctly
today = datetime.date.today()
year = today.year
# Remove nd,rd,th,st from the date so it can be parsed
datestring = re.sub(r'(\d)(st|nd|rd|th)', r'\1', collection["nextDate"])
date = datetime.datetime.strptime(datestring, "%A %d %B").date()
# Calculate the year. As we only get collections 2 weeks in advance we can assume the current
# year unless the month is January in December where it will be next year
if (date.month == 1) and (today.month == 12):
year=year+1
date = date.replace(year=year)
entries.append(
Collection(
date=parsedate(collection["nextDate"]),
t=NAMES.get(collection["name"]),
date=date,
t=collection["name"],
icon=ICON_MAP.get(collection["name"]),
picture=IMAGEPATH+collection["imageURL"]
)

View File

@@ -67,14 +67,14 @@ The API will return the next collection date for each container type. This will
## Returned collection types
### General (Red)
### Red Top Container
Red top container for general waste
### Recycling (Purple/Blue)
### Purple / Blue Containers
Purple top container for glass / cans and Blue bag for paper / cardboard
### Garden (Green)
### Green Container
Green container for garden waste
### Food (Silver)
### Silver Container
Silver coltainer for food waste