summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/openai/har_file.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-03-26 21:50:35 +0100
committerGitHub <noreply@github.com>2024-03-26 21:50:35 +0100
commitdd08125bb4740ab5dc1116868ff237ce51ae3d5e (patch)
treeed6339c66505409e7dced6d3499814b00446ff12 /g4f/Provider/openai/har_file.py
parentMerge pull request #1754 from hlohaus/goo (diff)
parentFix load .har files, add hardir to docker, add docs (diff)
downloadgpt4free-dd08125bb4740ab5dc1116868ff237ce51ae3d5e.tar
gpt4free-dd08125bb4740ab5dc1116868ff237ce51ae3d5e.tar.gz
gpt4free-dd08125bb4740ab5dc1116868ff237ce51ae3d5e.tar.bz2
gpt4free-dd08125bb4740ab5dc1116868ff237ce51ae3d5e.tar.lz
gpt4free-dd08125bb4740ab5dc1116868ff237ce51ae3d5e.tar.xz
gpt4free-dd08125bb4740ab5dc1116868ff237ce51ae3d5e.tar.zst
gpt4free-dd08125bb4740ab5dc1116868ff237ce51ae3d5e.zip
Diffstat (limited to 'g4f/Provider/openai/har_file.py')
-rw-r--r--g4f/Provider/openai/har_file.py36
1 files changed, 21 insertions, 15 deletions
diff --git a/g4f/Provider/openai/har_file.py b/g4f/Provider/openai/har_file.py
index 3e8535ad..68fe7420 100644
--- a/g4f/Provider/openai/har_file.py
+++ b/g4f/Provider/openai/har_file.py
@@ -11,11 +11,6 @@ from copy import deepcopy
from .crypt import decrypt, encrypt
from ...requests import StreamSession
-arkPreURL = "https://tcr9i.chat.openai.com/fc/gt2/public_key/35536E1E-65B4-4D96-9D97-6ADB7EFF8147"
-sessionUrl = "https://chat.openai.com/api/auth/session"
-chatArk = None
-accessToken = None
-
class arkReq:
def __init__(self, arkURL, arkBx, arkHeader, arkBody, arkCookies, userAgent):
self.arkURL = arkURL
@@ -25,21 +20,30 @@ class arkReq:
self.arkCookies = arkCookies
self.userAgent = userAgent
+arkPreURL = "https://tcr9i.chat.openai.com/fc/gt2/public_key/35536E1E-65B4-4D96-9D97-6ADB7EFF8147"
+sessionUrl = "https://chat.openai.com/api/auth/session"
+chatArk: arkReq = None
+accessToken: str = None
+cookies: dict = None
+
def readHAR():
dirPath = "./"
harPath = []
chatArks = []
accessToken = None
+ cookies = {}
for root, dirs, files in os.walk(dirPath):
for file in files:
if file.endswith(".har"):
harPath.append(os.path.join(root, file))
+ if harPath:
+ break
if not harPath:
raise RuntimeError("No .har file found")
for path in harPath:
- with open(path, 'r') as file:
+ with open(path, 'rb') as file:
try:
- harFile = json.load(file)
+ harFile = json.loads(file.read())
except json.JSONDecodeError:
# Error: not a HAR file!
continue
@@ -48,11 +52,12 @@ def readHAR():
chatArks.append(parseHAREntry(v))
elif v['request']['url'] == sessionUrl:
accessToken = json.loads(v["response"]["content"]["text"]).get("accessToken")
- if not chatArks:
- RuntimeError("No arkose requests found in .har files")
+ cookies = {c['name']: c['value'] for c in v['request']['cookies']}
if not accessToken:
RuntimeError("No accessToken found in .har files")
- return chatArks.pop(), accessToken
+ if not chatArks:
+ return None, accessToken, cookies
+ return chatArks.pop(), accessToken, cookies
def parseHAREntry(entry) -> arkReq:
tmpArk = arkReq(
@@ -60,7 +65,7 @@ def parseHAREntry(entry) -> arkReq:
arkBx="",
arkHeader={h['name'].lower(): h['value'] for h in entry['request']['headers'] if h['name'].lower() not in ['content-length', 'cookie'] and not h['name'].startswith(':')},
arkBody={p['name']: unquote(p['value']) for p in entry['request']['postData']['params'] if p['name'] not in ['rnd']},
- arkCookies=[{'name': c['name'], 'value': c['value'], 'expires': c['expires']} for c in entry['request']['cookies']],
+ arkCookies={c['name']: c['value'] for c in entry['request']['cookies']},
userAgent=""
)
tmpArk.userAgent = tmpArk.arkHeader.get('user-agent', '')
@@ -81,7 +86,6 @@ def genArkReq(chatArk: arkReq) -> arkReq:
tmpArk.arkBody['bda'] = base64.b64encode(bda.encode()).decode()
tmpArk.arkBody['rnd'] = str(random.random())
tmpArk.arkHeader['x-ark-esync-value'] = bw
- tmpArk.arkCookies = {cookie['name']: cookie['value'] for cookie in tmpArk.arkCookies}
return tmpArk
async def sendRequest(tmpArk: arkReq, proxy: str = None):
@@ -117,8 +121,10 @@ def getN() -> str:
return base64.b64encode(timestamp.encode()).decode()
async def getArkoseAndAccessToken(proxy: str):
- global chatArk, accessToken
+ global chatArk, accessToken, cookies
if chatArk is None or accessToken is None:
- chatArk, accessToken = readHAR()
+ chatArk, accessToken, cookies = readHAR()
+ if chatArk is None:
+ return None, accessToken, cookies
newReq = genArkReq(chatArk)
- return await sendRequest(newReq, proxy), accessToken, newReq.arkCookies \ No newline at end of file
+ return await sendRequest(newReq, proxy), accessToken, cookies \ No newline at end of file