service.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : service.py.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/6
  6. from base.R import copy_utils
  7. from models.storage import Storage
  8. from models.ruleclass import RuleClass
  9. from models.vipParse import VipParse
  10. from utils.cfg import cfg
  11. from base.database import db
  12. from datetime import datetime,timedelta
  13. class storage_service(object):
  14. @staticmethod
  15. def query_all():
  16. # 查询所有
  17. res = Storage.query.all()
  18. return copy_utils.obj_to_list(res)
  19. def __init__(self):
  20. conf_list = ['LIVE_URL', 'USE_PY', 'JS_MODE','JS0_DISABLE','JS0_PASSWORD','PLAY_URL', 'PLAY_DISABLE', 'LAZYPARSE_MODE', 'WALL_PAPER_ENABLE',
  21. 'WALL_PAPER', 'UNAME', 'PWD', 'LIVE_MODE', 'CATE_EXCLUDE', 'TAB_EXCLUDE','SEARCH_TIMEOUT','SEARCH_LIMIT','MULTI_MODE','XR_MODE','JS_PROXY','ENV','ALI_TOKEN']
  22. for conf in conf_list:
  23. if not self.hasItem(conf):
  24. print(f'开始初始化{conf}')
  25. self.setItem(conf, cfg.get(conf))
  26. @classmethod
  27. def getStoreConf(self):
  28. # MAX_CONTENT_LENGTH 最大上传和端口ip一样是顶级配置,无法外部修改的
  29. 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',
  30. 'WALL_PAPER', 'UNAME', 'PWD', 'CATE_EXCLUDE', 'TAB_EXCLUDE','SEARCH_TIMEOUT','SEARCH_LIMIT','MULTI_MODE','XR_MODE','JS_PROXY','ENV','ALI_TOKEN']
  31. conf_name_list = ['直播地址', '直播模式','远程地址', '进程管理链接','启用py源', 'js模式','禁用js0','js0密码','禁用免嗅', '免嗅模式', '启用壁纸', '壁纸链接', '管理账号',
  32. '管理密码', '分类排除', '线路排除','聚搜超时','搜索条数','多源模式','仙人模式','源代理','环境变量','阿里tk']
  33. conf_lists = []
  34. for i in range(len(conf_list)):
  35. conf = conf_list[i]
  36. conf_lists.append({
  37. 'key': conf,
  38. 'value': self.getItem(conf),
  39. 'name': conf_name_list[i]
  40. })
  41. return conf_lists
  42. @classmethod
  43. def getStoreConfDict(self):
  44. store_conf = self.getStoreConf()
  45. store_conf_dict = {}
  46. for stc in store_conf:
  47. store_conf_dict[stc['key']] = stc['value']
  48. return store_conf_dict
  49. @classmethod
  50. def getItem(self, key, value=''):
  51. res = Storage.getItem(key,value)
  52. if str(res) == '0' or str(res) == 'false' or str(res) == 'False':
  53. return 0
  54. return res
  55. @classmethod
  56. def hasItem(self, key):
  57. return Storage.hasItem(key)
  58. @classmethod
  59. def setItem(self,key, value):
  60. return Storage.setItem(key, value)
  61. @classmethod
  62. def clearItem(self,key):
  63. return Storage.clearItem(key)
  64. class rules_service(object):
  65. @staticmethod
  66. def query_all():
  67. # 查询所有
  68. res = RuleClass.query.order_by(RuleClass.order.asc(),RuleClass.write_date.desc()).all()
  69. # print(res)
  70. # res = RuleClass.query.order_by(RuleClass.write_date.asc()).all()
  71. return copy_utils.obj_to_list(res)
  72. @classmethod
  73. def hasItem(self, key):
  74. return RuleClass.hasItem(key)
  75. def getState(self,key):
  76. res = RuleClass.query.filter(RuleClass.name == key).first()
  77. if not res:
  78. return 1
  79. # print(res)
  80. state = res.state
  81. if state is None:
  82. state = 1
  83. return state or 0
  84. def setState(self,key,state=0):
  85. res = RuleClass.query.filter(RuleClass.name == key).first()
  86. if res:
  87. res.state = state
  88. db.session.add(res)
  89. else:
  90. res = RuleClass(name=key, state=state)
  91. db.session.add(res)
  92. db.session.flush() # 获取id
  93. try:
  94. db.session.commit()
  95. return res.id
  96. except Exception as e:
  97. print(f'发生了错误:{e}')
  98. return None
  99. def setOrder(self,key,order=0):
  100. res = RuleClass.query.filter(RuleClass.name == key).first()
  101. if res:
  102. res.order = order
  103. # print(f'{res.name}设置order为:{order}')
  104. if res.order == order:
  105. res.write_date = datetime.now()
  106. # res.write_date = res.write_date + timedelta(hours=2)
  107. db.session.add(res)
  108. else:
  109. res = RuleClass(name=key, order=order)
  110. db.session.add(res)
  111. db.session.flush() # 获取id
  112. try:
  113. db.session.commit()
  114. return res.id
  115. except Exception as e:
  116. print(f'发生了错误:{e}')
  117. return None
  118. @staticmethod
  119. def getHideRules():
  120. res = RuleClass.query.filter(RuleClass.state == 0).all()
  121. return copy_utils.obj_to_list(res)
  122. class parse_service(object):
  123. @staticmethod
  124. def query_all():
  125. # 查询所有
  126. res = VipParse.query.order_by(VipParse.order.asc(),VipParse.write_date.desc()).all()
  127. # print(res)
  128. # res = RuleClass.query.order_by(RuleClass.write_date.asc()).all()
  129. return copy_utils.obj_to_list(res)
  130. @classmethod
  131. def hasItem(self, key):
  132. return VipParse.hasItem(key)
  133. def getState(self,key):
  134. res = VipParse.query.filter(VipParse.url == key).first()
  135. if not res:
  136. return 1
  137. # print(res)
  138. state = res.state
  139. if state is None:
  140. state = 1
  141. return state or 0
  142. def setState(self,key,state=0):
  143. res = VipParse.query.filter(VipParse.url == key).first()
  144. if res:
  145. res.state = state
  146. db.session.add(res)
  147. else:
  148. res = VipParse(url=key, state=state)
  149. db.session.add(res)
  150. db.session.flush() # 获取id
  151. try:
  152. db.session.commit()
  153. return res.id
  154. except Exception as e:
  155. print(f'发生了错误:{e}')
  156. return None
  157. def setOrder(self,key,order=0):
  158. res = VipParse.query.filter(VipParse.url == key).first()
  159. if res:
  160. res.order = order
  161. # print(f'{res.name}设置order为:{order}')
  162. if res.order == order:
  163. res.write_date = datetime.now()
  164. # res.write_date = res.write_date + timedelta(hours=2)
  165. db.session.add(res)
  166. else:
  167. res = VipParse(url=key, order=order)
  168. db.session.add(res)
  169. db.session.flush() # 获取id
  170. try:
  171. db.session.commit()
  172. return res.id
  173. except Exception as e:
  174. print(f'发生了错误:{e}')
  175. return None
  176. def setEverything(self,key,name,state,typeno,order,ext,header):
  177. res = VipParse.query.filter(VipParse.url == key).first()
  178. if res:
  179. res.name = name
  180. res.state = state
  181. res.type = typeno
  182. res.order = order
  183. res.ext = ext
  184. res.header = header
  185. res.write_date = datetime.now()
  186. db.session.add(res)
  187. else:
  188. res = VipParse(name=name,url=key,state=state,type=typeno,order=order,ext=ext,header=header)
  189. db.session.add(res)
  190. db.session.flush() # 获取id
  191. try:
  192. db.session.commit()
  193. return res.id
  194. except Exception as e:
  195. print(f'发生了错误:{e}')
  196. return None
  197. def saveData(self,obj):
  198. """
  199. db.session.add_all([]) 可以一次性保存多条数据,但是这里用不到,因为涉及修改和新增一起的
  200. :param obj:
  201. :return:
  202. """
  203. # res = VipParse.query.filter(VipParse.url == obj['url']).first()
  204. res = VipParse.query.filter_by(url=obj['url']).first()
  205. if res:
  206. # res.update(obj)
  207. res.name = obj['name']
  208. res.state = obj['state']
  209. res.type = obj['type']
  210. res.order = obj['order']
  211. res.ext = obj['ext']
  212. res.header = obj['header']
  213. db.session.add(res)
  214. else:
  215. res = VipParse(**obj)
  216. db.session.add(res)
  217. db.session.flush() # 获取id
  218. try:
  219. db.session.commit()
  220. return res.id
  221. except Exception as e:
  222. print(f'发生了错误:{e}')
  223. return None
  224. @staticmethod
  225. def getHideRules():
  226. res = VipParse.query.filter(VipParse.state == 0).all()
  227. return copy_utils.obj_to_list(res)