layui.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : layui.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/14
  6. import ujson
  7. from flask import Blueprint,request,render_template,jsonify,make_response,redirect
  8. from utils.ua import UA
  9. from utils.web import getParmas,get_interval,layuiBack,verfy_token
  10. from utils.cfg import cfg
  11. from controllers.service import storage_service,rules_service,parse_service
  12. from utils.system import getHost
  13. from utils.files import getCustonDict,custom_merge
  14. from utils.encode import parseText
  15. from js.rules import getRules,getPys,getJxs
  16. from operator import itemgetter, attrgetter
  17. import functools
  18. layui = Blueprint("layui", __name__)
  19. @layui.route('/')
  20. def hello(): # put application's code here
  21. return jsonify({'msg':'hello layui'})
  22. @layui.route('/index')
  23. def layui_index(): # put application's code here
  24. # return render_template('layui_index.html')
  25. if not verfy_token():
  26. return render_template('login.html')
  27. return render_template('layui_list.html')
  28. @layui.route('/jxs')
  29. def layui_jxs(): # put application's code here
  30. # return render_template('layui_index.html')
  31. if not verfy_token():
  32. return render_template('login.html')
  33. return render_template('layui_jxs.html')
  34. def comp(x, y):
  35. if x['order'] > y['order']:
  36. return 1
  37. elif x['order'] < y['order']:
  38. return - 1
  39. else:
  40. if x['write_date'] < y['write_date']:
  41. return 1
  42. elif x['write_date'] > y['write_date']:
  43. return -1
  44. else:
  45. return 0
  46. @layui.route('/api/list')
  47. def layui_rule_list():
  48. page = int(getParmas('page',1))
  49. limit = int(getParmas('limit',10))
  50. # print(f'page:{page},limit:{limit}')
  51. new_conf = cfg
  52. lsg = storage_service()
  53. store_conf_dict = lsg.getStoreConfDict()
  54. new_conf.update(store_conf_dict)
  55. host = getHost(2)
  56. customConfig = getCustonDict(host)
  57. jxs = []
  58. js0_password = lsg.getItem('JS0_PASSWORD')
  59. lsg = storage_service()
  60. use_py = lsg.getItem('USE_PY')
  61. pys = getPys() if use_py else []
  62. # print(pys)
  63. alists = []
  64. live_url = []
  65. local_rules = getRules('js')
  66. # print(local_rules)
  67. html = render_template('config.txt', pys=pys, rules=local_rules, host=host, mode=2, jxs=jxs, alists=alists,
  68. alists_str='[]', live_url=live_url, config=new_conf)
  69. # html = render_template('config.txt', js0_password=js0_password, UA=UA, xr_mode=1, ISTVB=1, pys=pys,
  70. # rules=local_rules, host=host, mode=2, js_mode=1, jxs=jxs, alists=alists,
  71. # alists_str='[]', live_url=live_url, config=new_conf)
  72. merged_config = custom_merge(parseText(html), customConfig)
  73. sites = merged_config['sites']
  74. rules = rules_service()
  75. rule_list = rules.query_all()
  76. rule_names = list(map(lambda x:x['name'],rule_list))
  77. # print(rule_list)
  78. # print(rule_names)
  79. # print(sites)
  80. for i in range(len(sites)):
  81. sites[i]['id'] = i+1
  82. site_name = sites[i]['api'].split('rule=')[1].split('&')[0] if 'rule=' in sites[i]['api'] else sites[i]['key']
  83. if not str(sites[i]['key']).startswith('dr_') and site_name == 'drpy':
  84. site_name = sites[i]['key']
  85. # print(sites[i])
  86. if site_name in rule_names:
  87. site_rule = rule_list[rule_names.index(site_name)]
  88. sites[i]['state'] = 1 if site_rule['state'] is None else site_rule['state']
  89. sites[i]['order'] = 0 if site_rule['order'] is None else site_rule['order']
  90. sites[i]['write_date'] = 0 if site_rule['write_date'] is None else site_rule['write_date'].timestamp()
  91. else:
  92. sites[i]['state'] = 1
  93. sites[i]['order'] = 0
  94. sites[i]['write_date'] = 0
  95. sites[i]['site_name'] = site_name
  96. def multisort(sites, specs):
  97. """
  98. https://zhuanlan.zhihu.com/p/109269549?utm_id=0
  99. 多重排序,来自知乎的代码.明显只对了最后的元素进行排序.看完评论和实践发现不对
  100. :param sites:
  101. :param specs:
  102. :return:
  103. """
  104. for key, reverse in specs:
  105. # sites.sort(key=attrgetter(key), reverse=reverse)
  106. sites.sort(key=lambda x:x[key], reverse=reverse)
  107. return sites
  108. # multisort(sites, (('order', False), ('write_date', True)))
  109. # sites.sort(key=lambda x:x['order'],reverse=False)
  110. sites.sort(key=functools.cmp_to_key(comp),reverse=False)
  111. new_sites = sites[(page-1)*limit:page*limit]
  112. # print(new_sites)
  113. return layuiBack('获取成功',new_sites,count=len(sites))
  114. @layui.route('/api/jx_list')
  115. def layui_jx_list():
  116. # 拖拽排序教程 https://blog.csdn.net/qq_41829337/article/details/126610406
  117. host = request.host_url.rstrip('/') # 获取当前访问链接对应的host
  118. page = int(getParmas('page',1))
  119. limit = int(getParmas('limit',10))
  120. new_conf = cfg
  121. lsg = storage_service()
  122. store_conf_dict = lsg.getStoreConfDict()
  123. new_conf.update(store_conf_dict)
  124. ali_token = new_conf.ALI_TOKEN
  125. xr_mode = new_conf.XR_MODE
  126. js0_password = new_conf.JS0_PASSWORD
  127. js_mode = int(new_conf.JS_MODE or 0)
  128. customConfig = getCustonDict(host, ali_token, js0_password)
  129. jxs = getJxs(host=host)
  130. rules = {'list': [{"key": "dr_MXONE", "name": "MXONE(道长)", "type": 1, "api": "{{host}}/vod?{% if js0_password %}pwd={{js0_password}}&{% endif %}rule=MXONE&ext=txt/js/tg/MXONE.js", "searchable": 2, "quickSearch": 0, "filterable": 0},
  131. ], 'count': 1}
  132. html = render_template('config.txt', js0_password=js0_password, UA=UA, xr_mode=xr_mode, ISTVB=False, pys=[],
  133. rules=rules, host=host, mode=2, js_mode=js_mode, jxs=jxs, alists=[],
  134. alists_str='', live_url='', config=new_conf)
  135. merged_config = custom_merge(parseText(html), customConfig)
  136. parses = merged_config['parses']
  137. parse = parse_service()
  138. parse_list = parse.query_all()
  139. parse_url_list = list(map(lambda x:x['url'],parse_list))
  140. for i in range(len(parses)):
  141. parses[i]['id'] = i+1
  142. if str(parses[i]['url']).startswith(host):
  143. parses[i]['url'] = parses[i]['url'].replace(host,'')
  144. if parses[i]['url'] in parse_url_list:
  145. parse_rule = parse_list[parse_url_list.index(parses[i]['url'])]
  146. parses[i]['state'] = 1 if parse_rule['state'] is None else parse_rule['state']
  147. parses[i]['order'] = 0 if parse_rule['order'] is None else parse_rule['order']
  148. parses[i]['write_date'] = 0 if parse_rule['write_date'] is None else parse_rule['write_date'].timestamp()
  149. else:
  150. parses[i]['state'] = 1
  151. parses[i]['order'] = 0
  152. parses[i]['write_date'] = 0
  153. if not parses[i].get('header'):
  154. parses[i]['header'] = {'User-Agent': 'Mozilla/5.0'}
  155. if isinstance(parses[i].get('header'),dict):
  156. parses[i]['header'] = ujson.dumps(parses[i]['header'],ensure_ascii=False)
  157. if isinstance(parses[i].get('ext'),dict):
  158. parses[i]['ext'] = ujson.dumps(parses[i]['ext'],ensure_ascii=False)
  159. parses.sort(key=functools.cmp_to_key(comp), reverse=False)
  160. new_parses = parses[(page - 1) * limit:page * limit]
  161. return layuiBack('获取成功', new_parses, count=len(parses))