Skip to content

Commit

Permalink
add armadale_wa_gov_au
Browse files Browse the repository at this point in the history
  • Loading branch information
5ila5 committed Jul 6, 2023
1 parent f2a35d3 commit 4ae300b
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Waste collection schedules in the following formats and countries are supported.
<details>
<summary>Australia</summary>

- [Armadale (Western Australia)](/doc/source/armadale_wa_gov_au.md) / armadale.wa.gov.au
- [Australian Capital Territory (ACT)](/doc/source/act_gov_au.md) / cityservices.act.gov.au/recycling-and-waste
- [Banyule City Council](/doc/source/banyule_vic_gov_au.md) / banyule.vic.gov.au
- [Belmont City Council](/doc/source/belmont_wa_gov_au.md) / belmont.wa.gov.au
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import requests
from waste_collection_schedule import Collection # type: ignore[attr-defined]
from bs4 import BeautifulSoup, NavigableString
import datetime


TITLE = "Armadale (Western Australia)"
DESCRIPTION = "Source for Armadale (Western Australia)."
URL = "https://www.armadale.wa.gov.au"
TEST_CASES = {
"23 Sexty St, ARMADALE": {"address": "23 Sexty St, ARMADALE"},
"270 Skeet Rd, HARRISDALE": {"address": "270 Skeet Rd, HARRISDALE"}
}

WEEKDAYS = {
"Monday": 0,
"Tuesday": 1,
"Wednesday": 2,
"Thursday": 3,
"Friday": 4,
"Saturday": 5,
"Sunday": 6,
}


API_URL = "https://www.armadale.wa.gov.au/system/ajax"


def easter(year):
# taken from dateutil easter https://dateutil.readthedocs.io/en/stable/_modules/dateutil/easter.html to prevent dependency

y = year
g = y % 19
e = 0

# New method
c = y//100
h = (c - c//4 - (8*c + 13)//25 + 19*g + 15) % 30
i = h - (h//28)*(1 - (h//28)*(29//(h + 1))*((21 - g)//11))
j = (y + y//4 + i + 2 - c + c//4) % 7

# p can be from -6 to 56 corresponding to dates 22 March to 23 May
# (later dates apply to method 2, although 23 May never actually occurs)
p = i - j + e
d = 1 + (p + 27 + (p + 6)//40) % 31
m = 3 + (p + 26)//30
return datetime.date(int(y), int(m), int(d))


class Source:
def __init__(self, address: str):
self._address: str = address

def fetch(self):

args: dict[str, str] = {
"address": self._address,
"form_id": "waste_collection_form"
}

s = requests.Session()

r = s.get("https://www.armadale.wa.gov.au/my-waste-collection-day")
r.raise_for_status()

soup = BeautifulSoup(r.text, "html.parser")
form_build_id = soup.find(
"input", {"type": "hidden", "name": "form_build_id"})
if not form_build_id or isinstance(form_build_id, NavigableString) or not form_build_id.attrs["value"]:
raise Exception("Could not find form_build_id")

form_build_id = form_build_id["value"]
if not isinstance(form_build_id, str):
raise Exception("Could not find form_build_id")
args["form_build_id"] = form_build_id

# get json
r = s.post(API_URL, data=args)
r.raise_for_status()

data = r.json()
if len(data) < 2:
raise Exception("wrong data returned")

data = data[1]["data"]

soup = BeautifulSoup(data, "html.parser")

trs = soup.find_all("tr")
if not trs or len(trs) < 3:
raise Exception("Could not parse data correctly")

bin_day = trs[1].find("td").text.strip()
if not bin_day or not bin_day in WEEKDAYS:
raise Exception("Could not parse data correctly")
bin_day = WEEKDAYS[bin_day]

recycling: bool = trs[2].find(
"td").text.strip().lower().startswith("this week")

current_day = datetime.datetime.now().date()

diff_to_next = (bin_day - current_day.weekday()) % 7

# next is next week
if current_day.weekday() + diff_to_next >= 7:
recycling = not recycling

current_day = current_day + datetime.timedelta(days=diff_to_next)

entries = []
for i in range(52):
date = current_day
start_of_week = date - datetime.timedelta(days=date.weekday())

christmas = datetime.date(current_day.year, 12, 25)
new_years_day = datetime.date(
current_day.year + (1 if current_day.month == 12 else 0), 1, 1)
good_friday = easter(current_day.year) - datetime.timedelta(days=2)


if start_of_week <= christmas <= date or start_of_week <= new_years_day <= date:
# if christmas or new years day is in the current week
if 0 <= christmas.weekday() < 5: # if christmas is on a weekday
date += datetime.timedelta(days=1)


if date == good_friday:
date += datetime.timedelta(days=1)

entries.append(Collection(
date=date, t="rubbish", icon="mdi:trash-can"))
if recycling:
entries.append(Collection(
date=date, t="recycling", icon="mdi:recycle"))

current_day += datetime.timedelta(days=7)
recycling = not recycling

return entries
34 changes: 34 additions & 0 deletions doc/source/armadale_wa_gov_au.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Armadale (Western Australia)

Support for schedules provided by [Armadale (Western Australia)](https://www.armadale.wa.gov.au), serving Armadale (Western Australia), Australia.

## Configuration via configuration.yaml

```yaml
waste_collection_schedule:
sources:
- name: armadale_wa_gov_au
args:
address: ADDRESS

```
### Configuration Variables
**address**
*(String) (required)*
## Example
```yaml
waste_collection_schedule:
sources:
- name: armadale_wa_gov_au
args:
address: 23 Sexty St, ARMADALE

```
## How to get the source argument
Find the parameter of your address using [https://www.armadale.wa.gov.au/my-waste-collection](https://www.armadale.wa.gov.au/my-waste-collection) and write them exactly like on the web page.
2 changes: 1 addition & 1 deletion info.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Waste collection schedules from service provider web sites are updated daily, de
|--|--|
| Generic | ICS / iCal files |
| Static | User-defined dates or repeating date patterns |<!--Begin of country section-->
| Australia | Australian Capital Territory (ACT), Banyule City Council, Belmont City Council, Brisbane City Council, Campbelltown City Council (NSW), Cardinia Shire Council, City of Canada Bay Council, City of Kingston, City of Onkaparinga Council, Cumberland Council (NSW), Gold Coast City Council, Hume City Council, Inner West Council (NSW), Ipswich City Council, Ku-ring-gai Council, Lake Macquarie City Council, Logan City Council, Macedon Ranges Shire Council, Maribyrnong Council, Maroondah City Council, Melton City Council, Nillumbik Shire Council, North Adelaide Waste Management Authority, Port Adelaide Enfield, South Australia, RecycleSmart, Stonnington City Council, The Hills Shire Council, Sydney, Unley City Council (SA), Whittlesea City Council, Wollongong City Council, Wyndham City Council, Melbourne |
| Australia | Armadale (Western Australia), Australian Capital Territory (ACT), Banyule City Council, Belmont City Council, Brisbane City Council, Campbelltown City Council (NSW), Cardinia Shire Council, City of Canada Bay Council, City of Kingston, City of Onkaparinga Council, Cumberland Council (NSW), Gold Coast City Council, Hume City Council, Inner West Council (NSW), Ipswich City Council, Ku-ring-gai Council, Lake Macquarie City Council, Logan City Council, Macedon Ranges Shire Council, Maribyrnong Council, Maroondah City Council, Melton City Council, Nillumbik Shire Council, North Adelaide Waste Management Authority, Port Adelaide Enfield, South Australia, RecycleSmart, Stonnington City Council, The Hills Shire Council, Sydney, Unley City Council (SA), Whittlesea City Council, Wollongong City Council, Wyndham City Council, Melbourne |
| Austria | Andau, Apetlon, App CITIES, Bad Blumau, Bad Gleichenberg, Bad Loipersdorf, Bad Radkersburg, Bad Tatzmannsdorf, Bildein, Breitenbrunn am Neusiedler See, Breitenstein, Bruckneudorf, Buch - St. Magdalena, Burgau, Burgenländischer Müllverband, Dechantskirchen, Deutsch Goritz, Deutsch Jahrndorf, Deutsch Kaltenbrunn, Deutschkreutz, Dobl-Zwaring, Draßmarkt, Eberau, Eberndorf, Eberstein, Edelsbach bei Feldbach, Eggersdorf bei Graz, Eisenstadt, Fehring, Feistritz ob Bleiburg, Feldbach, Frankenau-Unterpullendorf, Frauenkirchen, Freistadt, Friedberg, Fürstenfeld, Gabersdorf, Gattendorf, Gols, Grafendorf bei Hartberg, Grafenstein, Gratkorn, Großwarasdorf, Großwilfersdorf, Gutenberg-Stenzengreith, Güssing, Hartberg, Heiligenkreuz am Waasen, Hofstätten an der Raab, Horitschon, Hornstein, Hüttenberg, Ilz, infeo, Innsbrucker Kommunalbetriebe, Jabing, Jagerberg, Kaindorf, Kaisersdorf, Kalsdorf bei Graz, Kapfenstein, Kirchberg an der Raab, Kleinmürbisch, Klingenbach, Klöch, Kohfidisch, Korneuburg, Laa an der Thaya, Leithaprodersdorf, Lieboch, Litzelsdorf, Loipersbach im Burgenland, Mariasdorf, Markt Hartmannsdorf, Markt Neuhodis, Marz, Mattersburg, Melk, Mettersdorf am Saßbach, Mischendorf, Mistelbach, Mitterdorf an der Raab, Mureck, Mörbisch am See, Neudorf bei Parndorf, Neufeld an der Leitha, Neusiedl am See, Nickelsdorf, Oberpullendorf, Oberwart, Oslip, Ottendorf an der Rittschein, Paldau, Pamhagen, Parndorf, Peggau, Pernegg an der Mur, Pilgersdorf, Pinggau, Pinkafeld, Podersdorf am See, Poggersdorf, Potzneusiedl, Poysdorf, Pöchlarn, Radmer, Ragnitz, Raiding, Rudersdorf, Rust, Saalfelden am Steinernen Meer, Sankt Oswald bei Plankenwarth, Schäffern, Schützen am Gebirge, Seiersberg-Pirka, Sigleß, Sinabelkirchen, St. Andrä, St. Anna am Aigen, St. Johann in der Haide, St. Lorenzen am Wechsel, St. Margarethen an der Raab, St. Margarethen im Burgenland, St. Peter - Freienstein, St. Peter am Ottersbach, St. Ruprecht an der Raab, St. Veit in der Südsteiermark, Stadt Salzburg, Stadtservice Korneuburg, Stegersbach, Steinbrunn, Steuerberg, Stinatz, Stiwoll, Stockerau, Söchau, Thal, Tieschen, Tulln an der Donau, Umweltprofis, Unterfrauenhaid, Unterkohlstätten, Unterlamm, Vasoldsberg, Vordernberg, Völkermarkt, Weiz, Werfenweng, Wies, Wiesen, Wiesfleck, Wimpassing an der Leitha, Winden am See, Wolfsberg, Wolkersdorf im Weinviertel, WSZ Moosburg, Zagersdorf, Zillingtal, Zurndorf, Übelbach |
| Belgium | Hygea, Limburg.net, Recycle! |
| Canada | City of Toronto, London, Ontario, Canada |
Expand Down

0 comments on commit 4ae300b

Please sign in to comment.