Skip to content

Commit 6447d83

Browse files
authored
Merge pull request #24 from st1vms/decoding_errors
v0.3.3 Dropped Brotli encoding, multiple organizations fix
2 parents 422581d + df75a13 commit 6447d83

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- [Faster Loading](#faster-loading-avoiding-selenium)
1212
- [Proxies](#proxies)
1313
- [Changing model version](#changing-claude-model)
14+
- [Changing Organization](#changing-organization)
1415
- [Troubleshooting](#troubleshooting)
1516
- [Donating](#donating)
1617

@@ -272,6 +273,24 @@ client = ClaudeAPIClient(session, model_name="claude-2.0")
272273

273274
You can retrieve the `model_name` strings from the [official API docs](https://docs.anthropic.com/claude/docs/models-overview#model-comparison)
274275

276+
__________
277+
278+
### Changing Organization
279+
280+
As reported in issue [#23](https://github.com/st1vms/unofficial-claude-api/issues/23)
281+
if you're encountering 403 errors when using Selenium to auto retrieve a `SessionData` class and your account has multiple organizations,
282+
you may want to override the default organization retrieved.
283+
284+
By default `get_session_data` retrieves the last organization from the result array found [here](https://claude.ai/api/organizations).
285+
You can override the index to fetch by using parameter `organization_index`:
286+
287+
```py
288+
from claude_api.session import get_session_data
289+
290+
# Defaults to -1 (last entry)
291+
session = get_session_data(organization_index=-1)
292+
```
293+
275294
## TROUBLESHOOTING
276295

277296
Some common errors that may arise during the usage of this API:

claude_api/client.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from mimetypes import guess_type
1111
from zlib import decompress as zlib_decompress
1212
from zlib import MAX_WBITS
13-
from brotli import decompress as br_decompress
13+
#from brotli import decompress as br_decompress
1414
from tzlocal import get_localzone
1515
from requests import post as requests_post
1616
from curl_cffi.requests import get as http_get
@@ -468,12 +468,14 @@ def __decode_response(self, buffer: bytes, encoding_header: str) -> bytes:
468468
if encoding_header == "gzip":
469469
# Content is gzip-encoded, decode it using zlib
470470
return zlib_decompress(buffer, MAX_WBITS | 16)
471-
elif encoding_header == "deflate":
471+
if encoding_header == "deflate":
472472
# Content is deflate-encoded, decode it using zlib
473473
return zlib_decompress(buffer, -MAX_WBITS)
474-
elif encoding_header == "br":
474+
475+
# DROPPING BROTLI DECODING
476+
#if encoding_header == "br":
475477
# Content is Brotli-encoded, decode it using the brotli library
476-
return br_decompress(buffer)
478+
# return br_decompress(buffer)
477479

478480
# Content is either not encoded or with a non supported encoding.
479481
return buffer
@@ -616,7 +618,16 @@ def send_message(
616618
enc = response.headers["Content-Encoding"]
617619

618620
# Decrypt encoded response
619-
dec = self.__decode_response(response.content, enc)
621+
try:
622+
dec = self.__decode_response(response.content, enc)
623+
except Exception as e:
624+
# Return raw response for inspection
625+
print(f"Exception decoding from {enc}: {e}")
626+
return SendMessageResponse(
627+
None,
628+
response.status_code,
629+
response.content,
630+
)
620631

621632
return SendMessageResponse(
622633
self.__parse_send_message_response(dec),

claude_api/session.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,16 @@ class SessionData:
3838
"""
3939

4040

41-
def get_session_data(profile: str = "", quiet: bool = False) -> SessionData | None:
41+
def get_session_data(profile: str = "", quiet: bool = False, organization_index:int=-1) -> SessionData | None:
4242
"""
4343
Retrieves Claude session data
4444
4545
This function requires a profile with Claude login and geckodriver installed!
4646
4747
The default Firefox profile will be used, if the profile argument was not overwrited.
48+
49+
Parameter `organization_index` is by default -1
50+
(last entry from https://claude.ai/api/organizations)
4851
"""
4952

5053
json_tab_id = 'a[id="rawdata-tab"]'
@@ -81,8 +84,8 @@ def get_session_data(profile: str = "", quiet: bool = False) -> SessionData | No
8184
if pre.text:
8285
j = json_loads(pre.text)
8386
try:
84-
if j and len(j) >= 1 and "uuid" in j[0]:
85-
org_id = j[0]["uuid"]
87+
if j and len(j) > organization_index and "uuid" in j[organization_index]:
88+
org_id = j[organization_index]["uuid"]
8689
except KeyError:
8790
print(
8891
f"\nUnable to retrieve organization_id from profile: {profile}\n"

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
setup(
1818
name="unofficial-claude-api",
19-
version="0.3.2",
19+
version="0.3.3",
2020
author="st1vms",
2121
author_email="stefano.maria.salvatore@gmail.com",
2222
description=__DESCRIPTION,
@@ -35,6 +35,6 @@
3535
"selgym",
3636
"curl_cffi",
3737
"tzlocal",
38-
"brotli",
38+
#"brotli",
3939
],
4040
)

0 commit comments

Comments
 (0)