fixing picking last schedule for multiple schedules provided

Signed-off-by: pawel.hulek <pawelhulek@gmail.com>
This commit is contained in:
pawel.hulek
2022-10-22 23:16:24 +02:00
parent 2c3c39928b
commit d799cd0cf8
3 changed files with 142 additions and 64 deletions

View File

@@ -0,0 +1,66 @@
import sys
import requests
towns_url = "https://ecoharmonogram.pl/api/api.php?action=getTowns"
scheduled_periods_url = "https://ecoharmonogram.pl/api/api.php?action=getSchedulePeriods"
streets_url = "https://ecoharmonogram.pl/api/api.php?action=getStreets"
schedules_url = "https://ecoharmonogram.pl/api/api.php?action=getSchedules"
headers = {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
}
class Ecoharmonogram:
@staticmethod
def fetch_schedules(sp, street):
schedules_response = requests.get(
schedules_url + "&streetId=" + street.get("id") + "&schedulePeriodId=" + sp.get("id"),
headers=headers)
schedules_response.encoding = "utf-8-sig"
schedules_response = schedules_response.json()
return schedules_response
@staticmethod
def fetch_streets(sp, town, street, house_number):
streets_response = requests.get(
streets_url + "&streetName=" + str(street) + "&number=" + str(
house_number) + "&townId=" + town.get("id") +
"&schedulePeriodId=" + sp.get("id"), headers=headers)
streets_response.encoding = "utf-8-sig"
streets = streets_response.json().get("streets")
return streets
@staticmethod
def fetch_scheduled_periods(town):
scheduled_perionds_response = requests.get(scheduled_periods_url + "&townId=" + town.get("id"), headers=headers)
scheduled_perionds_response.encoding = "utf-8-sig"
schedule_periods_data = scheduled_perionds_response.json()
return schedule_periods_data
@staticmethod
def fetch_town():
town_response = requests.get(towns_url, headers=headers)
town_response.encoding = "utf-8-sig"
town_data = town_response.json()
return town_data
@staticmethod
def print_possible_sides(town_input, street_input, house_number_input):
town_data = Ecoharmonogram.fetch_town()
matching_towns = filter(lambda x: town_input.lower() in x.get('name').lower(), town_data.get('towns'))
town = list(matching_towns)[0]
schedule_periods_data = Ecoharmonogram.fetch_scheduled_periods(town)
schedule_periods = schedule_periods_data.get("schedulePeriods")
for sp in schedule_periods:
streets = Ecoharmonogram.fetch_streets(sp, town, street_input, house_number_input)
for street in streets:
print(street.get("sides"))
if __name__ == '__main__':
Ecoharmonogram.print_possible_sides(sys.argv[1], sys.argv[2] or "", sys.argv[3] or "")

View File

