import authinfo
import json
import requests
import time

jar = requests.cookies.RequestsCookieJar()
jar.set('iksm_session', authinfo.cookie)
base_url = "https://app.splatoon2.nintendo.net"
shop_url = base_url + "/api/onlineshop/merchandises"
hook_url = authinfo.hook_url

while True:
    r = requests.get(shop_url, cookies=jar)
    response = json.loads(r.text)
    if not response["merchandises"]:
        print("Bad response: %s"%(r.text))
    else:
        gear = response["merchandises"][-1]
        name = gear["gear"]["name"]
        ability = gear["skill"]["name"]
        brand = gear["gear"]["brand"]
        brand_name = brand["name"]
        brand_icon = base_url + brand["image"]
        gear_image = base_url + gear["gear"]["image"]

        embed = {
            "title": "%s with %s"%(name, ability),
            "author": {
                "name": brand_name,
                "icon_url": brand_icon
             },
             "image": {
                "url": gear_image
             }
        }
        payload = {"embeds": [embed]}
        postres = requests.post(hook_url, json=payload)

        current_time = time.time()
        update_time = response["merchandises"][0]["end_time"]+5
        time.sleep(update_time-current_time)