task_api.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import importlib.util
  2. import json
  3. import os
  4. import requests
  5. from dacite import from_dict
  6. from model.custom_struct import PcConfig, AccountInfo, WindowInfo, UploadLog
  7. from tools.log import logger
  8. class TaskApi:
  9. def __init__(self):
  10. self.pc_name = os.getenv('COMPUTERNAME') or os.uname().nodename
  11. # 配置内容及路径
  12. config_content = """# 接口配置\n#http://dataset.skfzs.top/api\nconfig_url='http://dcf.loadfa.com/api'\n# http://dataset.skfzs.top:8001/api\nlog_url='http://assist.qiming321.cn:8888'"""
  13. config_path = os.path.join(os.getcwd(), 'config', 'config.py')
  14. if not os.path.exists(config_path):
  15. # 文件不存在,创建并写入内容
  16. os.makedirs(os.path.dirname(config_path), exist_ok=True) # 确保目录存在
  17. with open(config_path, 'w', encoding='utf-8') as file:
  18. file.write(config_content)
  19. # 动态导入配置文件
  20. config_spec = importlib.util.spec_from_file_location("config", config_path)
  21. config_module = importlib.util.module_from_spec(config_spec)
  22. config_spec.loader.exec_module(config_module)
  23. try:
  24. self.config_url = config_module.config_url
  25. self.log_url = config_module.log_url
  26. except AttributeError as e:
  27. raise ImportError("配置文件中缺少必要的变量 config_url 或 log_url") from e
  28. self.task_url = 'http://xjf.lianyou.fun:8099'
  29. def get_device_info(self) -> tuple[bool,PcConfig|str]:
  30. url = f'{self.config_url}/deviceComputer/uploadAndFindCCVersion?pcNum={self.pc_name}'
  31. try:
  32. result = requests.get(url, timeout=10)
  33. if result.status_code == 200:
  34. json_obj = result.json()
  35. if json_obj['code'] == 0:
  36. obj = json_obj['data']['config']
  37. pc_config = PcConfig(
  38. pc_name=json_obj['data']['pcNum'],
  39. officer=json_obj['data']['directorName'],
  40. use_wuyouip=obj['使用无忧ip'],
  41. only_new=obj['只做新增'],
  42. only_retained=obj['只做留存'],
  43. timed_start=0,
  44. check_md5_on_start=obj['启动时md5检测'],
  45. start_dkw_on_start=obj['多开王自启动'],
  46. check_script_update=obj['检查脚本更新'],
  47. check_image_update=obj['检查镜像更新'],
  48. window_nums=int(obj['窗口数量'])
  49. )
  50. return True, pc_config
  51. return False, result.text
  52. except Exception as e:
  53. return False,str(e)
  54. def get_window_list(self) -> list[WindowInfo]:
  55. url = f'{self.config_url}/window/getWindowPublicV2?computerID={self.pc_name}'
  56. try:
  57. result = requests.get(url, timeout=10)
  58. if result.status_code == 200:
  59. json_obj = result.json()
  60. if json_obj['code'] == 0:
  61. window_list = []
  62. for obj in json_obj['data']['list']:
  63. window_info = WindowInfo(
  64. window_id=obj['windowID'],
  65. game_list=obj['configGame']
  66. )
  67. window_list.append(window_info)
  68. return window_list
  69. return []
  70. except Exception as e:
  71. logger.exception(f'get_window_list error: {e}')
  72. return []
  73. def heartbeat(self,operator) -> tuple[bool, str]:
  74. url = f'http://assist.qiming321.cn:8888/loging/computerHeartbeat'
  75. data = {
  76. 'pc_code': self.pc_name,
  77. 'operator': operator
  78. }
  79. try:
  80. result = requests.post(url, data= json.dumps(data), timeout=10)
  81. if result.status_code == 200 :
  82. if result.json()['code'] == 0:
  83. return True,result.json()['msg']
  84. return False,result.json()['msg']
  85. except Exception as e:
  86. logger.exception(f'heartbeat error: {e}')
  87. return False, str(e)
  88. def get_account(self, task_id: str,new,retained) -> tuple[bool, AccountInfo] | tuple[bool, str]:
  89. if new == '1':
  90. url = f'{self.task_url}/v1/task/get_account?game_id={task_id}&new=1'
  91. elif retained == '1':
  92. url = f'{self.task_url}/v1/task/get_account?game_id={task_id}&new=2'
  93. else:
  94. url = f'{self.task_url}/v1/task/get_account?game_id={task_id}'
  95. try:
  96. result = requests.get(url, timeout=10)
  97. if result.status_code == 200:
  98. if isinstance(result.json(), dict):
  99. json_obj = result.json()
  100. return True , from_dict(AccountInfo, json_obj)
  101. return False , result.text
  102. except Exception as e:
  103. return False , str(e)
  104. def up_log_profession(self, account, game_id, action, action_result) -> str:
  105. url = f'{self.task_url}/v1/device/setAccountLog'
  106. data = {
  107. 'account': account,
  108. 'game_id': game_id,
  109. 'action': action,
  110. 'action_result': action_result,
  111. }
  112. try:
  113. result = requests.get(url, params=data, timeout=10)
  114. return result.text
  115. except Exception as e:
  116. logger.exception(f'up_log_profession error: {e}')
  117. return str(e)
  118. def upload_log_to_server(self, params: UploadLog) -> (bool, str):
  119. json_data = json.dumps(params.to_dict())
  120. url = f'{self.log_url}/loging/setLog'
  121. # 发送POST请求
  122. try:
  123. response = requests.post(url, data=json_data, headers={'Content-Type': 'application/json'}, timeout=10)
  124. return response.text
  125. except Exception as e:
  126. return str(e)
  127. def notify(self,desc:str):
  128. url = f'{self.config_url}/loging/supConErr'
  129. data = {
  130. 'pc_code': self.pc_name,
  131. 'describe': desc
  132. }
  133. try:
  134. result = requests.post(url, data=data, timeout=10)
  135. return result.text
  136. except Exception as e:
  137. return str(e)
  138. task_api = TaskApi()