Skip to content

Commit

Permalink
Merge pull request #213 from mampfes/a_region_ch
Browse files Browse the repository at this point in the history
add source for a_region_ch
  • Loading branch information
mampfes authored Apr 23, 2022
2 parents f54cc0d + 3a63834 commit f2bac87
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ Currently the following service providers are supported:

### Switzerland

- [A-Region.ch](./doc/source/a_region_ch.md)
- [Lindau.ch](./doc/source/lindau_ch.md)

### United States of America
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import datetime
from urllib.parse import parse_qs, urlparse

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

TITLE = "A-Region"
DESCRIPTION = "Source for A-Region, Switzerland waste collection."
URL = "https://www.a-region.ch"
TEST_CASES = {
"Andwil": {"municipality": "Andwil"},
}

BASE_URL = "https://www.a-region.ch"


class Source:
def __init__(self, municipality):
self._municipality = municipality

def fetch(self):
municipalities = self.get_municipalities()
if self._municipality not in municipalities:
raise Exception(f"municipality '{self._municipality}' not found")

waste_types = self.get_waste_types(municipalities[self._municipality])

entries = []

for (waste_type, link) in waste_types.items():
dates = self.get_dates(link)

for d in dates:
entries.append(Collection(d, waste_type))

return entries

def get_municipalities(self):
r = requests.get(f"{BASE_URL}/index.php?apid=13875680&apparentid=4618613")
r.raise_for_status()

municipalities = {}

soup = BeautifulSoup(r.text, features="html.parser")
downloads = soup.find_all("a", href=True)
for download in downloads:
# href ::= "/index.hp"
href = download.get("href")
if "ref=search" in href:
for title in download.find_all("div", class_="title"):
# title ::= "Abfallkalender Andwil"
municipalities[title.string.removeprefix("Abfallkalender ")] = href

return municipalities

def get_waste_types(self, link):
r = requests.get(f"{BASE_URL}{link}")
r.raise_for_status()

waste_types = {}

soup = BeautifulSoup(r.text, features="html.parser")
downloads = soup.find_all("a", href=True)
for download in downloads:
# href ::= "/index.php?apid=12731252&apparentid=5011362"
href = download.get("href")
if "apparentid" in href:
for title in download.find_all("div", class_="title"):
# title ::= "Altmetall"
waste_types[title.string] = href

return waste_types

def get_dates(self, link):
r = requests.get(f"{BASE_URL}{link}")
r.raise_for_status()

dates = set()

soup = BeautifulSoup(r.text, features="html.parser")
downloads = soup.find_all("a", href=True)
for download in downloads:
# href ::= "/appl/ics.php?apid=12731252&from=2022-05-04%2013%3A00%3A00&to=2022-05-04%2013%3A00%3A00"
href = download.get("href")
if "ics.php" in href:
parsed = urlparse(href)
query = parse_qs(parsed.query)
date = datetime.datetime.strptime(query["from"][0], "%Y-%m-%d %H:%M:%S")
dates.add(date.date())

return dates
32 changes: 32 additions & 0 deletions doc/source/a_region_ch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# A-Region, Switzerland

Support for schedules provided by [A-Region.ch](https://www.a-region.ch)

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: a_region_ch
args:
city: CITY
```
### Configuration Variables
**city**<br>
*(string) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: a_region_ch
args:
city: Andwil
```
## How to get the source argument
Open [Abfallkalender A-Region](https://www.a-region.ch/index.php?apid=13875680&apparentid=4618613) which shows a list of all cities. Select your city from the list.
1 change: 1 addition & 0 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ Currently the following service providers are supported:

### Switzerland

- [A-Region.ch](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/a_region_ch.md)
- [Lindau.ch](https://github.com/mampfes/hacs_waste_collection_schedule/blob/master/doc/source/lindau_ch.md)

### United States of America
Expand Down

0 comments on commit f2bac87

Please sign in to comment.