def extract_formkey(html):
script_regex = r"<script>.*?;</script>"
script_text = re.findall(script_regex, html)[0]
pattern = r'<script>window.\w+=function\(\){return window.\w+\("(\w+)"\);};</script>'
match = re.search(pattern, html)
if match:
key = match.group(1)
else:
raise Exception("Failed to get key for decoding form key")
pattern = r'return q\(\w+,(\[.*?\])\)\['
match = re.search(pattern, script_text)
if match:
js_array_str = match.group(1)
js_array = re.findall(r'0x[\da-fA-F]+', js_array_str) # match hexadecimal numbers
index_array = [int(num, 16) for num in js_array] # convert to decimal
else:
raise Exception("Failed to decode js_array")
return ''.join([key[index] for index in index_array])[:32]