diff --git a/.dockerignore b/.dockerignore index 9cebd33..5593dc1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,6 +2,7 @@ env/ __pycache__ src/database.sqlite src/config.ini +config.ini import.py check.py Dockerfile \ No newline at end of file diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 381202b..683b4e1 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -1,2 +1,4 @@ ## v0.1.0 [2023-10-21] -- Wersja beta \ No newline at end of file +- Wersja beta +## v0.1.1 [2023-10-22] +- Dodano obsługe błędnego logowania \ No newline at end of file diff --git a/src/config.yaml b/src/config.yaml index d8bae43..99aa6a6 100644 --- a/src/config.yaml +++ b/src/config.yaml @@ -1,6 +1,6 @@ name: "Energa meter" description: "Energa meter addon" -version: "0.1.0" +version: "0.1.1" slug: "energa_meter" init: false options: diff --git a/src/main.py b/src/main.py index 9a6c2c1..8cd9209 100644 --- a/src/main.py +++ b/src/main.py @@ -20,15 +20,16 @@ def main(): mojLicznik = MojLicznik() print(f"Logowanie...", username) mojLicznik.login(username, password) - print(f"Aktualizacja liczników...") - mojLicznik.uppdate_measurments() - print(f"Wyszukiwanie najstarszych danych...") - mojLicznik.update_first_date() - print(f"Pobieranie danych...") - mojLicznik.download_charts(True) - mojLicznik.update_last_days() - mojLicznik.set_daily_zones() - mojLicznik.logout() + if mojLicznik.loginStatus: + print(f"Aktualizacja liczników...") + mojLicznik.uppdate_measurments() + print(f"Wyszukiwanie najstarszych danych...") + mojLicznik.update_first_date() + print(f"Pobieranie danych...") + mojLicznik.download_charts(True) + mojLicznik.update_last_days() + mojLicznik.set_daily_zones() + mojLicznik.logout() if __name__ == "__main__": main() \ No newline at end of file diff --git a/src/moj_licznik.py b/src/moj_licznik.py index 1c2fee1..4450756 100644 --- a/src/moj_licznik.py +++ b/src/moj_licznik.py @@ -110,39 +110,50 @@ class MojLicznik: try: response = self.session.post(login_url, data=login_data) response.raise_for_status() - self.loginStatus = True - print(f"Zalogowano") + + except HTTPError as e: print(f"Wystąpił błąd HTTP: {e}") soup = BeautifulSoup(response.text, 'html.parser') - select_elements = soup.find_all('script', type='text/javascript') - meter_isd = [] - for el in select_elements: - pattern = r"id:\s+(\d+),[\s\S]*?ppe:\s+'([\d\s]+)',[\s\S]*?tariffCode:\s+'([^']+)',[\s\S]*?name:\s+'([^']+)'" - matches = re.search(pattern, el.text) - if matches: - id_value = matches.group(1) - ppe_value = matches.group(2) - tariffCode_value = matches.group(3) - name_value = matches.group(4) - meter_isd.append(id_value) - retrieved_record = PPETable.get_or_none(id=id_value) - if retrieved_record: - print(f"Licznik {id_value} istnieje w systemie.") - if not retrieved_record.is_active: - retrieved_record.is_active = True - retrieved_record.save() - else: - print(f"Licznik {id_value} nie istnieje w systemie.") - data = PPETable.create( - id=id_value, - ppe=ppe_value, - tariffCode=tariffCode_value, - name=name_value - ) - update_query = PPETable.update(is_active=0).where(PPETable.id.not_in(meter_isd)) - update_query.execute() + + login_error_text = 'Użytkownik lub hasło niepoprawne' + login_error = soup.find('div', text=login_error_text) + + if login_error: + self.loginStatus = False + print(login_error_text) + else: + self.loginStatus = True + print(f"Zalogowano") + + select_elements = soup.find_all('script', type='text/javascript') + meter_isd = [] + for el in select_elements: + pattern = r"id:\s+(\d+),[\s\S]*?ppe:\s+'([\d\s]+)',[\s\S]*?tariffCode:\s+'([^']+)',[\s\S]*?name:\s+'([^']+)'" + matches = re.search(pattern, el.text) + if matches: + id_value = matches.group(1) + ppe_value = matches.group(2) + tariffCode_value = matches.group(3) + name_value = matches.group(4) + meter_isd.append(id_value) + retrieved_record = PPETable.get_or_none(id=id_value) + if retrieved_record: + print(f"Licznik {id_value} istnieje w systemie.") + if not retrieved_record.is_active: + retrieved_record.is_active = True + retrieved_record.save() + else: + print(f"Licznik {id_value} nie istnieje w systemie.") + data = PPETable.create( + id=id_value, + ppe=ppe_value, + tariffCode=tariffCode_value, + name=name_value + ) + update_query = PPETable.update(is_active=0).where(PPETable.id.not_in(meter_isd)) + update_query.execute() def logout(self): logout_url = f"{self.meter_url}/dp/MainLogout.go"