Skip to content

Commit

Permalink
Merge branch 'master' into repserv_yardwaste
Browse files Browse the repository at this point in the history
  • Loading branch information
dt215git authored Oct 22, 2023
2 parents 3d15260 + 491a309 commit 7050f4f
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ repos:
hooks:
- id: codespell
args:
- --ignore-words-list=hass,alot,datas,dof,dur,farenheit,hist,iff,ines,ist,lightsensor,mut,nd,pres,referer,ser,serie,te,technik,ue,uint,visability,wan,wanna,withing,Adresse,termine,adresse,oder,alle,assistent,hart
- --ignore-words-list=hass,alot,datas,dof,dur,farenheit,hist,iff,ines,ist,lightsensor,mut,nd,pres,referer,ser,serie,te,technik,ue,uint,visability,wan,wanna,withing,Adresse,termine,adresse,oder,alle,assistent,hart,marz
- --skip="./.*,*.csv,*.json"
- --quiet-level=2
exclude_types: [csv, json]
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ Waste collection schedules in the following formats and countries are supported.

- [FKF Budapest](/doc/source/fkf_bp_hu.md) / fkf.hu
- [FKF Budaörs](/doc/source/fkf_bo_hu.md) / fkf.hu
- [ÉTH (Érd, Diósd, Nagytarcsa, Sóskút, Tárnok)](/doc/source/eth_erd_hu.md) / eth-erd.hu
</details>

<details>
Expand Down Expand Up @@ -1102,6 +1103,7 @@ Waste collection schedules in the following formats and countries are supported.
- [Vale of White Horse District Council](/doc/source/binzone_uk.md) / whitehorsedc.gov.uk
- [Walsall Council](/doc/source/walsall_gov_uk.md) / walsall.gov.uk
- [Warrington Borough Council](/doc/source/warrington_gov_uk.md) / warrington.gov.uk
- [Warwick District Council](/doc/source/warwickdc_gov_uk.md) / warwickdc.gov.uk
- [Waverley Borough Council](/doc/source/waverley_gov_uk.md) / waverley.gov.uk
- [Wealden District Council](/doc/source/wealden_gov_uk.md) / wealden.gov.uk
- [Welwyn Hatfield Borough Council](/doc/source/welhat_gov_uk.md) / welhat.gov.uk
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,19 @@
"Test_004": {"uprn": 90092621},
}
ICON_MAP = {"RECYCLING": "mdi:recycle", "GARDEN": "mdi:leaf", "REFUSE": "mdi:trash-can"}
REGEX = r"(\d+ \w{3})"
REGEX = {
"DATES": r"(\d+ \w{3})",
"DAYS": r"every: (Monday|Tuesday|Wednesday|Thursday|Friday)",
}
DAYS = {
"Monday": 0,
"Tuesday": 1,
"Wednesday": 2,
"Thursday": 3,
"Friday": 4,
"Saturday": 5,
"Sunday": 6,
}


class Source:
Expand All @@ -35,30 +47,16 @@ def check_date(self, d: str, t: datetime, y: int):
return date.date()

def append_entries(self, d: datetime, w: str, e: list) -> list:
"""
Append provided entry and Refuse entry for the same day.
Refuse is collected on the same dates as alternating Recycling/Garden collections,
so create two entries for each date Refuse & Recycling, or Refuse & Garden
"""
e.append(
Collection(
date=d,
t=w,
icon=ICON_MAP.get(w.upper()),
)
)
e.append(
Collection(
date=d,
t="Refuse",
icon=ICON_MAP.get("REFUSE"),
)
)
return e

def fetch(self):

today = datetime.now()
today = today.replace(hour=0, minute=0, second=0, microsecond=0)
yr = int(today.year)
Expand All @@ -71,22 +69,30 @@ def fetch(self):

