diff --git a/tgtg/__init__.py b/tgtg/__init__.py index 32387a6..7459826 100644 --- a/tgtg/__init__.py +++ b/tgtg/__init__.py @@ -1,11 +1,14 @@ import datetime import random +import sys import time from http import HTTPStatus from urllib.parse import urljoin import requests +from tgtg.google_play_scraper import get_last_apk_version + from .exceptions import TgtgAPIError, TgtgLoginError, TgtgPollingError BASE_URL = "https://apptoogoodtogo.com/api/" @@ -16,10 +19,11 @@ REFRESH_ENDPOINT = "auth/v3/token/refresh" ACTIVE_ORDER_ENDPOINT = "order/v6/active" INACTIVE_ORDER_ENDPOINT = "order/v6/inactive" +DEFAULT_APK_VERSION = "22.5.5" USER_AGENTS = [ - "TGTG/22.5.5 Dalvik/2.1.0 (Linux; U; Android 6.0.1; Nexus 5 Build/M4B30Z)", - "TGTG/22.5.5 Dalvik/2.1.0 (Linux; U; Android 7.0; SM-G935F Build/NRD90M)", - "TGTG/22.5.5 Dalvik/2.1.0 (Linux; Android 6.0.1; SM-G920V Build/MMB29K)", + "TGTG/{} Dalvik/2.1.0 (Linux; U; Android 9; Nexus 5 Build/M4B30Z)", + "TGTG/{} Dalvik/2.1.0 (Linux; U; Android 10; SM-G935F Build/NRD90M)", + "TGTG/{} Dalvik/2.1.0 (Linux; Android 12; SM-G920V Build/MMB29K)", ] DEFAULT_ACCESS_TOKEN_LIFETIME = 3600 * 4 # 4 hours MAX_POLLING_TRIES = 24 # 24 * POLLING_WAIT_TIME = 2 minutes @@ -55,13 +59,24 @@ def __init__( self.device_type = device_type - self.user_agent = user_agent if user_agent else random.choice(USER_AGENTS) + self.user_agent = user_agent if user_agent else self._get_user_agent() self.language = language self.proxies = proxies self.timeout = timeout self.session = requests.Session() self.session.headers = self._headers + def _get_user_agent(self): + try: + self.version = get_last_apk_version() + except Exception: + self.version = DEFAULT_APK_VERSION + sys.stdout.write("Failed to get last version\n") + + sys.stdout.write(f"Using version {self.version}\n") + + return random.choice(USER_AGENTS).format(self.version) + def _get_url(self, path): return urljoin(self.base_url, path) @@ -163,14 +178,14 @@ def start_polling(self, polling_id): timeout=self.timeout, ) if response.status_code == HTTPStatus.ACCEPTED: - print( + sys.stdout.write( "Check your mailbox on PC to continue... " - "(Mailbox on mobile won't work, if you have installed tgtg app.)" + "(Mailbox on mobile won't work, if you have installed tgtg app.)\n" ) time.sleep(POLLING_WAIT_TIME) continue elif response.status_code == HTTPStatus.OK: - print("Logged in!") + sys.stdout.write("Logged in!\n") login_response = response.json() self.access_token = login_response["access_token"] self.refresh_token = login_response["refresh_token"] diff --git a/tgtg/google_play_scraper.py b/tgtg/google_play_scraper.py new file mode 100644 index 0000000..b0f612c --- /dev/null +++ b/tgtg/google_play_scraper.py @@ -0,0 +1,17 @@ +import json +import re + +import requests + +RE_SCRIPT = re.compile( + r"AF_initDataCallback\({key:\s*'ds:4'.*?data:([\s\S]*?), sideChannel:.+<\/script" +) + + +def get_last_apk_version(): + response = requests.get( + "https://play.google.com/store/apps/details?id=com.app.tgtg&hl=en&gl=US" + ) + match = RE_SCRIPT.search(response.text) + data = json.loads(match.group(1)) + return data[1][2][140][0][0][0]