grassio_login_and_get_userid.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # -*- coding: utf-8 -*-
  2. import random
  3. import time
  4. import httpx
  5. class GrassClient:
  6. def __init__(self, email, password, user_agent, proxy_ip, proxy_api_port, proxy_web_port):
  7. self.email = email
  8. self.password = password
  9. self.user_agent = user_agent
  10. self.proxy_ip = proxy_ip
  11. self.proxy_api_port = proxy_api_port
  12. self.proxy_web_port = proxy_web_port
  13. self.session = httpx.Client()
  14. def login(self):
  15. url = 'https://api.getgrass.io/login'
  16. json_data = {
  17. 'password': self.password,
  18. 'username': self.email,
  19. }
  20. headers = {
  21. 'authority': 'api.getgrass.io',
  22. 'accept': 'application/json, text/plain, */*',
  23. 'accept-language': 'uk-UA,uk;q=0.9,en-US;q=0.8,en;q=0.7',
  24. 'content-type': 'application/json',
  25. 'origin': 'https://app.getgrass.io',
  26. 'referer': 'https://app.getgrass.io/',
  27. 'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
  28. 'sec-ch-ua-mobile': '?0',
  29. 'sec-ch-ua-platform': '"Windows"',
  30. 'sec-fetch-dest': 'empty',
  31. 'sec-fetch-mode': 'cors',
  32. 'sec-fetch-site': 'same-site',
  33. 'user-agent': self.user_agent,
  34. }
  35. for retry in range(5):
  36. try:
  37. # 使用 httpx 发送 POST 请求
  38. proxy_web_port = random.choice(self.proxy_web_port)
  39. proxy = f"{self.proxy_ip}:{proxy_web_port}"
  40. proxies = {
  41. "http://": f"http://{proxy}",
  42. "https://": f"http://{proxy}",
  43. }
  44. with httpx.Client(proxies=proxies, headers=headers) as client:
  45. response = client.post(url, json=json_data)
  46. if response.status_code != 200:
  47. error_message = response.text
  48. raise httpx.RequestError(f"登录失败,状态码 {response.status_code}: {error_message}")
  49. data = response.json()
  50. userId = data['result']['data']['userId']
  51. return userId
  52. except httpx.RequestError as e:
  53. print(e)
  54. time.sleep(3)
  55. def close(self):
  56. self.session.close()
  57. # 使用示例
  58. def main():
  59. email_list = [
  60. "yujiec0210@gmail.com",
  61. "yujieccyj01@hotmail.com",
  62. "yujieccyj02@hotmail.com",
  63. "yujieccyj03@hotmail.com",
  64. "yujieccyj04@hotmail.com",
  65. "yujieccyj05@hotmail.com",
  66. "yujieccyj06@hotmail.com",
  67. "yujieccyj07@hotmail.com",
  68. "yujieccyj08@hotmail.com",
  69. "yujieccyj09@hotmail.com",
  70. "yujieccyj10@hotmail.com",
  71. "yujieccyj11@hotmail.com"
  72. ]
  73. proxy_api_port = ["52001", "52002", "52003", "52004", "52005", "52006", "52007", "52008", "52009", "52010", "52011",
  74. "52012"]
  75. proxy_web_port = ["53001", "53002", "53003", "53004", "53005", "53006", "53007", "53008", "53009", "53010", "53011",
  76. "53012"]
  77. password = "aaaAAA111!!!"
  78. proxy_ip = "192.168.31.201"
  79. user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
  80. user_id_list = []
  81. for email in email_list:
  82. client = GrassClient(email, password, user_agent, proxy_ip, proxy_api_port, proxy_web_port)
  83. try:
  84. login_response = client.login()
  85. print("userId:", f'{email} - {login_response}')
  86. user_id_list.append(login_response)
  87. finally:
  88. client.close()
  89. if user_id_list:
  90. with open("account_details.txt", "w") as file:
  91. for email, user_id, api_port, web_port in zip(email_list, user_id_list, proxy_api_port, proxy_web_port):
  92. file.write(f"{email}|||{user_id}|||{proxy_ip}:{api_port}|||{proxy_ip}:{web_port}\n")
  93. # 运行主函数
  94. if __name__ == "__main__":
  95. main()