import requests
import time
import json
import random

requests.packages.urllib3.disable_warnings()


def test(authorization):
    # 自动点击
    url = 'https://api.hamsterkombat.io/clicker/tap'
    h = {
        "Host": "api.hamsterkombat.io",
        "Connection": "keep-alive",
        "Pragma": "no-cache",
        "Cache-Control": "no-cache",
        "Accept": "*/*",
        "Access-Control-Request-Method": "POST",
        "Access-Control-Request-Headers": "authorization,content-type",
        "Origin": "https://hamsterkombat.io",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://hamsterkombat.io/",
        "Accept-Encoding": "gzip, deflate, br, zstd",
        "Accept-Language": "zh,zh-CN;q=0.9,en;q=0.8"
    }

    s = requests.session()
    res = s.options(url, headers=h, verify=False)
    # print(res.text)

    h2 = {
        "Host": "api.hamsterkombat.io",
        "Connection": "keep-alive",
        "Content-Length": "55",
        "Pragma": "no-cache",
        "Cache-Control": "no-cache",
        "sec-ch-ua": "\"Google Chrome\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"",
        "accept": "application/json",
        "content-type": "application/json",
        "sec-ch-ua-mobile": "?0",
        "authorization": authorization,
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36",
        "sec-ch-ua-platform": "\"Windows\"",
        "Origin": "https://hamsterkombat.io",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://hamsterkombat.io/",
        "Accept-Encoding": "gzip, deflate, br, zstd",
        "Accept-Language": "zh,zh-CN;q=0.9,en;q=0.8"
    }
    r_int = random.randint(99, 150)
    data = {
        "count": r_int,
        "availableTaps": 1963 - r_int,
        "timestamp": int(str(time.time()).split('.')[0])
    }

    data = json.dumps(data, separators=(',', ':'))

    res2 = s.post(url, headers=h2, data=data, verify=False)

    print(res2.text)
    # print(res2.text)
    # print(res2.text)
    if res2.json()['clickerUser']['availableTaps'] >= 1:
        time.sleep(random.randint(4, 10))

        test(authorization)


def upgrades_buy(authorization):
    # 签到
    url = 'https://api.hamsterkombatgame.io/clicker/upgrades-for-buy'
    h = {
        "Host": "api.hamsterkombatgame.io",
        "Connection": "keep-alive",
        "Authorization": authorization,
        "User-Agent": "Mozilla/5.0 (Linux; Android 12; K) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/95.0.4638.74 Safari/537.36",
        "Accept": "*/*",
        "Origin": "https://hamsterkombatgame.io",
        "X-Requested-With": "org.telegram.messenger",
        "Sec-Fetch-Site": "same-site",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Dest": "empty",
        "Referer": "https://hamsterkombatgame.io/",
        "Accept-Language": "en,zh-CN;q=0.9,zh;q=0.8,en-US;q=0.7",
        "Accept-Encoding": "gzip, deflate",
        "Content-Length": "0",
    }
    res = requests.post(url, headers=h, verify=False)
    print(res.json())
    print(res.json()[
              'dailyCombo'])  # {'upgradeIds': [], 'bonusCoins': 5000000, 'isClaimed': False, 'remainSeconds': 31267}


authorization = "Bearer 17248120209751wF8gOWJiDNbXsFD2PdSW5AlpoIfM8REW62lLwPXgqaVLmACNtAL0UJblr21RRRU7478944675"
# upgrades_buy(authorization)
while True:
    try:
        test(authorization)
        now = time.localtime()
        print(time.strftime("%Y-%m-%d %H:%M:%S", now))
        print('等待12分钟')
        time.sleep(12 * 60)
        print('等待结束')
    except Exception as e:
        print('重试')
