Skip to content

Commit

Permalink
Merge pull request #242 from Der-Henning/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Der-Henning committed Jan 15, 2023
2 parents 440a0d9 + 90b6f25 commit 51c9a58
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions DOCKER_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Readme, source and documentation on [https://github.com/Der-Henning/tgtg](https:

- [`edge`](https://github.com/Der-Henning/tgtg/blob/main/Dockerfile)
- [`edge-alpine`](https://github.com/Der-Henning/tgtg/blob/main/Dockerfile.alpine)
- [`v1`, `v1.14`, `v1.14.4`, `latest`](https://github.com/Der-Henning/tgtg/blob/v1.14.4/Dockerfile)
- [`v1-alpine`, `v1.14-alpine`, `v1.14.4-alpine`, `latest-alpine`](https://github.com/Der-Henning/tgtg/blob/v1.14.4/Dockerfile.alpine)
- [`v1`, `v1.14`, `v1.14.5`, `latest`](https://github.com/Der-Henning/tgtg/blob/v1.14.5/Dockerfile)
- [`v1-alpine`, `v1.14-alpine`, `v1.14.5-alpine`, `latest-alpine`](https://github.com/Der-Henning/tgtg/blob/v1.14.5/Dockerfile.alpine)

# Quick Start

Expand Down
2 changes: 1 addition & 1 deletion src/_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__title__ = "TGTG Scanner"
__description__ = "Provides notifications for TGTG magic bags"
__version__ = "1.14.4"
__version__ = "1.14.5"
__author__ = "Henning Merklinger"
__author_email__ = "henning.merklinger@gmail.com"
__license__ = "GPL"
Expand Down
72 changes: 44 additions & 28 deletions src/tgtg/tgtg_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import random
import re
import time
from datetime import datetime, timedelta
from datetime import datetime
from http import HTTPStatus
from typing import List
from urllib.parse import urljoin
Expand Down Expand Up @@ -43,6 +43,33 @@
)


class TgtgSession(requests.Session):
http_adapter = HTTPAdapter(
max_retries=Retry(
total=5,
status_forcelist=[429, 500, 502, 503, 504],
allowed_methods=["GET", "POST"],
backoff_factor=1,
)
)

def __init__(self, headers: dict = {}, timeout: int = None,
proxies: dict = None, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.mount("https://", self.http_adapter)
self.mount("http://", self.http_adapter)
self.headers = headers
self.timeout = timeout
self.proxies = proxies

def send(self, request, **kwargs):
for key in ["timeout", "proxies"]:
val = kwargs.get(key)
if val is None and hasattr(self, key):
kwargs[key] = getattr(self, key)
return super().send(request, **kwargs)


class TgtgClient:
def __init__(
self,
Expand Down Expand Up @@ -78,18 +105,7 @@ def __init__(
self.language = language
self.proxies = proxies
self.timeout = timeout
self.http_adapter = HTTPAdapter(
max_retries=Retry(
total=5,
status_forcelist=[429, 500, 502, 503, 504],
allowed_methods=["GET", "POST"],
backoff_factor=1,
)
)
self.session = requests.Session()
self.session.mount("https://", self.http_adapter)
self.session.mount("http://", self.http_adapter)
self.session.headers = self._headers
self.session = self._create_session()

self.captcha_error_count = 0

Expand All @@ -99,6 +115,11 @@ def __del__(self) -> None:
def _get_url(self, path) -> str:
return urljoin(self.base_url, path)

def _create_session(self) -> TgtgSession:
return TgtgSession(self._headers,
self.timeout,
self.proxies)

def get_credentials(self) -> dict:
"""Returns current tgtg api credentials.
Expand All @@ -112,16 +133,12 @@ def get_credentials(self) -> dict:
"user_id": self.user_id,
}

def _post(self, path, login: bool = True, retry: int = 0, **kwargs
def _post(self, path, retry: int = 0, **kwargs
) -> requests.Response:
max_retries = 1
if login:
self.login()
response = self.session.post(
self._get_url(path),
headers=self._headers,
proxies=self.proxies,
timeout=self.timeout,
**kwargs,
)
if response.status_code in (HTTPStatus.OK, HTTPStatus.ACCEPTED):
Expand All @@ -133,11 +150,11 @@ def _post(self, path, login: bool = True, retry: int = 0, **kwargs
# Status Code == 403 and no json contend
# --> Blocked due to rate limit / wrong user_agent.
# 1. Get latest APK Version from google
# 2. Give the current token a 1 hour penalty (faster refresh)
# 2. Reset current session
# 3. Rety request
if response.status_code == 403 and retry < max_retries:
self.user_agent = self._get_user_agent()
self.last_time_token_refreshed -= timedelta(hours=1)
self.session = self._create_session()
return self._post(path, retry=retry + 1, **kwargs)
self.captcha_error_count += 1
raise TgtgCaptchaError(response.status_code,
Expand Down Expand Up @@ -195,8 +212,7 @@ def _refresh_token(self) -> None:
return
response = self._post(
REFRESH_ENDPOINT,
json={"refresh_token": self.refresh_token},
login=False
json={"refresh_token": self.refresh_token}
)
self.access_token = response.json()["access_token"]
self.refresh_token = response.json()["refresh_token"]
Expand All @@ -218,8 +234,7 @@ def login(self) -> None:
json={
"device_type": self.device_type,
"email": self.email,
},
login=False
}
)
first_login_response = response.json()
if first_login_response["state"] == "TERMS":
Expand All @@ -240,8 +255,7 @@ def start_polling(self, polling_id) -> None:
"device_type": self.device_type,
"email": self.email,
"request_polling_id": polling_id,
},
login=False
}
)
if response.status_code == HTTPStatus.ACCEPTED:
log.warning(
Expand Down Expand Up @@ -281,6 +295,7 @@ def get_items(
hidden_only=False,
we_care_only=False,
) -> List[dict]:
self.login()
# fields are sorted like in the app
data = {
"user_id": self.user_id,
Expand All @@ -303,13 +318,15 @@ def get_items(
return response.json()["items"]

def get_item(self, item_id: str) -> dict:
self.login()
response = self._post(
f"{API_ITEM_ENDPOINT}/{item_id}",
json={"user_id": self.user_id, "origin": None},
)
return response.json()

def set_favorite(self, item_id: str, is_favorite: bool) -> None:
self.login()
self._post(
f"{API_ITEM_ENDPOINT}/{item_id}/setFavorite",
json={"is_favorite": is_favorite},
Expand All @@ -333,8 +350,7 @@ def signup_by_email(
"name": name,
"newsletter_opt_in": newsletter_opt_in,
"push_notification_opt_in": push_notification_opt_in,
},
login=False
}
)
login_response = response.json()["login_response"]
self.access_token = login_response["access_token"]
Expand Down

0 comments on commit 51c9a58

Please sign in to comment.