files.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : files.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/6
  6. import os
  7. import shutil
  8. from utils.system import getHost
  9. from controllers.service import storage_service
  10. from utils.encode import base64Encode,parseText
  11. from flask import render_template_string
  12. from utils.log import logger
  13. def getPics(path='images'):
  14. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  15. img_path = os.path.join(base_path, f'{path}')
  16. os.makedirs(img_path,exist_ok=True)
  17. file_name = os.listdir(img_path)
  18. # file_name = list(filter(lambda x: str(x).endswith('.js') and str(x).find('模板') < 0, file_name))
  19. # print(file_name)
  20. pic_list = [img_path+'/'+file for file in file_name]
  21. # pic_list = file_name
  22. # print(type(pic_list))
  23. return pic_list
  24. def get_live_url(new_conf,mode):
  25. host = getHost(mode)
  26. lsg = storage_service()
  27. # t1 = time()
  28. # live_url = host + '/lives' if new_conf.get('LIVE_MODE',1) == 0 else lsg.getItem('LIVE_URL',getHost(2)+'/lives')
  29. live_url = host + '/lives' if lsg.getItem('LIVE_MODE',1) == 0 else lsg.getItem('LIVE_URL',getHost(2)+'/lives')
  30. live_url = base64Encode(live_url)
  31. # print(f'{get_interval(t1)}毫秒')
  32. return live_url
  33. def getAlist():
  34. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  35. alist_path = os.path.join(base_path, 'js/alist.conf')
  36. alist_cpath = os.path.join(base_path, 'base/alist.conf')
  37. try:
  38. if not os.path.exists(alist_cpath):
  39. shutil.copy(alist_path, alist_cpath) # 复制文件
  40. with open(alist_cpath,encoding='utf-8') as f:
  41. data = f.read().strip()
  42. alists = []
  43. for i in data.split('\n'):
  44. i = i.strip()
  45. dt = i.split(',')
  46. if not i.strip().startswith('#'):
  47. obj = {
  48. 'name': dt[0],
  49. 'server': dt[1],
  50. 'type':"alist",
  51. }
  52. if len(dt) > 2:
  53. obj.update({
  54. 'password': dt[2]
  55. })
  56. alists.append(obj)
  57. print(f'共计{len(alists)}条alist记录')
  58. return alists
  59. except Exception as e:
  60. print(f'获取alist列表失败:{e}')
  61. return []
  62. def custom_merge(original:dict,custom:dict):
  63. """
  64. 合并用户配置
  65. :param original: 原始配置
  66. :param custom: 自定义配置
  67. :return:
  68. """
  69. if not custom or len(custom.keys()) < 1:
  70. return original
  71. new_keys = custom.keys()
  72. updateObj = {}
  73. extend_obj = {}
  74. for key in ['wallpaper','spider','homepage','lives','hotSearch','sniffer','recommend','rating','rules']:
  75. if key in new_keys:
  76. updateObj[key] = custom[key]
  77. for key in ['drives','sites','flags','ads','parses']:
  78. if key in new_keys:
  79. extend_obj[key] = custom[key]
  80. original.update(updateObj)
  81. for key in extend_obj.keys():
  82. # original[key].extend(extend_obj[key])
  83. # print(key,original.get(key))
  84. if original.get(key) and isinstance(original[key],list):
  85. original[key].extend(extend_obj[key])
  86. else:
  87. original[key] = extend_obj[key]
  88. logger.info(f'合并配置共有解析数量:{len(original.get("parses"))}')
  89. return original
  90. def getCustonDict(host,ali_token='',js0_password=''):
  91. customFile = 'base/custom.conf'
  92. if not os.path.exists(customFile):
  93. with open(customFile, 'w+', encoding='utf-8') as f:
  94. f.write('{}')
  95. customConfig = False
  96. try:
  97. with open(customFile,'r',encoding='utf-8') as f:
  98. text = f.read()
  99. customConfig = parseText(render_template_string(text,host=host,ali_token=ali_token,js0_password=js0_password))
  100. except Exception as e:
  101. logger.info(f'用户自定义配置加载失败:{e}')
  102. return customConfig
  103. def get_multi_rules(rules):
  104. lsg = storage_service()
  105. multi_mode = lsg.getItem('MULTI_MODE',0)
  106. fix_multi = ['drpy']
  107. if not multi_mode or str(multi_mode)=='0':
  108. rules['list'] = list(filter(lambda x: x['name'] in fix_multi or x.get('multi'), rules['list']))
  109. rules['count'] = len(rules['list'])
  110. # print(rules)
  111. return rules