files.py 3.9 KB
Newer Older
H
hjdhnx 已提交
1 2 3 4 5 6 7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File  : files.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date  : 2022/9/6

import os
H
hjdhnx 已提交
8 9
import shutil

H
hjdhnx 已提交
10 11
from utils.system import getHost
from controllers.service import storage_service
H
hjdhnx 已提交
12
from utils.encode import base64Encode,parseText
H
hjdhnx 已提交
13 14
from flask import render_template_string
from utils.log import logger
H
hjdhnx 已提交
15 16 17 18 19 20 21 22

def getPics(path='images'):
    base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))  # 上级目录
    img_path = os.path.join(base_path, f'{path}')
    os.makedirs(img_path,exist_ok=True)
    file_name = os.listdir(img_path)
    # file_name = list(filter(lambda x: str(x).endswith('.js') and str(x).find('模板') < 0, file_name))
    # print(file_name)
H
hjdhnx 已提交
23
    pic_list = [img_path+'/'+file for file in file_name]
H
hjdhnx 已提交
24 25 26 27 28 29 30 31
    # pic_list = file_name
    # print(type(pic_list))
    return pic_list

def get_live_url(new_conf,mode):
    host = getHost(mode)
    lsg = storage_service()
    # t1 = time()
H
hjdhnx 已提交
32 33
    # live_url = host + '/lives' if new_conf.get('LIVE_MODE',1) == 0 else lsg.getItem('LIVE_URL',getHost(2)+'/lives')
    live_url = host + '/lives' if lsg.getItem('LIVE_MODE',1) == 0 else lsg.getItem('LIVE_URL',getHost(2)+'/lives')
H
hjdhnx 已提交
34 35 36 37 38 39 40
    live_url = base64Encode(live_url)
    # print(f'{get_interval(t1)}毫秒')
    return live_url

def getAlist():
    base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__)))  # 上级目录
    alist_path = os.path.join(base_path, 'js/alist.conf')
H
hjdhnx 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
    alist_cpath = os.path.join(base_path, 'base/alist.conf')
    try:
        if not os.path.exists(alist_cpath):
            shutil.copy(alist_path, alist_cpath)  # 复制文件
        with open(alist_cpath,encoding='utf-8') as f:
            data = f.read().strip()
        alists = []
        for i in data.split('\n'):
            i = i.strip()
            dt = i.split(',')
            if not i.strip().startswith('#'):
                obj = {
                    'name': dt[0],
                    'server': dt[1],
                    'type':"alist",
                }
                if len(dt) > 2:
                    obj.update({
                        'password': dt[2]
                    })
                alists.append(obj)
        print(f'共计{len(alists)}条alist记录')
        return alists
    except Exception as e:
        print(f'获取alist列表失败:{e}')
        return []
H
hjdhnx 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79

def custom_merge(original:dict,custom:dict):
    """
    合并用户配置
    :param original: 原始配置
    :param custom: 自定义配置
    :return:
    """
    if not custom or len(custom.keys()) < 1:
        return original
    new_keys = custom.keys()
    updateObj = {}
    extend_obj = {}
H
hjdhnx 已提交
80
    for key in ['wallpaper','spider','homepage','lives','hotSearch','sniffer','recommend','rating']:
H
hjdhnx 已提交
81 82 83
        if key in new_keys:
            updateObj[key] = custom[key]

H
hjdhnx 已提交
84
    for key in ['drives','sites','flags','ads']:
H
hjdhnx 已提交
85 86 87 88 89 90
        if key in new_keys:
            extend_obj[key] = custom[key]

    original.update(updateObj)
    for key in extend_obj.keys():
        original[key].extend(extend_obj[key])
H
hjdhnx 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104
    return original

def getCustonDict(host):
    customFile = 'base/custom.conf'
    if not os.path.exists(customFile):
        with open(customFile, 'w+', encoding='utf-8') as f:
            f.write('{}')
    customConfig = False
    try:
        with open(customFile,'r',encoding='utf-8') as f:
            text = f.read()
            customConfig = parseText(render_template_string(text,host=host))
    except Exception as e:
        logger.info(f'用户自定义配置加载失败:{e}')
H
hjdhnx 已提交
105 106 107 108
    return customConfig

def get_multi_rules(rules):
    lsg = storage_service()
H
hjdhnx 已提交
109
    multi_mode = lsg.getItem('MULTI_MODE',0)
H
hjdhnx 已提交
110
    fix_multi = ['drpy']
H
hjdhnx 已提交
111
    if not multi_mode or str(multi_mode)=='0':
H
hjdhnx 已提交
112 113 114 115
        rules['list'] = list(filter(lambda x: x['name'] in fix_multi or x.get('multi'), rules['list']))
        rules['count'] = len(rules['list'])
        # print(rules)
    return rules