Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into cumberland
Browse files Browse the repository at this point in the history
  • Loading branch information
5ila5 committed Jan 6, 2025
2 parents e76aa66 + 2cb7ccd commit 4174d53
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,7 @@ If your service provider is not listed, feel free to open a [source request issu
- [Rhondda Cynon Taf County Borough Council](/doc/source/rctcbc_gov_uk.md) / rctcbc.gov.uk
- [Richmondshire District Council](/doc/source/richmondshire_gov_uk.md) / richmondshire.gov.uk
- [Rochdale Borough Council](/doc/source/rochdale_gov_uk.md) / rochdale.gov.uk
- [Rother District Council](/doc/source/rother_gov_uk.md) / rother.gov.uk
- [Rotherham](/doc/source/apps_imactivate_com.md) / rotherham.gov.uk
- [Rotherham Metropolitan Borough Council](/doc/source/rotherham_gov_uk.md) / rotherham.gov.uk
- [Royal Borough Of Greenwich](/doc/source/royalgreenwich_gov_uk.md) / royalgreenwich.gov.uk
Expand Down
6 changes: 6 additions & 0 deletions custom_components/waste_collection_schedule/sources.json
Original file line number Diff line number Diff line change
Expand Up @@ -14731,6 +14731,12 @@
"default_params": {},
"id": "rochdale_gov_uk"
},
{
"title": "Rother District Council",
"module": "rother_gov_uk",
"default_params": {},
"id": "rother_gov_uk"
},
{
"title": "Rotherham",
"module": "apps_imactivate_com",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -15998,6 +15998,29 @@
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"args_rother_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "Ein lesbarerer oder benutzerfreundlicherer Name für den Müllkalender. Wenn nichts angegeben wird, wird der Name der Quelle verwendet.",
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"reconfigure_rother_gov_uk": {
"title": "Quelle Neu Konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md",
"data": {
"calendar_title": "Kalender Titel",
"uprn": "UPRN"
},
"data_description": {
"uprn": "Eine einfache Möglichkeit, Ihre Unique Property Reference Number (UPRN) zu finden, besteht darin, auf https://www.findmyaddress.co.uk/ zu gehen und Ihre Adressdaten einzugeben."
}
},
"args_rotherham_gov_uk": {
"title": "Quelle konfigurieren",
"description": "Konfiguriere deinen Service Provider. Mehr details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rotherham_gov_uk.md",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -16045,6 +16045,29 @@
"uprn": "An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details."
}
},
"args_rother_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "A more readable, or user-friendly, name for the waste calendar. If nothing is provided, the name returned by the source will be used.",
"uprn": "An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details."
}
},
"reconfigure_rother_gov_uk": {
"title": "Reconfigure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md.",
"data": {
"calendar_title": "Calendar Title",
"uprn": "UPRN"
},
"data_description": {
"uprn": "An easy way to discover your Unique Property Reference Number (UPRN) is by going to https://www.findmyaddress.co.uk/ and entering in your address details."
}
},
"args_rotherham_gov_uk": {
"title": "Configure Source",
"description": "Configure your service provider. More details: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rotherham_gov_uk.md.",
Expand Down
23 changes: 23 additions & 0 deletions custom_components/waste_collection_schedule/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -15959,6 +15959,29 @@
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"args_rother_gov_uk": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"uprn": "UPRN"
},
"data_description": {
"calendar_title": "Puoi cambiare il nome del calendario della raccolta dei rifiuti, altrimenti di default verra' utilizzato il nome del tuo fornitore di servizi.",
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"reconfigure_rother_gov_uk": {
"title": "Riconfigurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Per maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rother_gov_uk.md.",
"data": {
"calendar_title": "Nome Calendario",
"uprn": "UPRN"
},
"data_description": {
"uprn": "Un modo facile per scoprire il tuo Numero di Riferimento Proprietà Unica (UPRN) è andare su https://www.findmyaddress.co.uk/ e inserire i dettagli del tuo indirizzo."
}
},
"args_rotherham_gov_uk": {
"title": "Configurazione Sorgente",
"description": "Compila i campi per ottenere le informazioni sul tuo servizio di raccolta. Maggiori informazioni: https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/rotherham_gov_uk.md.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import datetime
import json
import re

import requests
Expand All @@ -15,7 +14,7 @@
"Am Schwarzen Berge": {"street": "am Schwarzen Berge "},
}

API_URL = "https://www.cederbaum.de/blaue-tonne/abfuhrkalender"
API_URL = "https://www.cederbaum.de/blaue-tonne/"
ICON_MAP = {
"PAPER": "mdi:newspaper",
}
Expand Down Expand Up @@ -54,7 +53,7 @@ def get_street_id(self):
value = option.get("value")
text = option.get_text()
if text.lower().strip() == self._street.lower().strip():
self.street_id = value
self.street_id = int(value)
break

def get_collection_data(self):
Expand All @@ -63,15 +62,23 @@ def get_collection_data(self):

script_tags = self.page_source.find_all("script")
script_with_text = [tag for tag in script_tags if tag.string]
pattern = re.compile(r"var rate = (\{.*?\});")
pattern = re.compile(r"var rate = \[(.*?)\];")

# the dates are stored in a hardcoded js array
raw_date_text = None
for script_tag in script_with_text:
match = pattern.search(script_tag.string)
if match:
var_content = match.group(1)
self.collection_data = json.loads(var_content)
raw_date_text = match.group(1)
break

if not raw_date_text:
raise ValueError("Raw date text not found")

# one list of dates per location
raw_dates = [text.strip('"') for text in raw_date_text.split('","')]
self.collection_data = [dates.split(",") for dates in raw_dates]

def fetch(self):
self.fetch_page_source()
self.get_street_id()
Expand All @@ -81,11 +88,9 @@ def fetch(self):
raise ValueError("No collection data found")

entries = []
waste_dates = self.collection_data[self.street_id]["Termine"]
waste_dates = self.collection_data[self.street_id] # type: ignore
for waste_date in waste_dates:
date = datetime.datetime.strptime(
waste_dates[waste_date]["Termin"], "%d.%m.%Y"
)
date = datetime.datetime.strptime(waste_date, "%d.%m.%Y")

entries.append(
Collection(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import requests
import datetime
from bs4 import BeautifulSoup
from dateutil import parser
from waste_collection_schedule import Collection # type: ignore[attr-defined]
Expand Down Expand Up @@ -40,6 +41,8 @@ def fetch(self):
for i in range(len(dates)):
bint = " ".join(bins[i].text.split()[2:4])
date = parser.parse(dates[i].text).date()
if date.month == 1 and datetime.date.today().month == 12 and date.year == datetime.date.today().year:
date = date.replace(year=date.year+1)
entries.append(
Collection(
date=date,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,14 @@ def fetch(self):
except IndexError:
pass
else:
try:
dt = datetime.strptime(collectionDate, "%A %d %B %Y").date()
except ValueError:
dt = datetime.strptime(collectionDate, "%d %B %Y").date()

entries.append(
Collection(
date=datetime.strptime(collectionDate, "%A %d %B %Y").date(),
date=dt,
t=title,
icon=ICON_MAP.get(title),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import re
from datetime import datetime

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

# With verify=True the POST fails due to a SSLCertVerificationError.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

TITLE = "Rother District Council"
DESCRIPTION = "Source for Rother District Council."
URL = "https://www.rother.gov.uk"
TEST_CASES = {
"Test_01": {"uprn": 10002653856},
"Test_02": {"uprn": 100060102891},
}


ICON_MAP: dict[str, str] = {
"refuse": "mdi:trash-can",
"garden": "mdi:leaf",
"recycling": "mdi:recycle",
}


API_URL = "https://www.rother.gov.uk/wp-admin/admin-ajax.php"


HEADERS = {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"User-Agent": "Mozilla/5.0",
}


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

def fetch(self):
data = {"action": "get_address_data", "uprn": self._uprn}

r = requests.post(API_URL, headers=HEADERS, data=data, timeout=10, verify=False)
r.raise_for_status()

# Response is JSON with fields: success(boolean) and data (string containing HTML fragment)
bin_collection_data = r.json()
self.__validate_response(bin_collection_data)

soup = BeautifulSoup(bin_collection_data["data"], "html.parser")

entries = []
for bin_type in ICON_MAP:
date_span = soup.find("span", class_=f"find-my-nearest-bindays-{bin_type}")
if date_span:
date_str = date_span.text.strip()

if date_str.startswith("Sign up"):
# Garden waste requires a subscription and may not be available for all
continue

parsed_collection_date = datetime.strptime(
self.__remove_ordinal_indicators(date_str), "%A %d %B"
)

# The returned bin day does not include the year
collection_year = self.__resolve_bin_collection_year(
parsed_collection_date.month
)

parsed_collection_date = parsed_collection_date.replace(
year=collection_year
)

icon = ICON_MAP.get(bin_type)
entries.append(
Collection(
date=parsed_collection_date.date(), t=bin_type, icon=icon
)
)

if not entries:
raise Exception("Unable to find any bin collection schedules")

return entries

def __validate_response(self, bin_collection_data):
if "success" not in bin_collection_data or "data" not in bin_collection_data:
raise Exception("Rother Disctrict Council returned an invalid response")

if not bin_collection_data["success"]:
raise Exception(
"Rother Disctrict Council returned a non-successful response"
)

def __remove_ordinal_indicators(self, original_str: str) -> str:
return re.sub(r"(\d)(st|nd|rd|th)", r"\1", original_str)

def __resolve_bin_collection_year(self, collection_month: int) -> int:
today = datetime.now()
collection_year = today.year

# Collection date is in previous year
if collection_month == 12 and today.month == 1:
collection_year -= 1

# Collection date is in the next year
elif collection_month == 1 and today.month == 12:
collection_year += 1

return collection_year
Loading

0 comments on commit 4174d53

Please sign in to comment.