quick.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. from quickjs import Function,Context
  2. import requests
  3. from time import time
  4. import js2py
  5. import json
  6. ctx = Context()
  7. ctx.add_callable("print", print)
  8. def print2(text):
  9. print('print2:',text)
  10. # ctx.set('print2',print2)
  11. ctx.add_callable("print2", print2)
  12. # ctx.add_callable("ua", 'mobile')
  13. gg="""
  14. function adder(a, b) {
  15. c=[1,2].filter(it=>it>1);
  16. print(c);
  17. print(ua);
  18. print(c.join('$'))
  19. print(typeof c)
  20. print(Array.isArray(c))
  21. return a + b+`你这个a是${a},c是${c}`;
  22. }
  23. function bd(){
  24. let html = request('https://www.baidu.com/')
  25. }
  26. function f(a, b) {
  27. let e = print2;
  28. e(2222);
  29. return adder(a, b);
  30. }
  31. var d = 123;
  32. print2('我是注入的');
  33. print2(typeof(ccc));
  34. var gs = {a:1};
  35. print2(gs)
  36. // print2(ccc(gs))
  37. var qw = [1,2,3]
  38. print2(mmp)
  39. """
  40. # f = Function("f",gg)
  41. #print(f(1,3))
  42. #assert f(1, 2) == 3
  43. # ctx.add_callable("f", f)
  44. # ctx.add_callable("f", f)
  45. ctx.set('ua','mobile')
  46. cc = {
  47. # "json":json
  48. "json":'22323'
  49. # json:json
  50. }
  51. def ccc(aa):
  52. return json.dumps(aa)
  53. # ctx.add_callable('json',json)
  54. # ctx.set('cc',cc) # 报错
  55. ctx.add_callable('ccc',ccc)
  56. # ctx.eval(gg)
  57. ctx.set('mmp','我的mm')
  58. #ctx.eval(f(1,3))
  59. ctx.eval(gg+'let lg = print;lg(111);lg(f(1,3))')
  60. ctx.set("v", 10**25)
  61. print('v',type(ctx.eval("v")),ctx.eval("v"))
  62. print(ctx.get('d'))
  63. gs = ctx.get('gs')
  64. qw = ctx.get('qw')
  65. print('qw',qw)
  66. print(qw.json())
  67. print('gs',gs)
  68. print(gs.json())
  69. print(json.loads(gs.json()))
  70. print(ctx.get('e'))
  71. print(ctx.get('print2'))
  72. print(11,ctx.parse_json('{"a":1}'))
  73. def request(url):
  74. r = requests.get(url)
  75. r.encoding = r.apparent_encoding
  76. html = r.text
  77. print(html)
  78. return html
  79. ctx.add_callable('request',request)
  80. ctx.eval('bd()')
  81. t1 = time()
  82. rule_file = '555影视.js'
  83. with open('../../js/模板.js', encoding='utf-8') as f:
  84. before = f.read().split('export')[0]
  85. with open(f'../../js/{rule_file}',encoding='utf-8') as f1:
  86. jscode = f1.read()
  87. jscode = before + jscode
  88. print(jscode)
  89. ctx1 = Context()
  90. ctx1.eval(jscode)
  91. ret = ctx1.get('rule')
  92. # print(type(ret.json()),ret.json())
  93. # print(json.loads(ret.json()))
  94. ret = json.loads(ret.json())
  95. print(type(ret),ret)
  96. t2 = time()
  97. print(f'quickjs耗时:{round((t2-t1)*1000,2)}毫秒')
  98. tt1 = time()
  99. ctx1.eval(jscode)
  100. ret = ctx1.get('rule')
  101. print(type(ret.json()),ret.json())
  102. print(json.loads(ret.json()))
  103. tt2 = time()
  104. print(f'quickjs第2次耗时:{round((tt2-tt1)*1000,2)}毫秒')
  105. t3 = time()
  106. with open(f'../../js/{rule_file}',encoding='utf-8') as f1:
  107. jscode = f1.read()
  108. jscode = before + jscode
  109. # print(jscode)
  110. ctx2 = js2py.EvalJs({},enable_require=False) # enable_require启用require关键字,会自动获取系统nodejs环境
  111. ctx2.execute(jscode)
  112. ret = ctx2.rule.to_dict()
  113. print(type(ret),ret)
  114. t4 = time()
  115. print(f'js2py耗时:{round((t4-t3)*1000,2)}毫秒')
  116. t5 = time()
  117. ctx2.execute(jscode)
  118. ret = ctx2.rule.to_dict()
  119. print(type(ret),ret)
  120. t6 = time()
  121. print(f'js2py第2次耗时:{round((t6-t5)*1000,2)}毫秒')
  122. # 报错提示:(没法把python对象给qkjs,基础数据类型字典也不行,json等包更不行了) Unsupported type when converting a Python object to quickjs: dict.