Running a large javascript file from within Python and applying a second javascript ‘fetch … then’ to the original code

Currently I can retrieve a HMAC-Secret and AES-key with a Python script by running an html fragment in a headless browser, parsing the browser page returned and obtaining said secret and key.

The html fragment is this:

<head>
    <script src="https://player.akamaized.net/html5player/core/html5-c5-player.js"></script>
    <script>
        fetch("https://player.akamaized.net/html5player/core/html5-c5-player.js")
            .then((r) => r.text())
            .then((b) => {
                const dfnm = /x72x65x74x75x72x6ex20x74x79x70x65x6fx66x20([a-z0-9]+)[(d+)].([a-z0-9]+)x20x3dx3dx3dx20x27x66x75x6ex63x74x69x6fx6ex27/gi.exec(b);
                const krm = /x27\x68x27:[a-z0-9]+.[a-z0-9]+((d+)),'\x61':[a-z0-9]+.[a-z0-9]+((d+))/gi.exec(b);
                document.write(JSON.stringify({ HMAC_SECRET: window[dfnm[1]][dfnm[2]][dfnm[3]](krm[1]), AES_KEY: window[dfnm[1]][dfnm[2]][dfnm[3]](krm[2]) }));
            });
    </script>
</head>

The whole operation takes over 7 seconds to run because of the time overhead of starting a headless browser. I would like to execute the JavaScript from within Python code using a module such as js2py.

I am now confused as to how to apply the ‘fetch … then’ construct to the JavaScript downloaded.

I have this Python so far:-

import requests
import js2py

js2 = b""".then((r) => r.text())
            .then((b) => {
                const dfnm = /x72x65x74x75x72x6ex20x74x79x70x65x6fx66x20([a-z0-9]+)[(d+)].([a-z0-9]+)x20x3dx3dx3dx20x27x66x75x6ex63x74x69x6fx6ex27/gi.exec(b);
                const krm = /x27\x68x27:[a-z0-9]+.[a-z0-9]+((d+)),'\x61':[a-z0-9]+.[a-z0-9]+((d+))/gi.exec(b);
                document.write(JSON.stringify({ HMAC_SECRET: window[dfnm[1]][dfnm[2]][dfnm[3]](krm[1]), AES_KEY: window[dfnm[1]][dfnm[2]][dfnm[3]](krm[2]) }));
            });"""

def get_hmac_aes_keys():
    # Fetch HTML content
    response = requests.get('https://player.akamaized.net/html5player/core/html5-c5-player.js')
    if response.status_code == 200:
        js = response.text
        
        # Execute JavaScript code using js2py
        context = js2py.EvalJs()
        context.execute(js + js2)


        
get_hmac_aes_keys()

But that produces a warning – SyntaxWarning: invalid escape sequence ‘[‘
And an error – KeyError: 3 from js2py (Is js2py up to managing this level of complexity?)

So here I am stuck…

I have tried a chatgpt (3.5) session but that wanted to regex the whole thing and appeared out of its depth.