diff --git a/.gitignore b/.gitignore index 68bc17f..767acfe 100644 --- a/.gitignore +++ b/.gitignore @@ -157,4 +157,5 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ + diff --git a/claude-api/claude_api.py b/claude-api/claude_api.py index f7c4973..fd71b82 100644 --- a/claude-api/claude_api.py +++ b/claude-api/claude_api.py @@ -1,15 +1,21 @@ import json import os +import re import uuid -from curl_cffi import requests + import requests as req -import re +from curl_cffi import requests class Client: - def __init__(self, cookie): + def __init__(self, cookie, proxies=None): + """ + :param cookie: cookie for Claude.ai + :param proxies: proxies for requests, e.g. {"http": "http://localhost:3450", "https": "http://localhost:3450"} + """ self.cookie = cookie + self.proxies = proxies self.organization_id = self.get_organization_id() def get_organization_id(self): @@ -28,7 +34,7 @@ def get_organization_id(self): 'Cookie': f'{self.cookie}' } - response = requests.get(url, headers=headers,impersonate="chrome110") + response = requests.get(url, headers=headers,impersonate="chrome110", proxies=self.proxies) res = json.loads(response.text) uuid = res[0]['uuid'] @@ -64,7 +70,7 @@ def list_all_conversations(self): 'Cookie': f'{self.cookie}' } - response = requests.get(url, headers=headers,impersonate="chrome110") + response = requests.get(url, headers=headers,impersonate="chrome110", proxies=self.proxies) conversations = response.json() # Returns all conversation information in a list @@ -119,7 +125,7 @@ def send_message(self, prompt, conversation_id, attachment=None,timeout=500): 'TE': 'trailers' } - response = requests.post( url, headers=headers, data=payload,impersonate="chrome110",timeout=500) + response = requests.post( url, headers=headers, data=payload,impersonate="chrome110",timeout=500, proxies=self.proxies) decoded_data = response.content.decode("utf-8") decoded_data = re.sub('\n+', '\n', decoded_data).strip() data_strings = decoded_data.split('\n') @@ -156,7 +162,7 @@ def delete_conversation(self, conversation_id): 'TE': 'trailers' } - response = requests.delete( url, headers=headers, data=payload,impersonate="chrome110") + response = requests.delete( url, headers=headers, data=payload,impersonate="chrome110", proxies=self.proxies) # Returns True if deleted or False if any error in deleting if response.status_code == 204: @@ -181,8 +187,8 @@ def chat_conversation_history(self, conversation_id): 'Cookie': f'{self.cookie}' } - response = requests.get( url, headers=headers,impersonate="chrome110") - + response = requests.get( url, headers=headers,impersonate="chrome110", proxies=self.proxies) + # List all the conversations in JSON return response.json() @@ -214,7 +220,7 @@ def create_new_chat(self): 'TE': 'trailers' } - response = requests.post( url, headers=headers, data=payload,impersonate="chrome110") + response = requests.post( url, headers=headers, data=payload,impersonate="chrome110", proxies=self.proxies) # Returns JSON of the newly created conversation information return response.json() @@ -271,9 +277,9 @@ def upload_attachment(self, file_path): return response.json() else: return False - - + + # Renames the chat conversation title def rename_chat(self, title, conversation_id): url = "https://claude.ai/api/rename_chat" @@ -298,10 +304,10 @@ def rename_chat(self, title, conversation_id): 'TE': 'trailers' } - response = requests.post(url, headers=headers, data=payload,impersonate="chrome110") + response = requests.post(url, headers=headers, data=payload,impersonate="chrome110", proxies=self.proxies) if response.status_code == 200: return True else: return False - +