files.py 2.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 12 13 14 15 16 17 18 19 20
from utils.system import getHost
from utils.encode import base64Encode
from controllers.service import storage_service

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 已提交
21
    pic_list = [img_path+'/'+file for file in file_name]
H
hjdhnx 已提交
22 23 24 25 26 27 28 29
    # 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 已提交
30 31
    # 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 已提交
32 33 34 35 36 37 38
    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 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
    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 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77

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 已提交
78
    for key in ['wallpaper','spider','homepage','lives']:
H
hjdhnx 已提交
79 80 81
        if key in new_keys:
            updateObj[key] = custom[key]

H
hjdhnx 已提交
82
    for key in ['drives','sites','flags','ads']:
H
hjdhnx 已提交
83 84 85 86 87 88 89
        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])
    return original