Skip to content

Commit

Permalink
Get last APK version from playstore (#171)
Browse files Browse the repository at this point in the history
* Build user agent from last APK version available in playstore
* update android version of default user agents
  • Loading branch information
ahivert authored Jun 21, 2022
1 parent f248155 commit c93b02a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
29 changes: 22 additions & 7 deletions tgtg/__init__.py
Original file line number Diff line number Diff line change
@@ -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/"
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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"]
Expand Down
17 changes: 17 additions & 0 deletions tgtg/google_play_scraper.py
Original file line number Diff line number Diff line change
@@ -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]

0 comments on commit c93b02a

Please sign in to comment.