Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved mail header and introduced localized decimal values #534

Merged
merged 4 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
535 changes: 292 additions & 243 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ version = "1.21.2"

[tool.poetry.dependencies]
apprise = "^1.4.0"
babel = "^2.16.0"
colorlog = "^6.7.0"
cron-descriptor = "^1.4.0"
discord = "^2.3.2"
Expand Down
18 changes: 10 additions & 8 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
aiohttp==3.9.5 ; python_version >= "3.9" and python_version < "3.13"
aiohappyeyeballs==2.3.7 ; python_version >= "3.9" and python_version < "3.13"
aiohttp==3.10.4 ; python_version >= "3.9" and python_version < "3.13"
aiosignal==1.3.1 ; python_version >= "3.9" and python_version < "3.13"
anyio==4.4.0 ; python_version >= "3.9" and python_version < "3.13"
apprise==1.8.0 ; python_version >= "3.9" and python_version < "3.13"
apprise==1.8.1 ; python_version >= "3.9" and python_version < "3.13"
async-timeout==4.0.3 ; python_version >= "3.9" and python_version < "3.11"
attrs==23.2.0 ; python_version >= "3.9" and python_version < "3.13"
attrs==24.2.0 ; python_version >= "3.9" and python_version < "3.13"
babel==2.16.0 ; python_version >= "3.9" and python_version < "3.13"
cachetools==5.3.3 ; python_version >= "3.9" and python_version < "3.13"
certifi==2024.7.4 ; python_version >= "3.9" and python_version < "3.13"
charset-normalizer==3.3.2 ; python_version >= "3.9" and python_version < "3.13"
Expand All @@ -13,16 +15,16 @@ colorlog==6.8.2 ; python_version >= "3.9" and python_version < "3.13"
cron-descriptor==1.4.3 ; python_version >= "3.9" and python_version < "3.13"
discord-py==2.4.0 ; python_version >= "3.9" and python_version < "3.13"
discord==2.3.2 ; python_version >= "3.9" and python_version < "3.13"
exceptiongroup==1.2.1 ; python_version >= "3.9" and python_version < "3.11"
exceptiongroup==1.2.2 ; python_version >= "3.9" and python_version < "3.11"
frozenlist==1.4.1 ; python_version >= "3.9" and python_version < "3.13"
googlemaps==4.10.0 ; python_version >= "3.9" and python_version < "3.13"
h11==0.14.0 ; python_version >= "3.9" and python_version < "3.13"
httpcore==1.0.5 ; python_version >= "3.9" and python_version < "3.13"
httpx==0.27.0 ; python_version >= "3.9" and python_version < "3.13"
humanize==4.10.0 ; python_version >= "3.9" and python_version < "3.13"
idna==3.7 ; python_version >= "3.9" and python_version < "3.13"
importlib-metadata==8.0.0 ; python_version >= "3.9" and python_version < "3.10"
markdown==3.6 ; python_version >= "3.9" and python_version < "3.13"
importlib-metadata==8.2.0 ; python_version >= "3.9" and python_version < "3.10"
markdown==3.7 ; python_version >= "3.9" and python_version < "3.13"
multidict==6.0.5 ; python_version >= "3.9" and python_version < "3.13"
oauthlib==3.2.2 ; python_version >= "3.9" and python_version < "3.13"
packaging==24.1 ; python_version >= "3.9" and python_version < "3.13"
Expand All @@ -31,11 +33,11 @@ prometheus-client==0.20.0 ; python_version >= "3.9" and python_version < "3.13"
pycron==3.0.0 ; python_version >= "3.9" and python_version < "3.13"
python-pushsafer==1.1 ; python_version >= "3.9" and python_version < "3.13"
python-telegram-bot[callback-data]==21.4 ; python_version >= "3.9" and python_version < "3.13"
pyyaml==6.0.1 ; python_version >= "3.9" and python_version < "3.13"
pyyaml==6.0.2 ; python_version >= "3.9" and python_version < "3.13"
requests-oauthlib==2.0.0 ; python_version >= "3.9" and python_version < "3.13"
requests==2.32.3 ; python_version >= "3.9" and python_version < "3.13"
sniffio==1.3.1 ; python_version >= "3.9" and python_version < "3.13"
typing-extensions==4.12.2 ; python_version >= "3.9" and python_version < "3.11"
urllib3==2.2.2 ; python_version >= "3.9" and python_version < "3.13"
yarl==1.9.4 ; python_version >= "3.9" and python_version < "3.13"
zipp==3.19.2 ; python_version >= "3.9" and python_version < "3.10"
zipp==3.20.0 ; python_version >= "3.9" and python_version < "3.10"
4 changes: 2 additions & 2 deletions tests/test_notifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ def test_smtp(test_item: Item, reservations: Reservations, favorites: Favorites,
assert body[2] == "From: user@example.com"
assert body[3] == "To: user@example.com"
assert body[4] == "Subject: New Magic Bags"
assert body[7] == 'Content-Type: text/html; charset="utf-8"'
assert body[11] == f"<b>=C3=81 =C3=AA</b> </br>Amount: {test_item.items_available}"
assert body[8] == 'Content-Type: text/html; charset="utf-8"'
assert body[12] == f"<b>=C3=81 =C3=AA</b> </br>Amount: {test_item.items_available}"


@pytest.fixture
Expand Down
21 changes: 20 additions & 1 deletion tgtg_scanner/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
from http import HTTPStatus
from typing import Any, Union

import babel.numbers
import humanize
import requests

from tgtg_scanner.errors import MaskConfigurationError
from tgtg_scanner.models import Config
from tgtg_scanner.models.location import DistanceTime, Location

ATTRS = [
Expand All @@ -17,10 +19,13 @@
"description",
"price",
"value",
"price_localized",
"value_localized",
"currency",
"pickupdate",
"favorite",
"rating",
"rating_localized",
"buffet",
"item_category",
"item_name",
Expand Down Expand Up @@ -52,7 +57,7 @@ class Item:
returns well formated data for notifications.
"""

def __init__(self, data: dict, location: Union[Location, None] = None):
def __init__(self, data: dict, config: Union[Config, None] = None, location: Union[Location, None] = None):
self.items_available = data.get("items_available", 0)
self.display_name = data.get("display_name", "-")
self.favorite = "Yes" if data.get("favorite", False) else "No"
Expand Down Expand Up @@ -91,6 +96,20 @@ def __init__(self, data: dict, location: Union[Location, None] = None):
self.scanned_on = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.location = location

# Localize attributes, first init with decoded values
self.price_localized = self.price
self.rating_localized = self.rating
self.value_localized = self.value
if config:
try:
self.rating_localized = babel.numbers.format_decimal(self.rating, locale=config.locale)

if self.currency != "-":
self.price_localized = babel.numbers.format_currency(self.price, self.currency, locale=config.locale)
self.value_localized = babel.numbers.format_currency(self.value, self.currency, locale=config.locale)
except babel.numbers.NumberFormatError:
pass

@staticmethod
def _datetimeparse(datestr: str) -> datetime.datetime:
"""
Expand Down
2 changes: 2 additions & 0 deletions tgtg_scanner/notifiers/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import formatdate
from smtplib import SMTPException, SMTPServerDisconnected
from typing import Union

Expand Down Expand Up @@ -108,6 +109,7 @@ def _send_mail(self, subject: str, html: str, item_id: int) -> None:

message["To"] = ", ".join(recipients)
message["Subject"] = subject
message["Date"] = formatdate(localtime=True)
message.attach(MIMEText(html, "html", "utf-8"))
body = message.as_string()
self._stay_connected()
Expand Down
6 changes: 3 additions & 3 deletions tgtg_scanner/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def _get_test_item(self) -> Item:
return items[0]
items = sorted(
[
Item(item, self.location)
Item(item, self.config, self.location)
for item in self.tgtg_client.get_items(favorites_only=False, latitude=53.5511, longitude=9.9937, radius=50)
],
key=lambda x: x.items_available,
Expand All @@ -100,7 +100,7 @@ def _job(self) -> None:
try:
if item_id != "":
item_dict = self.tgtg_client.get_item(item_id)
items.append(Item(item_dict, self.location))
items.append(Item(item_dict, self.config, self.location))
except TgtgAPIError as err:
log.error(err)
items += self._get_favorites()
Expand Down Expand Up @@ -133,7 +133,7 @@ def _get_favorites(self) -> list[Item]:
except TgtgAPIError as err:
log.error(err)
return []
return [Item(item, self.location) for item in items]
return [Item(item, self.config, self.location) for item in items]

def _check_item(self, item: Item) -> None:
"""
Expand Down
2 changes: 1 addition & 1 deletion wiki/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ You can combine multiple crons as semicolon separated list.
| ScheduleCron | SCHEDULE_CRON | run only on schedule | `* * * * *` |
| ItemIDs | ITEM_IDS | **Depreciated!** comma-separated list of additional (none favorite) items to scan | |
| Metrics | METRICS | enable Prometheus metrics HTTP server | `false` |
| MetricsPort | METRICS_PORTS | port for metrics server | `8000` |
| MetricsPort | METRICS_PORT | port for metrics server | `8000` |
| DisableTests | DISABLE_TESTS | disable test notifications on startup | `false` |
| Quiet | QUIET | minimal console output | `false` |
| Locale | LOCALE | localization | `en_US` |
Expand Down