summaryrefslogtreecommitdiffstats
path: root/g4f/Provider/you/har_file.py
diff options
context:
space:
mode:
authorH Lohaus <hlohaus@users.noreply.github.com>2024-04-07 11:27:26 +0200
committerGitHub <noreply@github.com>2024-04-07 11:27:26 +0200
commitd327afc60620913f5d2b0a9985b03a7934468ad4 (patch)
tree395de9142af3e6b9c0e5e3968ee7f8234b8b25e2 /g4f/Provider/you/har_file.py
parentUpdate Gemini.py (diff)
parentUpdate provider.py (diff)
downloadgpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.gz
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.bz2
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.lz
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.xz
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.tar.zst
gpt4free-d327afc60620913f5d2b0a9985b03a7934468ad4.zip
Diffstat (limited to 'g4f/Provider/you/har_file.py')
-rw-r--r--g4f/Provider/you/har_file.py72
1 files changed, 72 insertions, 0 deletions
diff --git a/g4f/Provider/you/har_file.py b/g4f/Provider/you/har_file.py
new file mode 100644
index 00000000..281f37e2
--- /dev/null
+++ b/g4f/Provider/you/har_file.py
@@ -0,0 +1,72 @@
+from __future__ import annotations
+
+import json
+import os
+import random
+import uuid
+
+from ...requests import StreamSession, raise_for_status
+
+class NoValidHarFileError(Exception):
+ ...
+
+class arkReq:
+ def __init__(self, arkURL, arkHeaders, arkBody, arkCookies, userAgent):
+ self.arkURL = arkURL
+ self.arkHeaders = arkHeaders
+ self.arkBody = arkBody
+ self.arkCookies = arkCookies
+ self.userAgent = userAgent
+
+arkPreURL = "https://telemetry.stytch.com/submit"
+chatArks: list = None
+
+def readHAR():
+ dirPath = "./"
+ harPath = []
+ chatArks = []
+ 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 NoValidHarFileError("No .har file found")
+ for path in harPath:
+ with open(path, 'rb') as file:
+ try:
+ harFile = json.load(file)
+ except json.JSONDecodeError:
+ # Error: not a HAR file!
+ continue
+ for v in harFile['log']['entries']:
+ if arkPreURL in v['request']['url']:
+ chatArks.append(parseHAREntry(v))
+ if not chatArks:
+ raise NoValidHarFileError("No telemetry in .har files found")
+ return chatArks
+
+def parseHAREntry(entry) -> arkReq:
+ tmpArk = arkReq(
+ arkURL=entry['request']['url'],
+ arkHeaders={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=entry['request']['postData']['text'],
+ arkCookies={c['name']: c['value'] for c in entry['request']['cookies']},
+ userAgent=""
+ )
+ tmpArk.userAgent = tmpArk.arkHeaders.get('user-agent', '')
+ return tmpArk
+
+async def sendRequest(tmpArk: arkReq, proxy: str = None):
+ async with StreamSession(headers=tmpArk.arkHeaders, cookies=tmpArk.arkCookies, proxies={"all": proxy}) as session:
+ async with session.post(tmpArk.arkURL, data=tmpArk.arkBody) as response:
+ await raise_for_status(response)
+ return await response.text()
+
+async def get_dfp_telemetry_id(proxy: str = None):
+ return str(uuid.uuid4())
+ global chatArks
+ if chatArks is None:
+ chatArks = readHAR()
+ return await sendRequest(random.choice(chatArks), proxy) \ No newline at end of file