service.py 9.0 KB

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