parser.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : parser.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/8/25
  6. import os
  7. import shutil
  8. import requests
  9. from flask import make_response, jsonify,render_template_string
  10. from functools import partial # 这玩意儿能锁定一个函数的参数
  11. import subprocess
  12. subprocess.Popen = partial(subprocess.Popen, encoding="utf-8") # 固定写法
  13. # 解决execjs执行js时产生的乱码报错,需要在导入该模块之前,让Popen的encoding参数锁定为utf-8
  14. # import execjs
  15. import js2py
  16. from js2py.base import JsObjectWrapper,PyJsString,PyJsObject
  17. # os.environ["EXECJS_RUNTIME"] = "JScript"
  18. # print(execjs.get().name)
  19. def runJScode(jscode,loader=None,ctx=None):
  20. if loader is None:
  21. if ctx is None:
  22. ctx = {}
  23. loader = js2py.EvalJs(ctx,enable_require=False) # enable_require启用require关键字,会自动获取系统nodejs环境
  24. loader.execute(jscode)
  25. return loader, jscode
  26. def runJs(jsPath, before='', after='', ctx=None):
  27. # base_path = os.path.dirname(os.path.abspath(__file__)) # 当前文件所在目录
  28. # base_path = os.path.dirname(os.getcwd()) # 当前主程序所在工作目录
  29. # base_path = os.path.dirname(os.path.abspath('.')) # 上级目录
  30. # js_code = 'var rule={}'
  31. if ctx is None:
  32. ctx = {}
  33. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  34. if str(jsPath).startswith('http'):
  35. js_name = jsPath.split('/')[-1]
  36. cache_path = os.path.join(base_path, f'cache/{js_name}')
  37. if not os.path.exists(cache_path):
  38. try:
  39. print(f'开始缓存远程规则:{js_name},来源{jsPath}')
  40. js_code = requests.get(url=jsPath,timeout=3).text
  41. # js_code = requests.get(jsPath).text
  42. with open(cache_path,mode='w+',encoding='utf-8') as f:
  43. f.write(js_code)
  44. except Exception as e:
  45. print('发生了错误:',e)
  46. return None, ''
  47. else:
  48. with open(cache_path, 'r', encoding='UTF-8') as fp:
  49. js_code = fp.read()
  50. else:
  51. js_path = os.path.join(base_path, jsPath)
  52. if not os.path.exists(js_path):
  53. return None,''
  54. js_name = jsPath.split('/')[-1]
  55. cache_path = os.path.join(base_path, f'cache/{js_name}')
  56. if not str(jsPath).startswith('js/') and not os.path.exists(cache_path) and os.path.exists(js_path):
  57. shutil.copy(js_path,cache_path) # 本地txt目录的复制过去凑数,实际不使用
  58. # print(js_path)
  59. with open(js_path, 'r', encoding='UTF-8') as fp:
  60. js_code = fp.read()
  61. # print(js_code)
  62. jscode_to_run = js_code
  63. # print(jscode_to_run)
  64. if before:
  65. jscode_to_run = before + jscode_to_run
  66. if after:
  67. jscode_to_run += after
  68. loader = js2py.EvalJs(ctx)
  69. return runJScode(jscode_to_run,loader)
  70. # loader = execjs.compile(jscode_to_run)
  71. # print(jscode_to_run)
  72. # loader.execute(jscode_to_run)
  73. # return loader,js_code
  74. def toJs(jsPath,jsRoot='cache',env=None):
  75. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  76. js_path = os.path.join(base_path, f'{jsRoot}/{jsPath}')
  77. # print(js_path)
  78. if not os.path.exists(js_path):
  79. return jsonify({'code': -2, 'msg': f'非法猥亵,文件不存在'})
  80. with open(js_path, 'r', encoding='UTF-8') as fp:
  81. js = fp.read()
  82. if env:
  83. # js = render_template_string(js,env=env)
  84. for k in env:
  85. # print(f'${k}', f'{env[k]}')
  86. if f'${k}' in js:
  87. js = js.replace(f'${k}', f'{env[k]}')
  88. # js = render_template_string(js,**env)
  89. response = make_response(js)
  90. response.headers['Content-Type'] = 'text/javascript; charset=utf-8'
  91. return response
  92. def getJs(jsPath,jsRoot='cache'):
  93. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  94. js_path = os.path.join(base_path, f'{jsRoot}/{jsPath}')
  95. # print(js_path)
  96. if not os.path.exists(js_path):
  97. return ''
  98. with open(js_path, 'r', encoding='UTF-8') as fp:
  99. js = fp.read()
  100. return js
  101. def toHtml(jsPath):
  102. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  103. js_path = os.path.join(base_path, f'cache/{jsPath}')
  104. with open(js_path, 'r', encoding='UTF-8') as fp:
  105. js = fp.read()
  106. response = make_response(js)
  107. response.headers['Content-Type'] = 'text/html; charset=utf-8'
  108. return response
  109. def runPy(pyPath):
  110. # base_path = os.path.dirname(os.path.abspath(__file__)) # 当前文件所在目录
  111. # base_path = os.path.dirname(os.getcwd()) # 当前主程序所在工作目录
  112. # base_path = os.path.dirname(os.path.abspath('.')) # 上级目录
  113. # js_code = 'var rule={}'
  114. if pyPath and not str(pyPath).endswith('.py'):
  115. pyPath += '.py'
  116. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  117. if str(pyPath).startswith('http'):
  118. py_name = pyPath.split('/')[-1]
  119. cache_path = os.path.join(base_path, f'cache/{py_name}')
  120. print('远程免嗅:',py_name)
  121. if not os.path.exists(cache_path):
  122. try:
  123. py_code = requests.get(pyPath,timeout=2).text
  124. with open(cache_path,mode='w+',encoding='utf-8') as f:
  125. f.write(py_code)
  126. except Exception as e:
  127. print('发生了错误:',e)
  128. return None
  129. else:
  130. with open(cache_path, 'r', encoding='UTF-8') as fp:
  131. py_code = fp.read()
  132. else:
  133. py_root = os.path.join(base_path, 'py/')
  134. os.makedirs(py_root,exist_ok=True)
  135. py_path = os.path.join(py_root, pyPath)
  136. if not os.path.exists(py_path):
  137. return ''
  138. with open(py_path, 'r', encoding='UTF-8') as fp:
  139. py_code = fp.read()
  140. # print(js_code)
  141. return py_code
  142. def covert_demo():
  143. ctx = {'py_sum':sum,'requests':requests}
  144. loader = js2py.EvalJs(ctx)
  145. # loader.execute('var a=py_sum(2,3);function f(x) {return x*x} var b=[a,"5"];var c={"a":a};')
  146. # loader.execute('var a=py_sum(2,3);function f(x) {return x*x}')
  147. loader.execute('function f(x) {return x*x};var a=py_sum([2,3]);var b=[a,5];var c={"a":a};')
  148. f = loader.f
  149. print(f(8))
  150. print(f.toString())
  151. print(loader.eval('py_sum(new Array(1, 2, 3))'))
  152. print(loader.eval('py_sum([1, 2])'))
  153. a = loader.a
  154. print(type(a),a)
  155. b = loader.b
  156. b.push(6)
  157. print(type(b),b)
  158. b = b.to_list()
  159. print(type(b),b)
  160. c = loader.c
  161. print(type(c),c)
  162. c = c.to_dict()
  163. print(type(c), c)
  164. # CryptoJS = js2py.require('crypto-js')
  165. # print(type(CryptoJS))
  166. # print(js2py.require('underscore'))
  167. JSON = js2py.eval_js('JSON')
  168. r = JSON.parse('[{"a":1}]')
  169. print(type(r),r)
  170. print(r[0].a)
  171. print(loader.eval('r = requests.get("https://www.baidu.com/");r.encoding = "utf-8";r.text'))
  172. # 下面是错误用法,没有loader环境没法正确eval_js,有loader用eval不需要eval_js
  173. # print(js2py.eval_js('r = requests.get("https://www.baidu.com/");r.encoding = "utf-8";r.text'))
  174. with open('../js/蓝莓影视.js',encoding='utf-8') as f:
  175. yk = f.read()
  176. print(yk)
  177. if __name__ == '__main__':
  178. covert_demo()