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

from base.R import copy_utils
from models.storage import Storage
H
hjdhnx 已提交
9
from models.ruleclass import RuleClass
H
hjdhnx 已提交
10
from models.vipParse import VipParse
H
hjdhnx 已提交
11
from utils.cfg import cfg
H
hjdhnx 已提交
12
from base.database import db
H
hjdhnx 已提交
13
from datetime import datetime,timedelta
H
hjdhnx 已提交
14 15 16 17 18 19 20 21 22 23

class storage_service(object):

    @staticmethod
    def query_all():
        # 查询所有
        res = Storage.query.all()
        return copy_utils.obj_to_list(res)

    def __init__(self):
H
hjdhnx 已提交
24
        conf_list = ['LIVE_URL', 'USE_PY', 'JS_MODE','JS0_DISABLE','JS0_PASSWORD','PLAY_URL', 'PLAY_DISABLE', 'LAZYPARSE_MODE', 'WALL_PAPER_ENABLE',
H
hjdhnx 已提交
25
                     'WALL_PAPER', 'UNAME', 'PWD', 'LIVE_MODE', 'CATE_EXCLUDE', 'TAB_EXCLUDE','SEARCH_TIMEOUT','MULTI_MODE','XR_MODE','ALI_TOKEN']
H
hjdhnx 已提交
26 27 28 29
        for conf in conf_list:
            if not self.hasItem(conf):
                print(f'开始初始化{conf}')
                self.setItem(conf, cfg.get(conf))
H
hjdhnx 已提交
30

H
hjdhnx 已提交
31 32
    @classmethod
    def getStoreConf(self):
H
hjdhnx 已提交
33
        # MAX_CONTENT_LENGTH 最大上传和端口ip一样是顶级配置,无法外部修改的
H
hjdhnx 已提交
34
        conf_list = ['LIVE_URL', 'LIVE_MODE','PLAY_URL', 'PID_URL','USE_PY','JS_MODE', 'JS0_DISABLE','JS0_PASSWORD','PLAY_DISABLE', 'LAZYPARSE_MODE', 'WALL_PAPER_ENABLE',
H
hjdhnx 已提交
35
                     'WALL_PAPER', 'UNAME', 'PWD',  'CATE_EXCLUDE', 'TAB_EXCLUDE','SEARCH_TIMEOUT','MULTI_MODE','XR_MODE','JS_PROXY','ALI_TOKEN']
H
hjdhnx 已提交
36
        conf_name_list = ['直播地址', '直播模式','远程地址', '进程管理链接','启用py源', 'js模式','禁用js0','js0密码','禁用免嗅', '免嗅模式', '启用壁纸', '壁纸链接', '管理账号',
H
hjdhnx 已提交
37
                          '管理密码',  '分类排除', '线路排除','聚搜超时','多源模式','仙人模式','源代理','阿里tk']
H
hjdhnx 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
        conf_lists = []
        for i in range(len(conf_list)):
            conf = conf_list[i]
            conf_lists.append({
                'key': conf,
                'value': self.getItem(conf),
                'name': conf_name_list[i]
            })
        return conf_lists

    @classmethod
    def getStoreConfDict(self):
        store_conf = self.getStoreConf()
        store_conf_dict = {}
        for stc in store_conf:
            store_conf_dict[stc['key']] = stc['value']
        return store_conf_dict
H
hjdhnx 已提交
55

H
hjdhnx 已提交
56 57
    @classmethod
    def getItem(self, key, value=''):
H
hjdhnx 已提交
58
        res = Storage.getItem(key,value)
H
hjdhnx 已提交
59
        if str(res) == '0' or str(res) == 'false' or str(res) == 'False':
H
hjdhnx 已提交
60 61 62 63 64 65
            return 0
        return res

    @classmethod
    def hasItem(self, key):
        return Storage.hasItem(key)
H
hjdhnx 已提交
66 67 68 69 70 71 72

    @classmethod
    def setItem(self,key, value):
        return Storage.setItem(key, value)

    @classmethod
    def clearItem(self,key):
H
hjdhnx 已提交
73 74 75 76 77 78 79
        return Storage.clearItem(key)

class rules_service(object):

    @staticmethod
    def query_all():
        # 查询所有
H
hjdhnx 已提交
80 81 82
        res = RuleClass.query.order_by(RuleClass.order.asc(),RuleClass.write_date.desc()).all()
        # print(res)
        # res = RuleClass.query.order_by(RuleClass.write_date.asc()).all()
H
hjdhnx 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
        return copy_utils.obj_to_list(res)

    @classmethod
    def hasItem(self, key):
        return RuleClass.hasItem(key)

    def getState(self,key):
        res = RuleClass.query.filter(RuleClass.name == key).first()
        if not res:
            return 1
        # print(res)
        state = res.state
        if state is None:
            state = 1
        return state or 0

