code cleanup (black, pylint)

This commit is contained in:
mampfes
2022-03-08 18:04:38 +01:00
parent c31757187f
commit 31257e73e4
2 changed files with 42 additions and 35 deletions

View File

@@ -1,12 +1,10 @@
import datetime
from html.parser import HTMLParser
import json
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
from waste_collection_schedule.service.ICS import ICS
TITLE = "Wellington City Council"
DESCRIPTION = "Source for Wellington City Council."
URL = "https://wellington.govt.nz"
@@ -17,58 +15,65 @@ TEST_CASES = {
ICON_MAP = {
"Rubbish Collection": "mdi:trash-can",
"Rubbish Collection": "mdi:trash-can",
"Glass crate": "mdi:glass-fragile",
"Wheelie bin or recycling bags":"mdi:recycle",
}
"Wheelie bin or recycling bags": "mdi:recycle",
}
PICTURE_MAP = {
"Rubbish Collection": "https://wellington.govt.nz/assets/images/rubbish-recycling/rubbish-bag.png",
"Rubbish Collection": "https://wellington.govt.nz/assets/images/rubbish-recycling/rubbish-bag.png",
"Glass crate": "https://wellington.govt.nz/assets/images/rubbish-recycling/glass-crate.png",
"Wheelie bin or recycling bags":"https://wellington.govt.nz/assets/images/rubbish-recycling/wheelie-bin.png",
}
"Wheelie bin or recycling bags": "https://wellington.govt.nz/assets/images/rubbish-recycling/wheelie-bin.png",
}
class Source:
def __init__(self, streetId= None, streetName= None):
def __init__(self, streetId=None, streetName=None):
self._streetId = streetId
self._streetName = streetName
self._ics = ICS()
def fetch(self):
# get token
if self._streetName:
if self._streetName:
url = "https://wellington.govt.nz/layouts/wcc/GeneralLayout.aspx/GetRubbishCollectionStreets"
headers = {
"Origin":"https://wellington.govt.nz/rubbish-recycling-and-waste/when-to-put-out-your-rubbish-and-recycling",
"Access-Control-Request-Method": "POST",
"Content-Type": "application/json",
}
data = {"partialStreetName":self._streetName}
json_string = json.dumps(data)
r = requests.post(url, headers=headers, data=json_string)
data = {"partialStreetName": self._streetName}
r = requests.post(url, json=data)
data = json.loads(r.text)
if len(data["d"]) == 0:
raise Exception(f"No result found for {self._streetName}")
if len(data["d"]) > 1:
raise Exception(f"More then one result returned for {self._streetName}, be more specific or use StreetId instead")
self._streetId = data["d"][0].get('Key')
raise Exception(f"No result found for streetName {self._streetName}")
if len(data["d"]) > 1:
raise Exception(
f"More then one result returned for streetName {self._streetName}, be more specific or use streetId instead"
)
self._streetId = data["d"][0].get("Key")
if not self._streetId:
raise Exception(f"No StreetId Supplied")
raise Exception("No streetId supplied")
url = "https://wellington.govt.nz/~/ical/"
params = {"type":"recycling","streetId":self._streetId,"forDate": datetime.date.today()}
params = {
"type": "recycling",
"streetId": self._streetId,
"forDate": datetime.date.today(),
}
r = requests.get(url, params=params)
if not r.text.startswith("BEGIN:VCALENDAR"):
raise Exception(f"StreetID: {self._streetId} is not a valid streetID")
raise Exception(f"{self._streetId} is not a valid streetID")
dates = self._ics.convert(r.text)
entries = []
for d in dates:
for wasteType in d[1].split("&"):
wasteType = wasteType.strip()
entries.append(Collection(d[0], wasteType, picture=PICTURE_MAP[wasteType], icon=ICON_MAP[wasteType]))
entries.append(
Collection(
d[0],
wasteType,
picture=PICTURE_MAP[wasteType],
icon=ICON_MAP[wasteType],
)
)
return entries

View File

@@ -17,9 +17,11 @@ waste_collection_schedule:
**streetName**<br>
*(string)*<br>
**streetId**<br>
*(string)*<br>
*One of the above is Required*
*One of the above is required*
## Example
@@ -30,6 +32,7 @@ waste_collection_schedule:
args:
streetName: Chelsea Street
```
```yaml
waste_collection_schedule:
sources:
@@ -38,13 +41,12 @@ waste_collection_schedule:
streetId: 6515
```
## How to get the source argument
The source argument is the either the street name or streetid number from Wellington City Council site:
The source argument is the either the street name or street id number from Wellington City Council site:
- Open your When to put out your rubbish and recycling page, and enter your address in the search box [on the Wellington City Council collection day finder](https://wellington.govt.nz/rubbish-recycling-and-waste/when-to-put-out-your-rubbish-and-recycling)
- The "streetName" variable uses the same code that the search box uses to find the streetID from your street name, suburb. But like the search box you do not need to enter the full name if your text matches only one result. For example "Miramar" would get you two results for Miramar Ave, Miramar and Miramar North Road, Miramar and the script would error as it can only deal with one street per call, so you would need to write "Miramar Ave" or "Miramar Avenue" etc to get the right result.
- If you are having issues with the script finding your street, or you can't get it to narrow the result without selection options on the Wellington Council site, you can use the streetID parameter directly.
- Once you have found your street on the Website, look at the Blue button which is meant to show you the next weeks collection info, if you hover over the button, right click and copy link, you should get back a URL e.g. https://wellington.govt.nz/rubbish-recycling-and-waste/when-to-put-out-your-rubbish-and-recycling/components/collection-search-results?streetId=8187&addWeeks=1
- Look for the streetId parameter in the URL in the example link above, it is streetId= `8187`
- Open your *When to put out your rubbish and recycling* page, and enter your address in the search box [on the Wellington City Council collection day finder](https://wellington.govt.nz/rubbish-recycling-and-waste/when-to-put-out-your-rubbish-and-recycling)
- The "streetName" variable uses the same code that the search box uses to find the streetId from your street name, suburb. But like the search box you do not need to enter the full name if your text matches only one result. For example "Miramar" would get you two results for Miramar Ave, Miramar and Miramar North Road, Miramar and the script would error as it can only deal with one street per call, so you would need to write "Miramar Ave" or "Miramar Avenue" etc to get the right result.
- If you are having issues with the script finding your street, or you can't get it to narrow the result without selection options on the Wellington Council site, you can use the streetId parameter directly.
- Once you have found your street on the website, look at the blue button which is meant to show you the next weeks collection info, if you hover over the button, right click and copy link, you should get back a URL e.g. https://wellington.govt.nz/rubbish-recycling-and-waste/when-to-put-out-your-rubbish-and-recycling/components/collection-search-results?streetId=8187&addWeeks=1
- Look for the streetId parameter in the URL in the example link above, it is streetId=`8187`