srkdev384 commited on
Commit
94e5a71
·
verified ·
1 Parent(s): d87493a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -17
app.py CHANGED
@@ -60,39 +60,60 @@ def get_current_time_in_timezone(timezone: str) -> str:
60
  @tool
61
  def get_live_cricket_score(team: str) -> str:
62
  """
63
- Fetches live cricket score for a given team.
64
 
65
  Args:
66
  team: Name of the cricket team (e.g., India, Australia).
67
 
68
  Returns:
69
- Live match details as formatted string.
70
  """
71
-
72
- url = "https://api.cricapi.com/v1/cricScore"
73
-
 
 
 
74
  params = {
75
  "apikey": CRICKET_API_KEY,
76
  "offset": 0
77
  }
78
-
79
- response = requests.get(url, params=params)
80
- data = response.json()
81
-
 
 
 
82
  if data.get("status") != "success":
83
  return "Unable to fetch live matches at the moment."
84
-
85
  matches = data.get("data", [])
86
-
87
  for match in matches:
88
- if team.lower() in match["name"].lower():
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  return f"""
90
- Match: {match['name']}
91
- Status: {match['status']}
92
- Score: {match.get('score', 'Score not available')}
 
 
93
  """
94
-
95
- return f"No live match found for {team} right now."
96
 
97
 
98
  @tool
 
60
  @tool
61
  def get_live_cricket_score(team: str) -> str:
62
  """
63
+ Fetch live cricket match details for a given team.
64
 
65
  Args:
66
  team: Name of the cricket team (e.g., India, Australia).
67
 
68
  Returns:
69
+ Formatted live match details.
70
  """
71
+
72
+ if not CRICKET_API_KEY:
73
+ return "Cricket API key not configured."
74
+
75
+ url = "https://api.cricapi.com/v1/currentMatches"
76
+
77
  params = {
78
  "apikey": CRICKET_API_KEY,
79
  "offset": 0
80
  }
81
+
82
+ try:
83
+ response = requests.get(url, params=params, timeout=10)
84
+ data = response.json()
85
+ except Exception as e:
86
+ return f"Error fetching data: {str(e)}"
87
+
88
  if data.get("status") != "success":
89
  return "Unable to fetch live matches at the moment."
90
+
91
  matches = data.get("data", [])
92
+
93
  for match in matches:
94
+ teams = match.get("teams", [])
95
+
96
+ if any(team.lower() in t.lower() for t in teams):
97
+ score_data = match.get("score", [])
98
+
99
+ score_text = ""
100
+ for inning in score_data:
101
+ runs = inning.get("r", 0)
102
+ wickets = inning.get("w", 0)
103
+ overs = inning.get("o", 0)
104
+ inning_name = inning.get("inning", "")
105
+
106
+ score_text += f"{inning_name}: {runs}/{wickets} ({overs} overs)\n"
107
+
108
  return f"""
109
+ 🏏 Match: {match.get("name")}
110
+ 📌 Status: {match.get("status")}
111
+
112
+ 📊 Score:
113
+ {score_text if score_text else "Score not available yet."}
114
  """
115
+
116
+ return f"No live match found for {team}."
117
 
118
 
119
  @tool