-
Notifications
You must be signed in to change notification settings - Fork 3
/
consume_user.py
111 lines (95 loc) · 3.27 KB
/
consume_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File : consume_user.py
@Time : 2023/10/09 20:03:35
@Author : lvguanjun
@Desc : consume_user.py
"""
# -*- coding: utf-8 -*-
import os
import time
from queue import Queue
import requests
from config import BASE_URL, LOGIN_URL
from custom_log import logger
from redis_cache import (
get_from_redis,
is_in_error_set,
set_error_to_redis,
set_to_gpt3_redis,
set_to_redis,
gpt3_redis_cli,
)
class AccountInvalidException(Exception):
pass
def get_token(user_name, password) -> dict:
url = LOGIN_URL
payload = {
"username": user_name,
"password": password,
}
headeds = {"content-type": "application/x-www-form-urlencoded"}
response = requests.request("POST", url, data=payload, headers=headeds)
if response.status_code == 200:
return response.json()
if response.status_code == 500 and any(
[
"wrong email or password" in response.text,
"it has been deleted or deactivated" in response.text,
"wrong username or password" in response.text,
]
):
raise AccountInvalidException
raise Exception(f"{response.status_code=}, {response.text=}")
def check_is_gpt4(token):
url = BASE_URL + "/backend-api/models"
headers = {"Authorization": f"Bearer {token}"}
try:
response = requests.request("GET", url, headers=headers)
if response.status_code == 200:
categories = response.json()["categories"]
for category in categories:
if category["category"] == "gpt_4":
return True
return False
raise Exception(f"{response.status_code=}, {response.text=}")
except Exception as e:
raise Exception(f"{e=}")
def consume_user(q: Queue, sleep_time: int = 0, max_consume: int = 0):
size = 0
while True:
if max_consume and size >= max_consume:
return
user = q.get()
if user is None:
break
user_name, password = user
if is_in_error_set(f"{user_name}=={password}"):
logger.info(f"{user_name} in error set")
continue
if get_from_redis(f"{user_name}=={password}") is not None:
logger.info(f"{user_name} already in redis")
continue
if get_from_redis(f"{user_name}=={password}", gpt3_redis_cli) is not None:
logger.info(f"{user_name} already in gpt3 redis")
continue
size += 1
try:
res: dict = get_token(user_name, password)
if not check_is_gpt4(res["access_token"]):
set_to_gpt3_redis(f"{user_name}=={password}", res)
set_error_to_redis(f"{user_name}=={password}")
logger.info(f"{user_name} not gpt4")
time.sleep(sleep_time)
continue
set_to_redis(f"{user_name}=={password}", res)
logger.info(f"{user_name} add to redis")
except AccountInvalidException:
set_error_to_redis(f"{user_name}=={password}")
logger.info(f"account invalid: {user_name}")
except Exception as e:
logger.error(f"{user_name=}, {password=}, {e=}")
time.sleep(sleep_time)
if __name__ == "__main__":
pass