rules.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : rules.py.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/8/25
  6. import json
  7. import os
  8. from time import time
  9. import js2py
  10. from quickjs import Function,Context
  11. from utils.log import logger
  12. # from utils.web import get_interval,UA
  13. from utils.ua import UA,get_interval
  14. from flask import render_template_string
  15. import ujson
  16. def getRuleLists():
  17. base_path = os.path.dirname(os.path.abspath(__file__)) # 当前文件所在目录
  18. # print(base_path)
  19. file_name = os.listdir(base_path)
  20. file_name = list(filter(lambda x:str(x).endswith('.js') and str(x).find('模板') < 0,file_name))
  21. # print(file_name)
  22. rule_list = [file.replace('.js','') for file in file_name]
  23. # print(rule_list)
  24. return rule_list
  25. def getCacheCount():
  26. base_path = 'cache/' # 当前缓存js所在目录
  27. os.makedirs(base_path, exist_ok=True)
  28. file_name = os.listdir(base_path)
  29. file_name = list(filter(lambda x: str(x).endswith('.js') and str(x).find('模板') < 0, file_name))
  30. return len(file_name)
  31. def getRulesJs2py(path='cache',js_mode=0):
  32. t1 = time()
  33. base_path = path+'/' # 当前文件所在目录
  34. # print(base_path)
  35. os.makedirs(base_path,exist_ok=True)
  36. file_name = os.listdir(base_path)
  37. file_name = list(filter(lambda x: str(x).endswith('.js') and str(x).find('模板') < 0, file_name))
  38. # print(file_name)
  39. rule_list = [file.replace('.js', '') for file in file_name]
  40. js_path = [f'{path}/{rule}.js' for rule in rule_list]
  41. with open('js/模板.js', encoding='utf-8') as f:
  42. # before = f.read().split('export')[0]
  43. before = f.read().split('export default')[0]
  44. rule_codes = []
  45. # for js in js_path:
  46. # with open(js,encoding='utf-8') as f:
  47. # code = f.read()
  48. # rule_codes.append(js2py.eval_js(before+code))
  49. ctx = js2py.EvalJs()
  50. codes = []
  51. for i in range(len(js_path)):
  52. js = js_path[i]
  53. with open(js,encoding='utf-8') as f:
  54. code = f.read()
  55. new_code = 'var muban = JSON.parse(JSON.stringify(mubanDict));\n'+code.replace('rule',f'rule{i}',1)
  56. # new_code = ''+code.replace('rule',f'rule{i}',1)
  57. codes.append(new_code)
  58. newCodes = before + '\n'+ '\n'.join(codes)
  59. # print(newCodes)
  60. try:
  61. ctx.execute(newCodes)
  62. for i in range(len(js_path)):
  63. rule_codes.append(ctx.eval(f'rule{i}'))
  64. # print(type(rule_codes[0]),rule_codes[0])
  65. # print(rule_codes[0].title)
  66. # print(rule_codes[0].searchable)
  67. # print(rule_codes[0].quickSearch)
  68. # rule_codes 是个 js2py.base.JsObjectWrapper 类型,所以下面才能用. 获取属性
  69. new_rule_list = []
  70. for i in range(len(rule_list)):
  71. # 过滤排除drpy
  72. if js_mode == 1 and rule_list[i] == 'drpy':
  73. continue
  74. sable = rule_codes[i].searchable or 0
  75. tmpObj = {
  76. 'name':rule_list[i],
  77. # 'searchable':1 if (js_mode==1 and sable==2) else sable, # 对js模式1开放软件聚搜(还是算了,服务器遭不住)
  78. 'searchable':sable,
  79. 'quickSearch':rule_codes[i].quickSearch or 0,
  80. 'filterable':rule_codes[i].filterable or 0,
  81. }
  82. if rule_codes[i].multi:
  83. tmpObj['multi'] = 1
  84. new_rule_list.append(tmpObj)
  85. # print(new_rule_list)
  86. rules = {'list': new_rule_list, 'count': len(rule_list)}
  87. except Exception as e:
  88. logger.info(f'装载js内置源列表失败,置空内置源')
  89. rules = []
  90. logger.info(f'自动配置装载耗时:{get_interval(t1)}毫秒')
  91. return rules
  92. def getRules(path='cache',js_mode=0):
  93. t1 = time()
  94. base_path = path+'/' # 当前文件所在目录
  95. os.makedirs(base_path,exist_ok=True)
  96. file_name = os.listdir(base_path)
  97. file_name = list(filter(lambda x: str(x).endswith('.js') and str(x).find('模板') < 0, file_name))
  98. rule_list = [file.replace('.js', '') for file in file_name]
  99. js_path = [f'{path}/{rule}.js' for rule in rule_list]
  100. with open('js/模板.js', encoding='utf-8') as f:
  101. # before = f.read().split('export')[0]
  102. before = f.read().split('export default')[0]
  103. rule_codes = []
  104. ctx = Context()
  105. codes = []
  106. for i in range(len(js_path)):
  107. js = js_path[i]
  108. with open(js,encoding='utf-8') as f:
  109. code = f.read()
  110. new_code = 'var muban = JSON.parse(JSON.stringify(mubanDict));\n'+code.replace('var rule',f'var rule{i}',1)+f'\nif (rule{i}.模板 && muban.hasOwnProperty(rule{i}.模板))'+'{'+f'rule{i} = Object.assign(muban[rule{i}.模板], rule{i});'+'}'
  111. # new_code = ''+code.replace('rule',f'rule{i}',1)
  112. codes.append(new_code)
  113. newCodes = before + '\n'+ '\n'.join(codes)
  114. # print(newCodes)
  115. try:
  116. ctx.eval(newCodes)
  117. for i in range(len(js_path)):
  118. rule_codes.append(ctx.get(f'rule{i}'))
  119. # rule_codes 是个 js2py.base.JsObjectWrapper 类型,所以下面才能用. 获取属性
  120. new_rule_list = []
  121. for i in range(len(rule_list)):
  122. # 过滤排除drpy
  123. # if js_mode == 1 and rule_list[i] == 'drpy':
  124. # continue
  125. rule_codes[i] = ujson.loads(rule_codes[i].json())
  126. sable = rule_codes[i].get('searchable',0)
  127. tmpObj = {
  128. 'name':rule_list[i],
  129. # 'searchable':1 if (js_mode==1 and sable==2) else sable, # 对js模式1开放软件聚搜(还是算了,服务器遭不住)
  130. 'searchable':sable,
  131. 'quickSearch': rule_codes[i].get('quickSearch',0),
  132. 'filterable': rule_codes[i].get('filterable',0),
  133. }
  134. # print(tmpObj)
  135. if rule_codes[i].get('multi'):
  136. tmpObj['multi'] = 1
  137. new_rule_list.append(tmpObj)
  138. # print(new_rule_list)
  139. rules = {'list': new_rule_list, 'count': len(rule_list)}
  140. except Exception as e:
  141. logger.info(f'装载js内置源列表失败,置空内置源:{e}')
  142. rules = {'list': [], 'count': 0}
  143. logger.info(f'自动配置装载耗时:{get_interval(t1)}毫秒')
  144. return rules
  145. def jxTxt2Json(text:str,host:str):
  146. try:
  147. data = render_template_string(text,host=host).strip().split('\n')
  148. except Exception as e:
  149. logger.info(f'jxTxt2Json发生错误:{e}')
  150. data = []
  151. jxs = []
  152. for i in data:
  153. i = i.strip()
  154. dt = i.split(',')
  155. if not i.startswith('#') and len(i) > 10:
  156. try:
  157. jxs.append({
  158. 'name':dt[0],
  159. 'url':dt[1],
  160. 'type':dt[2] if len(dt) > 2 and dt[2] else 0,
  161. 'ua':dt[3] if len(dt) > 3 and dt[3] else UA,
  162. })
  163. except Exception as e:
  164. logger.info(f'解析行有错误:{e}')
  165. return jxs
  166. def getJxs(path='js',host=None):
  167. custom_jx = 'base/解析.conf'
  168. if not os.path.exists(custom_jx):
  169. with open(custom_jx,'w+',encoding='utf-8') as f1:
  170. msg = """# 这是用户自定义解析列表,不会被系统升级覆盖
  171. # 0123,对应,普通解析,json解析,并发多json解析,聚合解析,参数3不填默认0
  172. # flags是线路名称标识,会自动拦截并走以下的解析
  173. # 名称,链接,类型,ua (ua不填默认 Mozilla/5.0) 可以手动填 Dart/2.14 (dart:io)
  174. 虾米,https://dm.xmflv.com:4433/?url=
  175. """
  176. f1.write(msg)
  177. base_path = 'jiexi' # 自建解析目录
  178. os.makedirs(base_path, exist_ok=True)
  179. file_name = os.listdir(base_path)
  180. file_name = list(filter(lambda x: str(x).endswith('.js') and str(x).find('模板') < 0 and str(x).find('加密') < 0, file_name))
  181. # print(file_name)
  182. jx_list = [file.replace('.js', '') for file in file_name]
  183. # print(file_name)
  184. # print(jx_list)
  185. jx_str = '\n'.join([jx+',{{host}}'+f'/parse/api/{jx}.js?url=,1' for jx in jx_list])
  186. # print(jx_str)
  187. with open(f'{path}/解析.conf',encoding='utf-8') as f:
  188. text = f.read()
  189. text = jx_str + '\n' + text
  190. jxs = jxTxt2Json(text,host)
  191. with open(custom_jx,encoding='utf-8') as f2:
  192. text = f2.read()
  193. jxs2 = jxTxt2Json(text,host)
  194. jxs.extend(jxs2)
  195. print(f'共计{len(jxs)}条解析')
  196. return jxs
  197. def getPys(path='txt/py'):
  198. t1 = time()
  199. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  200. py_path = os.path.join(base_path, path)
  201. os.makedirs(py_path, exist_ok=True)
  202. file_name = os.listdir(py_path)
  203. file_name = list(filter(lambda x: str(x).endswith('.py'), file_name))
  204. # print(file_name)
  205. rule_list = [file.replace('.py', '') for file in file_name]
  206. py_path = [f'{path}/{rule}.py' for rule in rule_list]
  207. new_rule_list = []
  208. for i in range(len(rule_list)):
  209. new_rule_list.append({
  210. 'name': rule_list[i],
  211. 'searchable': 1,
  212. 'quickSearch': 1,
  213. 'filterable': 0,
  214. })
  215. logger.info(f'自动加载Pyramid耗时:{get_interval(t1)}毫秒')
  216. return new_rule_list
  217. def gen_cache(path='txt/js/tg'):
  218. t1 = time()
  219. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  220. py_path = os.path.join(base_path, path)
  221. os.makedirs(py_path, exist_ok=True)
  222. file_name = os.listdir(py_path)
  223. file_name = list(filter(lambda x: str(x).endswith('.js'), file_name))
  224. # print(file_name)
  225. rule_list = [file.replace('.js', '') for file in file_name]
  226. js_path = [f'{path}/{rule}.js' for rule in rule_list]
  227. new_rule_list = []
  228. for i in range(len(rule_list)):
  229. # print(js_path[i])
  230. rname = rule_list[i]
  231. new_rule_list.append(
  232. {
  233. "key": f"dr_{rname}",
  234. "name": f"{rname}(道长)",
  235. "type": 1,
  236. # "api": "{{host}}"+f"/vod?rule={rname}&ext="+"{{host}}/"+js_path[i],
  237. "api": "{{host}}"+f"/vod?rule={rname}&ext="+js_path[i],
  238. "searchable": 2,
  239. "quickSearch": 0,
  240. "filterable": 0
  241. })
  242. logger.info(f'自动加载{len(new_rule_list)}个缓存JS耗时:{get_interval(t1)}毫秒')
  243. new_rules_texts = [json.dumps(new_rule,ensure_ascii=False) for new_rule in new_rule_list]
  244. # new_rules_text = json.dumps(new_rule_list,ensure_ascii=False)
  245. new_rules_text = ',\n'.join(new_rules_texts)+','
  246. return new_rules_text
  247. if __name__ == '__main__':
  248. print(getRuleLists())
  249. # print(gen_cache())
  250. # print(gen_cache('txt/js/18'))
  251. custom_file = gen_cache() + '\n'+gen_cache('txt/js/18')
  252. print(custom_file)
  253. with open('custom.conf','w+',encoding='utf-8') as f:
  254. f.write(custom_file)