Frog215 commited on
Commit
dcdd2a7
·
verified ·
1 Parent(s): cbd64b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -14
app.py CHANGED
@@ -13,17 +13,19 @@ API_BASE = "https://www.courtlistener.com/api/rest/v4/"
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
- # Use the correct API base URL with v4
24
  api_base = "https://www.courtlistener.com/api/rest/v4/"
25
 
26
- # Build query parameters
27
  params = {
28
  "q": keyword,
29
  "type": "o", # 'o' for opinions
@@ -33,29 +35,24 @@ def search_court_cases(keyword: str, jurisdiction: str = None,
33
  # Important: Do NOT use "-date_filed" as order_by parameter
34
  # The v4 API doesn't accept the same ordering parameters as v3
35
 
36
- # Add court filter for SCOTUS or other jurisdictions
37
  if jurisdiction:
38
  if jurisdiction.lower() == "scotus":
39
  params["court"] = "scotus"
40
  else:
41
  params["court"] = jurisdiction
42
 
43
- # Add date range if provided
44
  if date_range:
45
  params["date_filed__gte"] = date_range[0].strftime("%Y-%m-%d")
46
  params["date_filed__lte"] = date_range[1].strftime("%Y-%m-%d")
47
 
48
- # Set proper headers with API key
49
  headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
50
 
51
- # Print the request details for debugging
52
  print(f"Request URL: {api_base}search/")
53
  print(f"Params: {params}")
54
 
55
- # Make the API request
56
  response = requests.get(f"{api_base}search/", params=params, headers=headers)
57
 
58
- # Add detailed response info for debugging
59
  print(f"Response status: {response.status_code}")
60
  print(f"Response body preview: {response.text[:200]}...")
61
 
@@ -65,17 +62,13 @@ def search_court_cases(keyword: str, jurisdiction: str = None,
65
 
66
  response.raise_for_status()
67
 
68
- # Parse the JSON response
69
  data = response.json()
70
 
71
- # Check for empty results
72
  if 'results' not in data or len(data['results']) == 0:
73
  return "No matching cases found in the database"
74
 
75
- # Process and format the results
76
  cases = []
77
  for result in data['results']:
78
- # Extract the data with proper field names for v4 API
79
  case_info = {
80
  "case_name": result.get('caseName', result.get('case_name', 'Unknown')),
81
  "citation": result.get('citation', 'No citation available'),
@@ -93,12 +86,12 @@ def search_court_cases(keyword: str, jurisdiction: str = None,
93
  error_msg += f" (Status code: {e.response.status_code})"
94
  if hasattr(e.response, 'text'):
95
  error_msg += f" - {e.response.text[:200]}"
96
- print(error_msg) # Print for debugging
97
  return error_msg
98
 
99
  except Exception as e:
100
  error_msg = f"Unexpected error: {str(e)}"
101
- print(error_msg) # Print for debugging
102
  return error_msg
103
 
104
 
 
13
  @tool
14
  def search_court_cases(keyword: str, jurisdiction: str = None,
15
  date_range: tuple = None) -> List[Dict]:
16
+
17
  """Search court cases by keyword and jurisdiction
18
  Args:
19
  keyword: Legal concepts or case terms to search
20
  jurisdiction: Court jurisdiction (e.g., 'scotus' for Supreme Court)
21
  date_range: Tuple of (start_date, end_date) in UTC
22
  """
23
+
24
  try:
25
+ # API
26
  api_base = "https://www.courtlistener.com/api/rest/v4/"
27
 
28
+ # zgradimo query
29
  params = {
30
  "q": keyword,
31
  "type": "o", # 'o' for opinions
 
35
  # Important: Do NOT use "-date_filed" as order_by parameter
36
  # The v4 API doesn't accept the same ordering parameters as v3
37
 
 
38
  if jurisdiction:
39
  if jurisdiction.lower() == "scotus":
40
  params["court"] = "scotus"
41
  else:
42
  params["court"] = jurisdiction
43
 
44
+ # Datum in format
45
  if date_range:
46
  params["date_filed__gte"] = date_range[0].strftime("%Y-%m-%d")
47
  params["date_filed__lte"] = date_range[1].strftime("%Y-%m-%d")
48
 
 
49
  headers = {"Authorization": f"Token {COURTLISTENER_API_KEY}"}
50
 
 
51
  print(f"Request URL: {api_base}search/")
52
  print(f"Params: {params}")
53
 
 
54
  response = requests.get(f"{api_base}search/", params=params, headers=headers)
55
 
 
56
  print(f"Response status: {response.status_code}")
57
  print(f"Response body preview: {response.text[:200]}...")
58
 
 
62
 
63
  response.raise_for_status()
64
 
 
65
  data = response.json()
66
 
 
67
  if 'results' not in data or len(data['results']) == 0:
68
  return "No matching cases found in the database"
69
 
 
70
  cases = []
71
  for result in data['results']:
 
72
  case_info = {
73
  "case_name": result.get('caseName', result.get('case_name', 'Unknown')),
74
  "citation": result.get('citation', 'No citation available'),
 
86
  error_msg += f" (Status code: {e.response.status_code})"
87
  if hasattr(e.response, 'text'):
88
  error_msg += f" - {e.response.text[:200]}"
89
+ print(error_msg) # debug
90
  return error_msg
91
 
92
  except Exception as e:
93
  error_msg = f"Unexpected error: {str(e)}"
94
+ print(error_msg) # debug
95
  return error_msg
96
 
97