Skip to content

Commit

Permalink
add item properties images as bytes and link
Browse files Browse the repository at this point in the history
  • Loading branch information
Der-Henning committed Mar 28, 2023
1 parent b3d6fd6 commit ea519fa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 13 deletions.
15 changes: 9 additions & 6 deletions src/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
'url': '',
'cron': Cron('* * * * *'),
'title': 'New Magic Bags',
'body': '${{display_name}} - new amount: ${{items_available}} '
'- https://share.toogoodtogo.com/item/${{item_id}}'
'body': '${{display_name}} - new amount: '
'${{items_available}} - ${{link}}'
},
'console': {
'enabled': False,
Expand Down Expand Up @@ -77,7 +77,7 @@
'key': '',
'body': '{"value1": "${{display_name}}", '
'"value2": ${{items_available}}, '
'"value3": "https://share.toogoodtogo.com/item/${{item_id}}"}',
'"value3": "${{link}}"}',
'timeout': 60,
'cron': Cron('* * * * *')
},
Expand All @@ -86,11 +86,12 @@
'server': 'https://ntfy.sh',
'topic': None,
'title': 'New TGTG items',
'message': '${{display_name}} - New Amount: ${{items_available}} - '
'https://share.toogoodtogo.com/item/${{item_id}}',
'message': '${{display_name}} - New Amount: '
'${{items_available}} - ${{link}}',
'body': None,
'priority': 'default',
'tags': 'shopping,tgtg',
'click': 'https://share.toogoodtogo.com/item/${{item_id}}',
'click': '${{link}}',
'username': None,
'password': None,
'timeout': 60,
Expand Down Expand Up @@ -330,6 +331,7 @@ def _read_ini(self) -> None:
self._ini_get(config, "NTFY", "Topic", "ntfy.topic")
self._ini_get(config, "NTFY", "Title", "ntfy.title")
self._ini_get(config, "NTFY", "Message", "ntfy.message")
self._ini_get(config, "NTFY", "Body", "ntfy.body")
self._ini_get(config, "NTFY", "Priority", "ntfy.priority")
self._ini_get(config, "NTFY", "Tags", "ntfy.tags")
self._ini_get(config, "NTFY", "Click", "ntfy.click")
Expand Down Expand Up @@ -461,6 +463,7 @@ def _read_env(self) -> None:
self._env_get("NTFY_TOPIC", "ntfy.topic")
self._env_get("NTFY_TITLE", "ntfy.title")
self._env_get("NTFY_MESSAGE", "ntfy.message")
self._env_get("NTFY_BODY", "ntfy.body")
self._env_get("NTFY_PRIORITY", "ntfy.priority")
self._env_get("NTFY_TAGS", "ntfy.tags")
self._env_get("NTFY_CLICK", "ntfy.click")
Expand Down
36 changes: 33 additions & 3 deletions src/models/item.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import datetime
import logging
import re
from http import HTTPStatus

import humanize
import requests

from models.errors import MaskConfigurationError

ATTRS = ["item_id", "items_available", "display_name", "description",
"price", "currency", "pickupdate", "favorite", "rating",
"buffet", "item_category", "item_name", "packaging_option",
"pickup_location", "store_name", "item_logo", "item_cover",
"scanned_on"]
"scanned_on", "item_logo_bytes", "item_cover_bytes", "link"]

log = logging.getLogger('tgtg')


class Item():
Expand Down Expand Up @@ -77,14 +82,39 @@ def check_mask(text: str) -> None:
if not match.group(1) in ATTRS:
raise MaskConfigurationError(match.group(0))

@staticmethod
def get_image(url: str) -> bytes:
response = requests.get(url)
if not response.status_code == HTTPStatus.OK:
log.warning("Get Image Error: %s - %s",
response.status_code,
response.content)
return None
return response.content

@property
def item_logo_bytes(self) -> bytes:
return self.get_image(self.item_logo)

@property
def item_cover_bytes(self) -> bytes:
return self.get_image(self.item_cover)

@property
def link(self) -> str:
return f"https://share.toogoodtogo.com/item/{self.item_id}"

def unmask(self, text: str) -> str:
"""
Replaces variables with the current values.
"""
if text in ["${{item_logo_bytes}}", "${{item_cover_bytes}}"]:
matches = re.findall(r"\${{([a-zA-Z0-9_]+)}}", text)
return getattr(self, matches[0])
for match in re.finditer(r"\${{([a-zA-Z0-9_]+)}}", text):
if hasattr(self, match.group(1)):
text = text.replace(match.group(0), str(
getattr(self, match.group(1))))
val = getattr(self, match.group(1))
text = text.replace(match.group(0), str(val))
return text

@property
Expand Down
3 changes: 1 addition & 2 deletions src/notifiers/ntfy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ def __init__(self, config: Config):
self.topic = config.ntfy.get("topic")
self.title = config.ntfy.get("title", "tgtg")
self.message = config.ntfy.get("message")
self.body = config.ntfy.get("body")
self.priority = config.ntfy.get("priority", "default")
self.tags = config.ntfy.get("tags", "tgtg")
self.click = config.ntfy.get("click")
self.username = config.ntfy.get("username")
self.password = config.ntfy.get("password")
self.timeout = config.ntfy.get("timeout", 60)
self.cron = config.ntfy.get("cron")

self.headers = None
self.auth = None
self.body = None
self.method = "POST"
self.type = None

Expand Down
7 changes: 5 additions & 2 deletions src/notifiers/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ def _send(self, item: Item) -> None:
headers["Content-Type"] = self.type
if self.body:
body = item.unmask(self.body)
if self.type and 'json' in self.type:
if isinstance(body, bytes):
pass
elif self.type and 'json' in self.type:
body = json.dumps(json.loads(body.replace('\n', '\\n')))
log.debug("%s body: %s", self.name, body)
else:
body = body.encode('utf-8')
log.debug("%s body: %s", self.name, body)
log.debug("%s body: %s", self.name, body)
log.debug("%s headers: %s", self.name, headers)
res = requests.request(method=self.method, url=url,
timeout=self.timeout, data=body,
Expand Down

0 comments on commit ea519fa

Please sign in to comment.