mirror of
https://github.com/sascha-hemi/hacs_waste_collection_schedule.git
synced 2026-03-21 04:06:03 +01:00
fixing picking last schedule for multiple schedules provided
Signed-off-by: pawel.hulek <pawelhulek@gmail.com>
This commit is contained in:
@@ -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 "")
|
||||
@@ -1,85 +1,68 @@
|
||||
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")
|
||||
|
||||
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()
|
||||
|
||||
schedules_raw = schedules_response.get('schedules')
|
||||
schedules_descriptions_dict = dict()
|
||||
schedules_descriptions_raw = schedules_response.get('scheduleDescription')
|
||||
|
||||
for sd in schedules_descriptions_raw:
|
||||
schedules_descriptions_dict[sd.get('id')] = sd
|
||||
|
||||
schedules = []
|
||||
for s in schedules_raw:
|
||||
z = s.copy()
|
||||
get = schedules_descriptions_dict.get(s.get('scheduleDescriptionId'))
|
||||
z['name'] = get.get("name")
|
||||
schedules.append(z)
|
||||
schedule_periods_data = Ecoharmonogram.fetch_scheduled_periods(town)
|
||||
schedule_periods = schedule_periods_data.get("schedulePeriods")
|
||||
|
||||
entries = []
|
||||
for sch in schedules:
|
||||
days = sch.get("days").split(';')
|
||||
month = sch.get("month")
|
||||
year = sch.get("year")
|
||||
for d in days:
|
||||
entries.append(
|
||||
Collection(
|
||||
datetime.date(int(year), int(month), int(d)),
|
||||
sch.get('name')
|
||||
)
|
||||
)
|
||||
for sp in schedule_periods:
|
||||
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')
|
||||
|
||||
for sd in schedules_descriptions_raw:
|
||||
schedules_descriptions_dict[sd.get('id')] = sd
|
||||
|
||||
schedules = []
|
||||
for sr in schedules_raw:
|
||||
z = sr.copy()
|
||||
get = schedules_descriptions_dict.get(sr.get('scheduleDescriptionId'))
|
||||
z['name'] = get.get("name")
|
||||
schedules.append(z)
|
||||
|
||||
entries = []
|
||||
for sch in schedules:
|
||||
days = sch.get("days").split(';')
|
||||
month = sch.get("month")
|
||||
year = sch.get("year")
|
||||
for d in days:
|
||||
entries.append(
|
||||
Collection(
|
||||
datetime.date(int(year), int(month), int(d)),
|
||||
sch.get('name')
|
||||
)
|
||||
)
|
||||
if self.additional_sides_matcher_input != "":
|
||||
return entries
|
||||
|
||||
return entries
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user