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

import os
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 已提交
19
    pic_list = [img_path+'/'+file for file in file_name]
H
hjdhnx 已提交
20 21 22 23 24 25 26 27
    # 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 已提交
28 29
    # 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 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    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')
    with open(alist_path,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记录')
H
hjdhnx 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68
    return alists

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 已提交
69
    for key in ['wallpaper','spider','homepage','lives']:
H
hjdhnx 已提交
70 71 72
        if key in new_keys:
            updateObj[key] = custom[key]

H
hjdhnx 已提交
73
    for key in ['drives','sites','flags','ads']:
H
hjdhnx 已提交
74 75 76 77 78 79 80
        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