import execjs
import os
from common.common import Common


def get_xb(f_url, ua):
    js_path = os.path.abspath(os.path.dirname(os.getcwd()))
    Common.logger(log_type="recommend", crawler="douyin").info(f"js_path:{js_path}/piaoquan_crawler/douyin/xb.js")
    # with open(f'{js_path}/xb.js', 'r', encoding='utf-8') as f:
    with open(f'{js_path}/piaoquan_crawler/douyin/xb.js', 'r', encoding='utf-8') as f:
        douyin_js = f.read()

    params = f_url.split('/?')[1]
    # params = urllib.parse.unquote(params)
    # params = urllib.parse.unquote(urllib.parse.urlencode(params, safe='='))
    ctx = execjs.compile(douyin_js)
    for i in range(10):
        xb = ctx.call('_0x11bbd8', params, ua)
        if xb:
            return xb