# -*- coding: utf-8 -*-
import json
import logging
import os
import re
import time
import urllib
import sys
import random

import requests
from py_mini_racer import MiniRacer

def get_bogus(url, user_agent):
    query = urllib.parse.urlparse(url).query
    js_path = os.path.dirname(os.path.abspath(__file__))

    '''
    x_bogus_js_path = os.path.join(js_path, 'x_bogus.js')
    with open(x_bogus_js_path, 'r', encoding='utf-8') as f:
        x_bogus_js_code = f.read()
    x_bogus_ctx = MiniRacer()
    x_bogus_ctx.eval(x_bogus_js_code)
    xbogus = x_bogus_ctx.call('sign', query, user_agent)
    url = url + "&X-Bogus=" + xbogus
    '''

    a_bogus_js_path = os.path.join(js_path, 'a_bogus.js')
    with open(a_bogus_js_path, 'r', encoding='utf-8') as f:
            a_bogus_js_code = f.read()
    a_bogus_ctx = MiniRacer()
    a_bogus_ctx.eval(a_bogus_js_code)
    abogus = a_bogus_ctx.call('generate_a_bogus', query, user_agent)
    url = url + "&a_bogus=" + abogus
    return url

def get_ttwid_webid(url, user_agent):
    while True:
        try:
            headers = {
                "User-Agent": user_agent,
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
                "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8"
            }

            response = requests.request("GET", url, headers=headers, verify=False, timeout=3)
            cookies_dict = response.cookies.get_dict()
            ttwid_str = cookies_dict.get('ttwid')
            render_data_text = \
            re.compile('\<script id=\"RENDER_DATA\" type\=\"application\/json\">(.*?)\<\/script\>').findall(
                response.text)
            if(render_data_text):
                render_data_text=render_data_text[0]
                render_data_text = requests.utils.unquote(render_data_text)
                render_data_json = json.loads(render_data_text, strict=False)
                webid = render_data_json.get('app').get('odin').get('user_unique_id')
                return ttwid_str, webid
        except Exception as e:
            logging.error(e)
            time.sleep(1)

if __name__ == '__main__':
    try:
        payload = sys.argv[1]
        params = json.loads(payload)
        if params['type'] == 'bogus':
            result = get_bogus(params['url'], params['user_agent'])
        elif params['type'] == 'webid':
            result = get_ttwid_webid(params['url'], params['user_agent'])
        print(result)
    except Exception as e:
        print(e)