-
Notifications
You must be signed in to change notification settings - Fork 3
/
load_session.py
66 lines (55 loc) · 1.98 KB
/
load_session.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@File : load_session.py
@Time : 2024/01/21 02:13:51
@Author : lvguanjun
@Desc : load_session.py
"""
import json
from concurrent.futures import ThreadPoolExecutor
import requests
from config import BASE_URL
from consume_user import check_is_gpt4
from redis_cache import update_and_add_to_redis, update_and_add_to_redis_gpt3
from session_refresh import session_refresh
def get_session(file):
with open(file, "r") as f:
for line in f:
line = line.strip()
if not line or "session_token" not in line:
continue
try:
yield json.loads(line)
except Exception:
print(line)
continue
def get_user_email(token):
url = BASE_URL + "/backend-api/me"
headeds = {"Authorization": f"Bearer {token}"}
response = requests.request("GET", url, headers=headeds)
if response.status_code == 200:
return response.json().get("email")
else:
print(response.status_code, response.text)
def check_session(session_token):
if session_token := session_token.get("session_token"):
response = session_refresh(session_token)
if response.status_code == 200:
token_info = response.json()
access_token = token_info.get("access_token")
user = get_user_email(access_token) + "==mock_password"
is_gpt4 = check_is_gpt4(access_token)
if is_gpt4:
print("add gpt4 to redis")
update_and_add_to_redis(user, token_info)
else:
print("add gpt3 to redis")
update_and_add_to_redis_gpt3(user, token_info)
else:
print(response.status_code, response.text)
if __name__ == "__main__":
file = "res.txt"
with ThreadPoolExecutor(max_workers=10) as executor:
for session_token in get_session(file):
executor.submit(check_session, session_token)