Update app.py
Browse files
app.py
CHANGED
|
@@ -11,44 +11,79 @@ COURTLISTENER_API_KEY = "8287a459013c68a65edda52a2f96126138f567e9"
|
|
| 11 |
API_BASE = "https://www.courtlistener.com/api/rest/v3/"
|
| 12 |
|
| 13 |
@tool
|
| 14 |
-
def search_court_cases(keyword: str, jurisdiction: str =
|
| 15 |
-
date_range: tuple =
|
| 16 |
-
|
| 17 |
-
"""Search recent court cases by keyword and jurisdiction
|
| 18 |
Args:
|
| 19 |
keyword: Legal concepts or case terms to search
|
| 20 |
-
jurisdiction: Court jurisdiction (e.g., '
|
| 21 |
date_range: Tuple of (start_date, end_date) in UTC
|
| 22 |
"""
|
| 23 |
try:
|
|
|
|
| 24 |
params = {
|
| 25 |
"q": keyword,
|
| 26 |
-
"jurisdiction": jurisdiction,
|
| 27 |
-
"dateFiled__gte": date_range[0].isoformat(),
|
| 28 |
-
"dateFiled__lte": date_range[1].isoformat(),
|
| 29 |
"order_by": "-dateFiled",
|
| 30 |
"page_size": 5
|
| 31 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
response.raise_for_status()
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
cases = []
|
| 38 |
-
for result in
|
| 39 |
case_info = {
|
| 40 |
"case_name": result.get('caseName', 'N/A'),
|
| 41 |
"citation": result.get('citation', 'N/A'),
|
| 42 |
-
"court": result.get('
|
| 43 |
"date_filed": result.get('dateFiled', 'N/A'),
|
| 44 |
-
"summary": result.get('snippet', '')[:200] + '...'
|
| 45 |
}
|
| 46 |
cases.append(case_info)
|
| 47 |
|
| 48 |
-
return cases
|
| 49 |
|
| 50 |
except requests.exceptions.RequestException as e:
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
@tool
|
| 54 |
def check_citation_status(citation_id: str) -> Dict:
|
|
|
|
| 11 |
API_BASE = "https://www.courtlistener.com/api/rest/v3/"
|
| 12 |
|
| 13 |
@tool
|
| 14 |
+
def search_court_cases(keyword: str, jurisdiction: str = None,
|
| 15 |
+
date_range: tuple = None) -> List[Dict]:
|
| 16 |
+
"""Search court cases by keyword and jurisdiction
|
|
|
|
| 17 |
Args:
|
| 18 |
keyword: Legal concepts or case terms to search
|
| 19 |
+
jurisdiction: Court jurisdiction (e.g., 'scotus' for Supreme Court)
|
| 20 |
date_range: Tuple of (start_date, end_date) in UTC
|
| 21 |
"""
|
| 22 |
try:
|
| 23 |
+
# Build base params
|
| 24 |
params = {
|
| 25 |
"q": keyword,
|
|
|
|
|
|
|
|
|
|
| 26 |
"order_by": "-dateFiled",
|
| 27 |
"page_size": 5
|
| 28 |
}
|
| 29 |
+
|
| 30 |
+
# Add jurisdiction filter if provided
|
| 31 |
+
if jurisdiction:
|
| 32 |
+
# Use correct parameter for jurisdiction filtering
|
| 33 |
+
if jurisdiction.lower() == "scotus":
|
| 34 |
+
params["court"] = "scotus"
|
| 35 |
+
else:
|
| 36 |
+
params["court"] = jurisdiction
|
| 37 |
+
|
| 38 |
+
# Add date range if provided
|
| 39 |
+
if date_range:
|
| 40 |
+
# Format dates properly for the API
|
| 41 |
+
params["dateFiled__gte"] = date_range[0].strftime("%Y-%m-%d")
|
| 42 |
+
params["dateFiled__lte"] = date_range[1].strftime("%Y-%m-%d")
|
| 43 |
+
|
| 44 |
+
# Set proper headers with your API key
|
| 45 |
headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
|
| 46 |
|
| 47 |
+
# Use the correct endpoint - 'opinions' not 'search'
|
| 48 |
+
response = requests.get(f"{API_BASE}opinions/", params=params, headers=headers)
|
| 49 |
+
|
| 50 |
+
# Check for and handle HTTP errors
|
| 51 |
+
if response.status_code == 403:
|
| 52 |
+
return "Authentication failed: Please check your API key or account permissions"
|
| 53 |
+
|
| 54 |
response.raise_for_status()
|
| 55 |
|
| 56 |
+
# Process results
|
| 57 |
+
data = response.json()
|
| 58 |
+
|
| 59 |
+
# Handle empty results
|
| 60 |
+
if 'results' not in data or len(data['results']) == 0:
|
| 61 |
+
return "No matching cases found"
|
| 62 |
+
|
| 63 |
cases = []
|
| 64 |
+
for result in data['results']:
|
| 65 |
case_info = {
|
| 66 |
"case_name": result.get('caseName', 'N/A'),
|
| 67 |
"citation": result.get('citation', 'N/A'),
|
| 68 |
+
"court": result.get('court_id', 'N/A'),
|
| 69 |
"date_filed": result.get('dateFiled', 'N/A'),
|
| 70 |
+
"summary": result.get('snippet', '')[:200] + '...' if result.get('snippet') else "No summary available"
|
| 71 |
}
|
| 72 |
cases.append(case_info)
|
| 73 |
|
| 74 |
+
return cases
|
| 75 |
|
| 76 |
except requests.exceptions.RequestException as e:
|
| 77 |
+
# Provide more detailed error information
|
| 78 |
+
error_msg = f"API Error: {str(e)}"
|
| 79 |
+
if hasattr(e, 'response') and e.response is not None:
|
| 80 |
+
error_msg += f" (Status code: {e.response.status_code})"
|
| 81 |
+
if hasattr(e.response, 'text'):
|
| 82 |
+
error_msg += f" - {e.response.text[:200]}"
|
| 83 |
+
return error_msg
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"Unexpected error: {str(e)}"
|
| 87 |
|
| 88 |
@tool
|
| 89 |
def check_citation_status(citation_id: str) -> Dict:
|