2
0

daily_3dos.py 7.9 KB

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