use_js.py 539 B

12345678910111213141516171819202122232425
  1. """
  2. @author: luojunhui
  3. """
  4. import json
  5. import subprocess
  6. from config import toutiao_js_path
  7. def call_js_function(arguments_list):
  8. """
  9. call js function
  10. """
  11. # 将参数转换为JSON字符串
  12. args_json = json.dumps(arguments_list)
  13. # 调用Node.js执行脚本
  14. result = subprocess.run(
  15. ['node', toutiao_js_path, args_json],
  16. capture_output=True,
  17. text=True
  18. )
  19. if result.returncode == 0:
  20. return result.stdout.strip()
  21. else:
  22. raise Exception(f"Error: {result.stderr}")