Skip to content

Commit

Permalink
Update for updated TGTG API
Browse files Browse the repository at this point in the history
  • Loading branch information
Der-Henning committed Nov 15, 2021
1 parent f9836aa commit 5b126b3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion requirements.dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-r requirements.txt
altgraph==0.17.2
pyinstaller==4.5.1
pyinstaller==4.7
pyinstaller-hooks-contrib==2021.3
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
certifi==2021.10.8
charset-normalizer==2.0.7
idna==3.3
packaging==21.0
pyparsing==3.0.1
python-pushsafer==0.4
packaging==21.2
pyparsing==2.4.7
python-pushsafer==1.0
requests==2.26.0
urllib3==1.26.7
6 changes: 3 additions & 3 deletions src/notifiers/pushSafer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pushsafer import init, Client
from pushsafer import Client
from models import Item, Config, PushSaferConfigurationError
import logging

Expand All @@ -13,11 +13,11 @@ def __init__(self, config: Config):
if self.enabled and (not self.key or not self.deviceId):
raise PushSaferConfigurationError()
if self.enabled:
init(self.key)
self.client = Client(self.key)

def send(self, item: Item):
if self.enabled:
log.info("Sending PushSafer Notification")
message = f"New Amount: {item.items_available}"
Client("").send_message(message, item.display_name, self.deviceId,
self.client.send_message(message, item.display_name, self.deviceId,
"", "", "", "", "", "", "", "", "", "", "", "", "")
43 changes: 25 additions & 18 deletions src/tgtg/tgtgClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
log = logging.getLogger('tgtg')
BASE_URL = "https://apptoogoodtogo.com/api/"
API_ITEM_ENDPOINT = "item/v7/"
LOGIN_ENDPOINT = "auth/v1/loginByEmail"
LOGIN_ENDPOINT = "auth/v2/loginByEmail"
SIGNUP_BY_EMAIL_ENDPOINT = "auth/v2/signUpByEmail"
REFRESH_ENDPOINT = "auth/v1/token/refresh"
ALL_BUSINESS_ENDPOINT = "map/v1/listAllBusinessMap"
Expand All @@ -30,6 +30,7 @@ def __init__(
email=None,
password=None,
access_token=None,
refresh_token=None,
user_id=None,
user_agent=None,
language="en-UK",
Expand All @@ -43,15 +44,11 @@ def __init__(
self.password = password

self.access_token = access_token
if self.access_token is not None:
log.warn("'access_token' is deprecated; use 'email' and 'password'")
self.refresh_token = None
self.refresh_token = refresh_token
self.user_id = user_id
self.last_time_token_refreshed = None
self.access_token_lifetime = access_token_lifetime

self.user_id = user_id
if self.user_id is not None:
log.warn("'user_id' is deprecated; use 'email' and 'password'")
self.user_agent = user_agent if user_agent else random.choice(USER_AGENTS)
self.language = language
self.proxies = proxies
Expand All @@ -62,14 +59,18 @@ def _get_url(self, path):

@property
def _headers(self):
headers = {"user-agent": self.user_agent, "accept-language": self.language}
headers = {
"user-agent": self.user_agent,
"accept-language": self.language,
"Accept-Encoding": "gzip"
}
if self.access_token:
headers["authorization"] = f"Bearer {self.access_token}"
return headers

@property
def _already_logged(self):
return bool(self.access_token and self.user_id)
return bool(self.access_token and self.refresh_token and self.user_id)

def _refresh_token(self):
if (
Expand All @@ -93,15 +94,21 @@ def _refresh_token(self):
else:
raise TgtgAPIError(response.status_code, response.content)

def _login(self):
def login(self):
if not (
self.password
and self.email
or self.access_token
and self.refresh_token
and self.user_id
):
raise TypeError(
"You must provide at least email and password or access_token, refresh_token and user_id"
)

if self._already_logged:
self._refresh_token()
else:
if not self.email or not self.password:
raise ValueError(
"You must fill email and password or access_token and user_id"
)

response = requests.post(
self._get_url(LOGIN_ENDPOINT),
headers=self._headers,
Expand Down Expand Up @@ -141,7 +148,7 @@ def get_items(
hidden_only=False,
we_care_only=False,
):
self._login()
self.login()

# fields are sorted like in the app
data = {
Expand Down Expand Up @@ -174,7 +181,7 @@ def get_items(
raise TgtgAPIError(response.status_code, response.content)

def get_item(self, item_id):
self._login()
self.login()
response = requests.post(
urljoin(self._get_url(API_ITEM_ENDPOINT), str(item_id)),
headers=self._headers,
Expand All @@ -188,7 +195,7 @@ def get_item(self, item_id):
raise TgtgAPIError(response.status_code, response.content)

def set_favorite(self, item_id, is_favorite):
self._login()
self.login()
response = requests.post(
urljoin(self._get_url(API_ITEM_ENDPOINT), f"{item_id}/setFavorite"),
headers=self._headers,
Expand Down

0 comments on commit 5b126b3

Please sign in to comment.