H
hjdhnx 已提交
99

H
hjdhnx 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    def setState(self,key,state=0):
        res = RuleClass.query.filter(RuleClass.name == key).first()
        if res:
            res.state = state
            db.session.add(res)
        else:
            res = RuleClass(name=key, state=state)
            db.session.add(res)
            db.session.flush()  # 获取id
        try:
            db.session.commit()
            return res.id
        except Exception as e:
            print(f'发生了错误:{e}')
H
hjdhnx 已提交
114 115
            return None

H
hjdhnx 已提交
116 117 118 119
    def setOrder(self,key,order=0):
        res = RuleClass.query.filter(RuleClass.name == key).first()
        if res:
            res.order = order
H
hjdhnx 已提交
120 121 122 123
            # print(f'{res.name}设置order为:{order}')
            if res.order == order:
                res.write_date = datetime.now()
                # res.write_date = res.write_date + timedelta(hours=2)
H
hjdhnx 已提交
124 125 126 127 128 129 130 131 132 133 134 135
            db.session.add(res)
        else:
            res = RuleClass(name=key, order=order)
            db.session.add(res)
            db.session.flush()  # 获取id
        try:
            db.session.commit()
            return res.id
        except Exception as e:
            print(f'发生了错误:{e}')
            return None

H
hjdhnx 已提交
136 137 138
    @staticmethod
    def getHideRules():
        res = RuleClass.query.filter(RuleClass.state == 0).all()
H
hjdhnx 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        return copy_utils.obj_to_list(res)

class parse_service(object):

    @staticmethod
    def query_all():
        # 查询所有
        res = VipParse.query.order_by(VipParse.order.asc(),VipParse.write_date.desc()).all()
        # print(res)
        # res = RuleClass.query.order_by(RuleClass.write_date.asc()).all()
        return copy_utils.obj_to_list(res)

    @classmethod
    def hasItem(self, key):
        return VipParse.hasItem(key)

    def getState(self,key):
        res = VipParse.query.filter(VipParse.url == key).first()
        if not res:
            return 1
        # print(res)
        state = res.state
        if state is None:
            state = 1
        return state or 0


    def setState(self,key,state=0):
        res = VipParse.query.filter(VipParse.url == key).first()
        if res:
            res.state = state
            db.session.add(res)
        else:
            res = VipParse(url=key, state=state)
            db.session.add(res)
            db.session.flush()  # 获取id
        try:
            db.session.commit()
            return res.id
        except Exception as e:
            print(f'发生了错误:{e}')
            return None

    def setOrder(self,key,order=0):
        res = VipParse.query.filter(VipParse.url == key).first()
        if res:
            res.order = order
            # print(f'{res.name}设置order为:{order}')
            if res.order == order:
                res.write_date = datetime.now()
                # res.write_date = res.write_date + timedelta(hours=2)
            db.session.add(res)
        else:
            res = VipParse(url=key, order=order)
            db.session.add(res)
            db.session.flush()  # 获取id
        try:
            db.session.commit()
            return res.id
        except Exception as e:
            print(f'发生了错误:{e}')
            return None

    def setEverything(self,key,name,state,typeno,order,ext,header):
        res = VipParse.query.filter(VipParse.url == key).first()
        if res:
            res.name = name
            res.state = state
            res.type = typeno
            res.order = order
            res.ext = ext
            res.header = header
            res.write_date = datetime.now()
            db.session.add(res)
        else:
            res = VipParse(name=name,url=key,state=state,type=typeno,order=order,ext=ext,header=header)
            db.session.add(res)
            db.session.flush()  # 获取id
        try:
            db.session.commit()
            return res.id
        except Exception as e:
            print(f'发生了错误:{e}')
            return None

    def saveData(self,obj):
        """
        db.session.add_all([]) 可以一次性保存多条数据,但是这里用不到,因为涉及修改和新增一起的
        :param obj:
        :return:
        """
        # res = VipParse.query.filter(VipParse.url == obj['url']).first()
        res = VipParse.query.filter_by(url=obj['url']).first()
        if res:
            # res.update(obj)
            res.name = obj['name']
            res.state = obj['state']
            res.type = obj['type']
            res.order = obj['order']
            res.ext = obj['ext']
            res.header = obj['header']
            db.session.add(res)
        else:
            res = VipParse(**obj)
            db.session.add(res)
            db.session.flush()  # 获取id
        try:
            db.session.commit()
            return res.id
        except Exception as e:
            print(f'发生了错误:{e}')
            return None

    @staticmethod
    def getHideRules():
        res = VipParse.query.filter(VipParse.state == 0).all()
        return copy_utils.obj_to_list(res)