panel = soup.find("div", {"aria-label": "Refuse and Recycling Collection"})
panel_data = panel.find("div", {"class": "atPanelData"})
panel_data = panel_data.text.split("Next")[
waste_data = panel_data.text.split("Next")[
1:
] # remove first element it just contains general info

entries = []
for item in panel_data:
# Deal with Recycling and Garden collections
for item in waste_data:
text = item.replace("\r\n", "").strip()
if "recycling" in text:
dates = re.findall(REGEX, text)
dates = re.findall(REGEX["DATES"], text)
for dt in dates:
dt = self.check_date(dt, today, yr)
self.append_entries(dt, "Recycling", entries)
elif "garden" in text:
dates = re.findall(REGEX, text)
dates = re.findall(REGEX["DATES"], text)
for dt in dates:
dt = self.check_date(dt, today, yr)
self.append_entries(dt, "Garden", entries)

# Refuse collections only have a DAY not a date, so work out dates for the next few collections
refuse_day = re.findall(REGEX["DAYS"], panel_data.text)[0]
refuse_date = today + timedelta((int(DAYS[refuse_day]) - today.weekday()) % 7)
for i in range(0, 4):
temp_date = refuse_date + timedelta(days=7 * i)
self.append_entries(temp_date.date(), "Refuse", entries)

return entries
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import json
import requests
import datetime

from bs4 import BeautifulSoup
from waste_collection_schedule import Collection

TITLE = "ÉTH (Érd, Diósd, Nagytarcsa, Sóskút, Tárnok)"
DESCRIPTION = "Source script for www.eth-erd.hu"
URL = "https://www.eth-erd.hu"
COUNTRY = "hu"
TEST_CASES = {
"Test_1": {
"city": "Diósd",
"street": "Diófasor",
"house_number": 10
},
"Test_2": {
"city": "Érd",
"street": "Hordó",
"house_number": 3
},
"Test_3": {
"city": "Sóskút"
}
}

API_URL = "https://www.eth-erd.hu/trashcalendarget"

ICON_MAP = {
"Kommunális": "mdi:trash-can",
"Szelektív": "mdi:recycle",
"Zöldhulladék": "mdi:leaf",
"Papír": "mdi:newspaper",
"Fenyőfa": "mdi:pine-tree",
"Üveg": "mdi:glass-fragile",
}

NAME_MAP = {
"Kommunális": "Communal",
"Szelektív": "Selective",
"Zöldhulladék": "Green",
"Papír": "Paper",
"Fenyőfa": "Pine Tree",
"Üveg": "Glass",
}

CITY_MAP = {
"diósd": 1,
"érd": 2,
"nagytarcsa": 822,
"sóskút": 3,
"tárnok": 4,
}


class Source:
def __init__(self, city: str, street: str = "", house_number: int = 1) -> None:
self._city = city
self._street = street
self._house_number = house_number

def fetch(self):
session = requests.Session()

city_id = CITY_MAP.get(self._city.lower())
if city_id == None: raise Exception("City not found")
has_streets = city_id != CITY_MAP["sóskút"]

if has_streets:
r = session.post(
API_URL + "streets",
data={
"sid": city_id
},
headers={
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
}
)
r.raise_for_status()
streets = json.loads(r.text)["results"]
available_streets = [item["text"] for item in streets]
try: street_id = [item for item in streets if item.get('text') == self._street][0]["id"]
except IndexError: raise Exception("Street not found, available streets: " + ", ".join(available_streets))

r = session.post(
API_URL,
data={
"wctown": city_id,
"wcstreet": street_id,
"wchousenumber": self._house_number,
} if has_streets else {
"wctown": city_id,
},
headers={
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
}
)
r.raise_for_status()
result: dict = json.loads(r.text)

entries = []

for trash_type_id in result["types"]:
trash_type = result["types"][trash_type_id]
trash_icon = ICON_MAP[trash_type["name"]]
trash_name = NAME_MAP[trash_type["name"]]

for element in result["routelist"][trash_type_id]:
entries.append(
Collection(
date = datetime.datetime.strptime(element, "%Y-%m-%d").date(),
t = trash_name,
icon = trash_icon,
)
)

return entries
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from datetime import datetime

import requests
from bs4 import BeautifulSoup
from waste_collection_schedule import Collection # type: ignore[attr-defined]

TITLE = "Warwick District Council"
DESCRIPTION = "Source for Warwick District Council rubbish collection."
URL = "https://www.warwickdc.gov.uk"
TEST_CASES = {
"Test_001": {"uprn": "100070260258"},
"Test_002": {"uprn": "100070258568"},
"Test_003": {"uprn": 100070263501},
}
ICON_MAP = {
"FOOD": "mdi:food",
"GARDEN": "mdi:leaf",
"RECYCLING": "mdi:recycle",
"REFUSE": "mdi:trash-can",
}


class Source:
def __init__(self, uprn):
self._uprn = str(uprn)

def fetch(self):
s = requests.Session()
r = s.get(
f"https://estates7.warwickdc.gov.uk/PropertyPortal/Property/Recycling/{self._uprn}"
)
soup = BeautifulSoup(r.text, "html.parser")

infoboxes = soup.findAll(
"div", {"class": "col-xs-12 text-center waste-dates margin-bottom-15"}
)

entries = []
for box in infoboxes:
items = box.findAll("p")
waste_type = items[0].text.strip().split(" ")[0].strip()
dates = [datetime.strptime(d.text, "%d/%m/%Y").date() for d in items[1:]]
for date in dates:
entries.append(
Collection(
date=date,
t=waste_type,
icon=ICON_MAP.get(waste_type.upper()),
)
)

return entries
41 changes: 41 additions & 0 deletions doc/source/eth_erd_hu.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# ÉTH (Érd, Diósd, Nagytarcsa, Sóskút, Tárnok)

Support for schedules provided by [ÉTH](https://www.eth-erd.hu/hulladeknaptar), serving Diósd, Érd, Nagytarcsa, Sóskút, Tárnok, HU.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: eth_erd_hu
args:
city: CITY_NAME
street: FULL_STREET_NAME
house_number: HOUSE_NUMBER
```
### Configuration Variables
**CITY**
*(string) (required)*
**STREET**
*(string) (required)*
without "utca", "út", etc.
not required in Sóskút
**HOUSE_NUMBER**
*(number) (required)*
not required in Sóskút
## Example
```yaml
waste_collection_schedule:
sources:
- name: eth_erd_hu
args:
city: Diósd
street: Diófasor
house_number: 10
```
35 changes: 35 additions & 0 deletions doc/source/warwickdc_gov_uk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Warwick District Council

Support for schedules provided by [Warwick District Council](https://www.warwickdc.gov.uk/info/20465/rubbish_waste_and_recycling).

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: warwickdc_gov_uk
args:
uprn: UNIQUE_PROPERTY_REFERENCE_NUMBER
```
### Configuration Variables
**uprn**
*(string) (required)*
## Example using UPRN
```yaml
waste_collection_schedule:
sources:
- name: warwickdc_gov_uk
args:
uprn: "100070263501"
```
## How to find your `UPRN`

An easy way to find your Unique Property Reference Number (UPRN) is by going to <https://www.findmyaddress.co.uk/> and entering in your address details.

Alternatively you can inspect the URL on [Warwick District Council](https://www.warwickdc.gov.uk/info/20465/rubbish_waste_and_recycling). Having searched for your collection schedule, your UPRN is the collection of digits at the end of the URL, for example: _https://estates7.warwickdc.gov.uk/PropertyPortal/Property/Recycling/`100070260258`_
Loading

0 comments on commit 7050f4f

Please sign in to comment.