2
0

daily_3dos.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # -*- coding: utf-8 -*-
  2. from odoo import models, fields
  3. from httpx import Client
  4. from concurrent.futures import ThreadPoolExecutor, as_completed
  5. from odoo.exceptions import UserError
  6. class Daily3dos(models.Model):
  7. _name = 'daily.3dos'
  8. _description = 'Daily 3dos'
  9. name = fields.Char(string='Name', default=fields.datetime.now())
  10. state = fields.Selection([
  11. ('draft', 'Draft'),
  12. ('done', 'Done'),
  13. ], string='State', default='draft')
  14. daily_3dos_line_ids = fields.One2many('daily.3dos.line', 'daily_3dos_id', string='Daily 3dos Lines')
  15. def btn_execute(self):
  16. # 获取所有3DOS账户配置
  17. if self.daily_3dos_line_ids:
  18. all_3dos_account = []
  19. for line in self.daily_3dos_line_ids:
  20. if line.line_state != 'done':
  21. all_3dos_account.append(
  22. self.env['daily.3dos.config'].search([('account', '=', line.account)], limit=1))
  23. else:
  24. all_3dos_account = self.env['daily.3dos.config'].search([('activate', '=', True)])
  25. if not all_3dos_account:
  26. raise UserError('先设置3dos账号!')
  27. # 创建字典, 记录每个账号的领取结果
  28. save_data = {}
  29. with ThreadPoolExecutor(max_workers=len(all_3dos_account)) as executor:
  30. futures = []
  31. for account in all_3dos_account:
  32. email, password = account.account, account.password
  33. futures.append(executor.submit(self.account_handle, email, password))
  34. for future in as_completed(futures):
  35. result = future.result()
  36. if result:
  37. save_data.update(result)
  38. for email, data in save_data.items():
  39. message, state = data
  40. # 判定 line 里面是否 name 是否 等于 email, 如果是, 更新当条数据, 如果不是, 创建数据
  41. email_exists = any(email == line.account for line in self.daily_3dos_line_ids)
  42. if email_exists:
  43. line = self.env['daily.3dos.line'].search([('account', '=', email), ('daily_3dos_id', '=', self.id)])
  44. line.write({
  45. 'line_state': state,
  46. 'message': message,
  47. })
  48. else:
  49. self.env['daily.3dos.line'].create({
  50. 'daily_3dos_id': self.id,
  51. 'line_state': state,
  52. 'account': email,
  53. 'message': message,
  54. })
  55. return {
  56. 'type': 'ir.actions.client',
  57. 'tag': 'display_notification',
  58. 'params': {
  59. 'title': '提示',
  60. 'message': '操作成功!',
  61. 'sticky': False,
  62. }
  63. }
  64. def account_handle(self, email, password):
  65. save_data = {}
  66. email = email
  67. password = password
  68. client = Client(proxy="http://127.0.0.1:7890")
  69. login_url = "https://api.dashboard.3dos.io/api/auth/login"
  70. login_headers = {
  71. "Accept": "application/json, text/plain, */*",
  72. "Accept-Encoding": "gzip, deflate, br, zstd",
  73. "Accept-Language": "zh-CN,zh;q=0.9",
  74. "Content-Type": "application/json",
  75. "Origin": "https://dashboard.3dos.io",
  76. "Priority": "u=1, i",
  77. "Referer": "https://dashboard.3dos.io/",
  78. "Sec-CH-UA": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
  79. "Sec-CH-UA-Mobile": "?0",
  80. "Sec-CH-UA-Platform": '"Windows"',
  81. "Sec-Fetch-Dest": "empty",
  82. "Sec-Fetch-Mode": "cors",
  83. "Sec-Fetch-Site": "same-site",
  84. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
  85. }
  86. login_payload = {
  87. "email": email,
  88. "password": password
  89. }
  90. try:
  91. login_response = client.post(login_url, json=login_payload, headers=login_headers)
  92. except Exception as e:
  93. save_data[email] = [str(f"登录请求失败:{e}"), 'error']
  94. return save_data
  95. if login_response.status_code == 200:
  96. login_data = login_response.json()
  97. data = login_data.get("data")
  98. token = data.get("access_token") or data.get("token")
  99. token_type = data.get("token_type")
  100. if not token or not token_type:
  101. save_data[email] = [f"登录响应中未找到 Token 或 Token 类型!", 'error']
  102. return save_data
  103. authorization_header = f"{token_type} {token}"
  104. else:
  105. save_data[email] = [f"登录失败!状态码:{login_response.status_code}", 'error']
  106. return save_data
  107. options_url = "https://api.dashboard.3dos.io/api/claim-reward"
  108. options_headers = {
  109. "Accept": "*/*",
  110. "Accept-Encoding": "gzip, deflate, br, zstd",
  111. "Accept-Language": "zh-CN,zh;q=0.9",
  112. "Access-Control-Request-Headers": "authorization,cache-control,content-type,expires,pragma",
  113. "Access-Control-Request-Method": "POST",
  114. "Origin": "https://dashboard.3dos.io",
  115. "Priority": "u=1, i",
  116. "Referer": "https://dashboard.3dos.io/",
  117. "Sec-Fetch-Dest": "empty",
  118. "Sec-Fetch-Mode": "cors",
  119. "Sec-Fetch-Site": "same-site",
  120. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
  121. }
  122. try:
  123. options_response = client.options(options_url, headers=options_headers)
  124. except Exception as e:
  125. save_data[email] = [f"OPTIONS 请求失败:{e}", 'error']
  126. return save_data
  127. claim_url = "https://api.dashboard.3dos.io/api/claim-reward"
  128. claim_headers = {
  129. "Accept": "application/json, text/plain, */*",
  130. "Authorization": authorization_header,
  131. "Cache-Control": "no-cache",
  132. "Content-Type": "application/json",
  133. "Expires": "0",
  134. "Origin": "https://dashboard.3dos.io",
  135. "Pragma": "no-cache",
  136. "Priority": "u=1, i",
  137. "Referer": "https://dashboard.3dos.io/",
  138. "Sec-CH-UA": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99"',
  139. "Sec-CH-UA-Mobile": "?0",
  140. "Sec-CH-UA-Platform": '"Windows"',
  141. "Sec-Fetch-Dest": "empty",
  142. "Sec-Fetch-Mode": "cors",
  143. "Sec-Fetch-Site": "same-site",
  144. "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
  145. }
  146. claim_payload = {
  147. "id": "daily-reward-api"
  148. }
  149. try:
  150. claim_response = client.post(claim_url, json=claim_payload, headers=claim_headers)
  151. except Exception as e:
  152. save_data[email] = [f"claim-reward 请求失败:{e}", 'error']
  153. return save_data
  154. tmp_message = claim_response.json()
  155. message = tmp_message.get("message")
  156. if claim_response.status_code not in [200, 429]:
  157. save_data[email] = [f"claim 报错: {message}, 状态码{claim_response.status_code}", 'error']
  158. return save_data
  159. save_data[email] = [message, 'done']
  160. print(f"{email}: {message}")
  161. return save_data
  162. class Daily3dosLine(models.Model):
  163. _name = 'daily.3dos.line'
  164. _description = 'Daily 3dos Line'
  165. daily_3dos_id = fields.Many2one('daily.3dos', string='Daily 3dos')
  166. line_state = fields.Selection([
  167. ('draft', 'Draft'),
  168. ('error', 'Error'),
  169. ('done', 'Done'),
  170. ], string='Line State', default='draft')
  171. account = fields.Char(string='Account')
  172. password = fields.Char(string='Password')
  173. message = fields.Char(string='Message')
  174. class Daily3dosConfig(models.Model):
  175. _name = 'daily.3dos.config'
  176. _description = 'Daily 3dos Config'
  177. activate = fields.Boolean(string='Activate', default=True)
  178. account = fields.Char(string='Account')
  179. password = fields.Char(string='Password')
  180. description = fields.Text(string='Description')