@@ -1,60 +1,41 @@
import datetime
from ..collection import Collection
import requests
from ..service.EcoHarmonogramPL import Ecoharmonogram
DESCRIPTION = "Source for ecoharmonogram.pl"
URL = "ecoharmonogram.pl"
TEST_CASES = {
"TestName": {"town_input": "Krzeszowice", "street_input": "Wyki", "house_number_input": ""}
"Simple test case": {"town": "Krzeszowice", "street": "Wyki", "house_number": ""},
"Sides multi test case": {"town": "Częstochowa", "street": "Boczna", "additional_sides_matcher": "wie"},
"Sides test case": {"town": "Częstochowa", "street": "Azaliowa", "house_number": "1",
"additional_sides_matcher": "jedn"}
}
TITLE = "ecoharmonogram.pl"
headers = {
'Content-Type': 'application/json; charset=utf-8',
'Accept': 'application/json',
}
towns_url = "https://ecoharmonogram.pl/api/api.php?action=getTowns"
scheduled_periods_url = "https://ecoharmonogram.pl/api/api.php?action=getSchedulePeriods"
streets_url = "https://ecoharmonogram.pl/api/api.php?action=getStreets"
schedules_url = "https://ecoharmonogram.pl/api/api.php?action=getSchedules"
class Source:
def __init__(self, town, street="", house_number=""):
def __init__(self, town, street="", house_number="", additional_sides_matcher=""):
self.town_input = town
self.street_input = street
self.house_number_input = house_number
self.additional_sides_matcher_input = additional_sides_matcher
def fetch(self):
town_response = requests.get(towns_url, headers=headers)
town_response.encoding = "utf-8-sig"
town_date = town_response.json()
matching_towns = filter(lambda x: self.town_input.lower() in x.get('name').lower(), town_date.get('towns'))
town_data = Ecoharmonogram.fetch_town()
matching_towns = filter(lambda x: self.town_input.lower() in x.get('name').lower(), town_data.get('towns'))
town = list(matching_towns)[0]
scheduled_perionds_response = requests.get(scheduled_periods_url + "&townId=" + town.get("id"), headers=headers)
scheduled_perionds_response.encoding = "utf-8-sig"
town_date = scheduled_perionds_response.json()
schedule_periods = town_date.get("schedulePeriods")
schedule_periods_data = Ecoharmonogram.fetch_scheduled_periods(town)
schedule_periods = schedule_periods_data.get("schedulePeriods")
entries = []
for sp in schedule_periods:
streets_response = requests.get(
streets_url + "&streetName=" + str(self.street_input) + "&number=" + str(
self.house_number_input) + "&townId=" + town.get("id") +
"&schedulePeriodId=" + sp.get("id"), headers=headers)
streets_response.encoding = "utf-8-sig"
streets = streets_response.json().get("streets")
for s in streets:
schedules_response = requests.get(
schedules_url + "&streetId=" + s.get("id") + "&schedulePeriodId=" + sp.get("id"),
headers=headers)
schedules_response.encoding = "utf-8-sig"
schedules_response = schedules_response.json()
streets = Ecoharmonogram.fetch_streets(sp, town, self.street_input, self.house_number_input)
for street in streets:
if self.additional_sides_matcher_input.lower() in street.get("sides").lower():
schedules_response = Ecoharmonogram.fetch_schedules(sp, street)
schedules_raw = schedules_response.get('schedules')
schedules_descriptions_dict = dict()
schedules_descriptions_raw = schedules_response.get('scheduleDescription')
@@ -63,9 +44,9 @@ class Source:
schedules_descriptions_dict[sd.get('id')] = sd
schedules = []
for s in schedules_raw:
z = s.copy()
get = schedules_descriptions_dict.get(s.get('scheduleDescriptionId'))
for sr in schedules_raw:
z = sr.copy()
get = schedules_descriptions_dict.get(sr.get('scheduleDescriptionId'))
z['name'] = get.get("name")
schedules.append(z)
@@ -81,5 +62,7 @@ class Source:
sch.get('name')
)
)
if self.additional_sides_matcher_input != "":
return entries
return entries

View File

@@ -14,6 +14,7 @@ waste_collection_schedule:
town: town
street: street
house_number: house_number
additional_sides_matcher: additional matching parameter
```
## Example
@@ -25,10 +26,38 @@ waste_collection_schedule:
args:
town: Krzeszowice
street: Wyki
additional_sides_matcher: Nieruchomości wielolokalowe
```
Worth to mention that street and house_number are optional parameters if your city doesn't require one.
## Keep in mind
Ecoharmonogram data is filled by each city, so that some towns doesn't require street name while some does.
Also, together with garbage collection schedule you may see also payment reminders or other events.
## How to check additional matchers
If your community is providing schedules for separate side types - like house/company/flats you will need to find
additinal matching parameter:
to do that you should execute the following script:
```bash
python3 ./custom_components/waste_collection_schedule/waste_collection_schedule/service/EcoHarmonogramPL.py town_name street_name house_number
```
for example:
```bash
python3 ./custom_components/waste_collection_schedule/waste_collection_schedule/service/EcoHarmonogramPL.py Częstochowa Boczna 1
```
This should print you following types of additional match:
`
Zawodzie-Dąbie - Zab. jedn.
Zawodzie-Dąbie - Zab. wiel.
`
You can also try to match by shorter strings like `jedn` as this is quite popular part of phrase for standalone housing
schedules
The parameter is optional - if not used last type of housing will be picked as it was working before