-
-
Notifications
You must be signed in to change notification settings - Fork 738
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
288 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
...components/waste_collection_schedule/waste_collection_schedule/source/kiedysmieci_info.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
import datetime | ||
import ssl, urllib, json, requests | ||
from waste_collection_schedule import Collection | ||
from waste_collection_schedule.exceptions import ( | ||
SourceArgumentNotFoundWithSuggestions, | ||
) | ||
|
||
TITLE = "Kiedy śmieci" | ||
DESCRIPTION = "Source script for Kiedy śmieci, Poland" | ||
URL = "https://kiedysmieci.info" | ||
COUNTRY = "pl" | ||
TEST_CASES = { | ||
"Nadolany, podkarpackie, sanocki, Bukowsko": { | ||
"voivodeship": "podkarpackie", | ||
"district": "sanocki", | ||
"municipality": "Bukowsko", | ||
"street": "Nadolany", | ||
}, | ||
"Kędzierz, podkarpackie, dębicki, Dębica": { | ||
"voivodeship": "podkarpackie", | ||
"district": "dębicki", | ||
"municipality": "Dębica", | ||
"street": "Kędzierz", | ||
}, | ||
} | ||
|
||
API_URL = "https://cloud.fxsystems.com.pl:8078/odbiory_smieci/%s" | ||
|
||
ICON_MAP = { | ||
"zmieszane": "mdi:trash-can", | ||
"metale i tworzywa sztuczne": "mdi:recycle", | ||
"papier i tektura": "mdi:file-outline", | ||
"szkło": "mdi:glass-fragile", | ||
"biodegradowalne": "mdi:leaf", | ||
} | ||
|
||
def get_json(url): | ||
#workaround to establish ssl connection to this host | ||
ssl_ctx = ssl.create_default_context() | ||
ssl_ctx.set_ciphers("DEFAULT") | ||
|
||
response = urllib.request.urlopen(API_URL % url, context=ssl_ctx) | ||
return json.loads(response.read().decode("utf-8")) | ||
|
||
def get_municipalities(): | ||
try: | ||
municipalities = get_json("dostepne_gminy/v5")["gminy"] | ||
except: | ||
municipalities = None | ||
|
||
return municipalities | ||
|
||
municipalities = get_municipalities() | ||
|
||
''' | ||
def EXTRA_INFO(): | ||
return [ | ||
{ | ||
"title": "Gmina %s [woj. %s, pow. %s]" % (m["gmina"], m["wojewodztwo"], m["powiat"]), | ||
"default_params": { | ||
"voivodeship": m["wojewodztwo"], | ||
"district": m["powiat"], | ||
"municipality": m["gmina"] | ||
} | ||
} for m in municipalities | ||
] | ||
''' | ||
|
||
class Source: | ||
def __init__(self, voivodeship: str, district: str, municipality: str, street: str): | ||
self.voivodeship = voivodeship | ||
self.district = district | ||
self.municipality = municipality | ||
self.street = street | ||
|
||
voivodeships_list = self.get_voivodeships_list() | ||
|
||
if voivodeship.lower() not in [v.lower() for v in voivodeships_list]: | ||
raise SourceArgumentNotFoundWithSuggestions( | ||
"voivodeship", | ||
voivodeship, | ||
suggestions = voivodeships_list, | ||
) | ||
|
||
districts_list = self.get_districts_list() | ||
|
||
if district.lower() not in [c.lower() for c in districts_list]: | ||
raise SourceArgumentNotFoundWithSuggestions( | ||
"district", | ||
district, | ||
suggestions = districts_list, | ||
) | ||
|
||
municipalities_list = self.get_municipalities_list() | ||
|
||
if municipality.lower() not in [m.lower() for m in municipalities_list]: | ||
raise SourceArgumentNotFoundWithSuggestions( | ||
"municipality", | ||
municipality, | ||
suggestions = municipalities_list, | ||
) | ||
|
||
streets_list = self.get_streets_list() | ||
|
||
if street.lower() not in [s.lower() for s in streets_list]: | ||
raise SourceArgumentNotFoundWithSuggestions( | ||
"street", | ||
street, | ||
suggestions = streets_list, | ||
) | ||
|
||
self.schedule = self.get_schedule() | ||
|
||
def get_voivodeships_list(self): | ||
return list(set(m["wojewodztwo"] for m in municipalities)) | ||
|
||
def get_districts_list(self): | ||
return list(set([m["powiat"] for m in municipalities if m["wojewodztwo"].lower() == self.voivodeship.lower()])) | ||
|
||
def get_municipalities_list(self): | ||
return [m["gmina"] for m in municipalities if m["wojewodztwo"].lower() == self.voivodeship.lower() and m["powiat"].lower() == self.district.lower()] | ||
|
||
def get_streets_list(self): | ||
try: | ||
streets = get_json("dostepne_gminy?wojewodztwo=%s&powiat=%s&gmina=%s" % (urllib.parse.quote(self.voivodeship), urllib.parse.quote(self.district), urllib.parse.quote(self.municipality)))["listaUlic"] | ||
streets = [s["ulica"] for s in streets] | ||
except: | ||
streets = None | ||
|
||
return streets | ||
|
||
def get_schedule(self): | ||
try: | ||
schedule = get_json("lista_terminow/v6?wojewodztwo=%s&powiat=%s&gmina=%s&ulica=%s" % (urllib.parse.quote(self.voivodeship), urllib.parse.quote(self.district), urllib.parse.quote(self.municipality), urllib.parse.quote(self.street)))["listaTerminow"] | ||
|
||
except: | ||
schedule = None | ||
|
||
return schedule | ||
|
||
def fetch(self) -> list[Collection]: | ||
|
||
entries = [] | ||
|
||
for entry in self.schedule: | ||
entries.append( | ||
Collection( | ||
date = datetime.datetime.strptime(entry["dataOdbioru"], "%Y-%m-%d").date(), | ||
t = entry["nazwaTypuSmieci"], | ||
icon = ICON_MAP.get(entry["nazwaTypuSmieci"], "mdi:trash-can"), | ||
) | ||
) | ||
|
||
return entries |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Kiedy śmieci | ||
|
||
Support for schedules provided by [Kiedy śmieci](https://kiedysmieci.info/), serving multiple municipalities, Poland. | ||
|
||
## Configuration via configuration.yaml | ||
|
||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: kiedysmieci_info | ||
args: | ||
voivodeship: voivodeship (województwo) | ||
district: district (powiat) | ||
municipality: municipality (gmina) | ||
street: street (ulica) | ||
|
||
``` | ||
### Configuration Variables | ||
**voivodeship** | ||
*(String) (required)* | ||
**district** | ||
*(String) (required)* | ||
**municipality** | ||
*(String) (required)* | ||
**street** | ||
*(String) (required)* | ||
## Example | ||
```yaml | ||
waste_collection_schedule: | ||
sources: | ||
- name: kiedysmieci_info | ||
args: | ||
voivodeship: podkarpackie | ||
district: sanocki | ||
municipality: Bukowsko | ||
street: Nadolany | ||
``` | ||
## How to get the source argument | ||
The parameters can be found by using the apps ([GooglePlay](https://play.google.com/store/apps/details?id=com.fxsystems.KiedySmieci_info), [AppStore](https://apps.apple.com/pl/app/kiedy-%C5%9Bmieci/id1539957094?l=pl)) or on the [website](https://kiedysmieci.info/index.html#harmonogram) by providing your municipality in the search form. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters