files.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 get_jar_list():
  63. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  64. jar_path = os.path.join(base_path, 'libs/jar')
  65. if not os.path.exists(jar_path):
  66. os.makedirs(jar_path, exist_ok=True)
  67. logger.info(f'初始化{jar_path}目录')
  68. jars = os.listdir(jar_path)
  69. jars = list(filter(lambda x: str(x).endswith('.jar') and str(x).find('base') < 0, jars))
  70. # print(jars)
  71. # jar_list = [file.replace('.jar', '') for file in jars]
  72. return jars
  73. def get_drop_js(jsd_list):
  74. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  75. js_path = os.path.join(base_path, 'js')
  76. js_list = [os.path.join(js_path, jsd.replace('jsd','js')) for jsd in jsd_list]
  77. return js_list
  78. def get_jsd_list():
  79. base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
  80. js_path = os.path.join(base_path, 'js')
  81. if not os.path.exists(js_path):
  82. os.makedirs(js_path, exist_ok=True)
  83. logger.info(f'初始化{js_path}目录')
  84. jsds = os.listdir(js_path)
  85. jsds = list(filter(lambda x: str(x).endswith('.jsd'), jsds))
  86. return jsds
  87. def custom_merge(original:dict,custom:dict):
  88. """
  89. 合并用户配置
  90. :param original: 原始配置
  91. :param custom: 自定义配置
  92. :return:
  93. """
  94. if not custom or len(custom.keys()) < 1:
  95. return original
  96. new_keys = custom.keys()
  97. updateObj = {}
  98. extend_obj = {}
  99. for key in ['wallpaper','spider','homepage','lives','hotSearch','sniffer','recommend','rating','rules']:
  100. if key in new_keys:
  101. updateObj[key] = custom[key]
  102. for key in ['drives','sites','flags','ads','parses']:
  103. if key in new_keys:
  104. extend_obj[key] = custom[key]
  105. original.update(updateObj)
  106. for key in extend_obj.keys():
  107. # original[key].extend(extend_obj[key])
  108. # print(key,original.get(key))
  109. if original.get(key) and isinstance(original[key],list):
  110. original[key].extend(extend_obj[key])
  111. else:
  112. original[key] = extend_obj[key]
  113. logger.info(f'合并配置共有解析数量:{len(original.get("parses"))}')
  114. return original
  115. def getCustonDict(host,ali_token='',js0_password=''):
  116. customFile = 'base/custom.conf'
  117. if not os.path.exists(customFile):
  118. with open(customFile, 'w+', encoding='utf-8') as f:
  119. f.write('{}')
  120. customConfig = False
  121. try:
  122. with open(customFile,'r',encoding='utf-8') as f:
  123. text = f.read()
  124. customConfig = parseText(render_template_string(text,host=host,ali_token=ali_token,js0_password=js0_password))
  125. print(customConfig)
  126. except Exception as e:
  127. logger.info(f'用户自定义配置加载失败:{e}')
  128. return customConfig
  129. def get_multi_rules(rules):
  130. lsg = storage_service()
  131. multi_mode = lsg.getItem('MULTI_MODE',0)
  132. fix_multi = ['drpy']
  133. if not multi_mode or str(multi_mode)=='0':
  134. rules['list'] = list(filter(lambda x: x['name'] in fix_multi or x.get('multi'), rules['list']))
  135. rules['count'] = len(rules['list'])
  136. # print(rules)
  137. return rules