Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Commit

Permalink
feat: add message prefix (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
sibalzer committed Jun 21, 2021
1 parent 08ec0a9 commit d389f5f
Show file tree
Hide file tree
Showing 16 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions config.example.ini
Expand Up @@ -41,6 +41,8 @@ enable=false
service_uris=discord://webhook_id/webhook_token,matrix://hostname

[ADVANCED]
;;; Ein Präfix (optional), das der gesendeten Nachricht vorangestellt wird.
custom_message_prefix=Impfbot
;;; Wartezeit zwischen den Abfragen eine zu kleine Wartezeit führt (vermutlich) zu einem IP-Ban (Default: 1min, kann aber empirisch verkleinert werden)
cooldown_between_requests=60
;;; Wartezeit zwischen fehlgeschlagenen Versuchen. Bei jedem weiteren wird die Wartezeit nochmal hinzuaddiert. D.h. fünf Fehlschläge = Wartezeit von 5*30s bis zum nächsen Aufruf
Expand Down
2 changes: 2 additions & 0 deletions src/config_generator.py
Expand Up @@ -44,6 +44,8 @@ def init_input(config_dict):
for item in NOTIFIERS:
config_dict[item.upper()] = {}
config_dict["ADVANCED"] = {}
config_dict["ADVANCED"]["custom_message_prefix"] = str(
SKELETON["ADVANCED"]["custom_message_prefix"]["default"])
config_dict["ADVANCED"]["cooldown_between_requests"] = str(
SKELETON["ADVANCED"]["cooldown_between_requests"]["default"])
config_dict["ADVANCED"]["cooldown_between_failed_requests"] = str(
Expand Down
5 changes: 5 additions & 0 deletions src/config_skeleton.py
Expand Up @@ -104,6 +104,11 @@
},
},
"ADVANCED": {
"custom_message_prefix": {
"default": "Impfbot",
"type": str,
"regex": USER_AGENT_REGEX
},
"cooldown_between_requests": {
"default": 30,
"type": float,
Expand Down
2 changes: 1 addition & 1 deletion src/impfbot.py
Expand Up @@ -49,7 +49,7 @@ def check_for_slot() -> None:

log.info(
f"Free slot! ({free_slots}) {vaccine_name}/{vaccine_type} Appointment date: {first_appoinment_date}")
msg = f"Freier Impfslot ({free_slots})! {vaccine_name}/{vaccine_type} verfügbar ab dem {first_appoinment_date}"
msg = f"[{settings.CUSTOM_MESSAGE_PREFIX}] Freier Impfslot ({free_slots})! {vaccine_name}/{vaccine_type} verfügbar ab dem {first_appoinment_date}"

alert(msg)
sleep(settings.COOLDOWN_AFTER_SUCCESS)
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-advanced.ini
Expand Up @@ -3,6 +3,7 @@ birthdate=23.6.1912
zip_code=49042

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=60
cooldown_between_failed_requests=10
cooldown_after_ip_ban=10800
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-invalid-advanced.ini
Expand Up @@ -24,6 +24,7 @@ enable=true
service_uris=discord://webhook_id/webhook_token,matrix://hostname

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=60a
cooldown_between_failed_requests=10a
cooldown_after_ip_ban=10800a
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-invalid-apprise.ini
Expand Up @@ -24,6 +24,7 @@ enable=true
service_uris=discord://webhook_id/webhook_token,matrix://hostname

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=60
cooldown_between_failed_requests=10
cooldown_after_ip_ban=10800
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-invalid-birthdate.ini
Expand Up @@ -24,6 +24,7 @@ enable=true
service_uris=discord://webhook_id/webhook_token,matrix://hostname

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=60
cooldown_between_failed_requests=10
cooldown_after_ip_ban=10800
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-invalid-email.ini
Expand Up @@ -24,6 +24,7 @@ enable=true
service_uris=discord://webhook_id/webhook_token,matrix://hostname

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=60
cooldown_between_failed_requests=10
cooldown_after_ip_ban=10800
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-invalid-telegram.ini
Expand Up @@ -24,6 +24,7 @@ enable=true
service_uris=discord://webhook_id/webhook_token,matrix://hostname

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=60
cooldown_between_failed_requests=10
cooldown_after_ip_ban=10800
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-invalid-zip-code.ini
Expand Up @@ -20,6 +20,7 @@ chat_ids=123456789,987654321
enable=true

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=60
cooldown_between_failed_requests=10
cooldown_after_ip_ban=10800
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-no-alerts.ini
Expand Up @@ -19,6 +19,7 @@ chat_id=123456789,987654321
open_browser=false

[ADVANCED]
custom_message_prefix=Impfbot
sleep_between_requests_in_s=1
sleep_between_failed_requests_in_s=2
sleep_after_ipban_in_min=3
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config-valid.ini
Expand Up @@ -24,6 +24,7 @@ enable=true
service_uris=discord://webhook_id/webhook_token,matrix://hostname

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=60
cooldown_between_failed_requests=10
cooldown_after_ip_ban=10800
Expand Down
1 change: 1 addition & 0 deletions tests/configs/test-config.ini
Expand Up @@ -20,6 +20,7 @@ chat_ids=123456789,987654321
enable=true

[ADVANCED]
custom_message_prefix=Impfbot
cooldown_between_requests=1
cooldown_between_failed_requests=2
cooldown_after_ip_ban=3
Expand Down
2 changes: 1 addition & 1 deletion tests/test_impfbot.py
Expand Up @@ -93,7 +93,7 @@ def test_check_for_slot_in_stock(log_info_mock,
impfbot.check_for_slot()

alert_mock.assert_called_once_with(
"Freier Impfslot (253)! AstraZeneca/Vector verfügbar ab dem 31.05.2021")
"[Impfbot] Freier Impfslot (253)! AstraZeneca/Vector verfügbar ab dem 31.05.2021")
log_info_mock.assert_called_once_with(
"Free slot! (253) AstraZeneca/Vector Appointment date: 31.05.2021")
log_warning_mock.assert_not_called()
Expand Down
11 changes: 8 additions & 3 deletions tests/test_settings.py
Expand Up @@ -45,6 +45,7 @@ def test_valid(log_info_mock,
assert settings.WEBBROWSER_ENABLE is True

# ADVANCED
assert settings.CUSTOM_MESSAGE_PREFIX == "Impfbot"
assert settings.COOLDOWN_BETWEEN_REQUESTS == 60
assert settings.COOLDOWN_BETWEEN_FAILED_REQUESTS == 10
assert settings.COOLDOWN_AFTER_IP_BAN == 10800
Expand Down Expand Up @@ -123,6 +124,7 @@ def test_invalid_advanced(log_info_mock,
log_error_mock.assert_not_called()

# ADVANCED
assert settings.CUSTOM_MESSAGE_PREFIX == "Impfbot"
assert settings.COOLDOWN_BETWEEN_REQUESTS == 30
assert settings.COOLDOWN_BETWEEN_FAILED_REQUESTS == 5
assert settings.COOLDOWN_AFTER_IP_BAN == 10800
Expand All @@ -142,10 +144,11 @@ def test_old(log_info_mock,

# Check output
assert log_info_mock.call_count == 1
assert log_warning_mock.call_count == 11
assert log_warning_mock.call_count == 12
for msg in log_warning_mock.call_args_list:
assert (" is depracated please use: " in msg.args[0]
or "[EMAIL] 'user' is missing; set sender as user" in msg.args[0])
or "[EMAIL] 'user' is missing; set sender as user" in msg.args[0]
or "[ADVANCED] 'custom_message_prefix' not set. Using default: 'Impfbot'" in msg.args[0])
log_error_mock.assert_not_called()

# COMMON
Expand All @@ -171,6 +174,7 @@ def test_old(log_info_mock,
assert settings.WEBBROWSER_ENABLE is True

# ADVANCED
assert settings.CUSTOM_MESSAGE_PREFIX == "Impfbot"
assert settings.COOLDOWN_BETWEEN_REQUESTS == 1
assert settings.COOLDOWN_BETWEEN_FAILED_REQUESTS == 2
assert settings.COOLDOWN_AFTER_IP_BAN == 3*60
Expand All @@ -190,7 +194,7 @@ def test_missing_optional(log_info_mock,

# Check output
assert log_info_mock.call_count == 4
assert log_warning_mock.call_count == 7
assert log_warning_mock.call_count == 8
log_error_mock.assert_not_called()

# EMAIL
Expand All @@ -200,6 +204,7 @@ def test_missing_optional(log_info_mock,
# WEBBROWSER
assert settings.WEBBROWSER_ENABLE is False
# ADVANCED
assert settings.CUSTOM_MESSAGE_PREFIX == "Impfbot"
assert settings.COOLDOWN_BETWEEN_REQUESTS == 30
assert settings.COOLDOWN_BETWEEN_FAILED_REQUESTS == 5
assert settings.COOLDOWN_AFTER_IP_BAN == 3*60*60
Expand Down

0 comments on commit d389f5f

Please sign in to comment.