Add exec_js helper def

This commit is contained in:
Heiner Lohaus 2023-11-03 22:24:35 +01:00
parent 65b5227500
commit dfefd22aa1

View File

@ -145,11 +145,6 @@ class OpenaiChat(AsyncGeneratorProvider):
return f"g4f.provider.{cls.__name__} supports: ({param})" return f"g4f.provider.{cls.__name__} supports: ({param})"
async def get_arkose_token(proxy: str = None) -> str: async def get_arkose_token(proxy: str = None) -> str:
node = shutil.which("node")
if not node:
if debug.logging:
print('OpenaiChat: "node" not found')
return
dir = os.path.dirname(os.path.dirname(__file__)) dir = os.path.dirname(os.path.dirname(__file__))
include = f'{dir}/npm/node_modules/funcaptcha' include = f'{dir}/npm/node_modules/funcaptcha'
config = { config = {
@ -175,17 +170,28 @@ fun.getToken(config).then(token => {
tmp.write(source.encode()) tmp.write(source.encode())
tmp.close() tmp.close()
try: try:
return await exec_js(tmp.name)
finally:
os.unlink(tmp.name)
async def exec_js(file: str) -> str:
node = shutil.which("node")
if not node:
if debug.logging:
print('OpenaiChat: "node" not found')
return
if sys.platform == 'win32': if sys.platform == 'win32':
p = subprocess.Popen( p = subprocess.Popen(
[node, tmp.name], [node, file],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE stderr=subprocess.PIPE
) )
stdout, stderr = p.communicate()
if p.returncode == 0: if p.returncode == 0:
return p.stdout.read().decode() return stdout.decode()
raise RuntimeError(f"Exec Error: {p.stderr.read().decode()}") raise RuntimeError(f"Exec Error: {stderr.decode()}")
p = await asyncio.create_subprocess_exec( p = await asyncio.create_subprocess_exec(
node, tmp.name, node, file,
stderr=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
stdout=asyncio.subprocess.PIPE stdout=asyncio.subprocess.PIPE
) )
@ -193,5 +199,3 @@ fun.getToken(config).then(token => {
if p.returncode == 0: if p.returncode == 0:
return stdout.decode() return stdout.decode()
raise RuntimeError(f"Exec Error: {stderr.decode()}") raise RuntimeError(f"Exec Error: {stderr.decode()}")
finally:
os.unlink(tmp.name)