import requests, re, io, sys, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
s = requests.Session()
s.headers.update({
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
    'Accept': 'text/html,application/xhtml+xml,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.9',
    'Referer': 'https://apkpure.com/vn/vinfast/com.vinfast.companion.app',
})
for url in [
    'https://apkpure.com/vn/vinfast/com.vinfast.companion.app/download',
    'https://apkpure.com/vn/vinfast/com.vinfast.companion.app/download?from=app-page',
]:
    r = s.get(url, timeout=30, allow_redirects=True)
    print('\n[dl]', url, r.status_code, 'len=', len(r.text), 'final=', r.url)
    if r.status_code != 200:
        continue
    t = r.text
    # tìm mọi URL có .apk/.xapk hoặc d.apkpure
    pats = [
        r'(https://[a-z0-9.-]+/(?:b|e)/[A-Z]+/[a-z0-9.-]+[^"\')<]*\.(?:apk|xapk)[a-z0-9.?=&%-]*)',
        r'(https://d\.apkpure[^"\')<\s]+)',
        r'(https://[^"\')<\s]+\.(?:apk|xapk)\?[^"\')<\s]*)',
        r'data-dt-original="([^"]+)"',
        r'href="(https?://[^"]+download[^"]*)"',
    ]
    for p in pats:
        for m in sorted(set(re.findall(p, t)))[:12]:
            print('   cand:', html.unescape(m)[:180])
