SURIAPRAKASH1 commited on
Commit
809e4be
·
1 Parent(s): 6b1354b

server -> bug fix

Browse files
Files changed (1) hide show
  1. server.py +25 -28
server.py CHANGED
@@ -13,19 +13,17 @@ load_dotenv()
13
  # Logging
14
  # ------------
15
  logger = logging.getLogger(__name__)
16
- # logger.setLevel("DEBUG")
17
 
18
  # formatter
19
  fmt = logging.Formatter("%(asctime)s -- %(name)s -- %(levelname)s -- %(message)s")
20
 
21
- # handlers
22
- # console_handler = logging.StreamHandler()
23
  file_handler = logging.FileHandler(filename= "multitools-server.log")
 
24
 
25
- # add to logger
26
- # logger.addHandler(console_handler)
27
- logger.addHandler(file_handler.setFormatter(fmt))
28
-
29
 
30
  # -------------------------
31
  # Initiating FastMCP server
@@ -74,7 +72,7 @@ TYPE_MAPPING = {
74
  # Available tools for LLM
75
  # -----------------------
76
 
77
- async def cricket_source(mode: str) -> str:
78
  """Fetches whole html from source url then extracts html container that contains necessary details"""
79
 
80
  if mode == "live":
@@ -100,9 +98,19 @@ async def cricket_source(mode: str) -> str:
100
  if response:
101
  # convert htmldoc content to proper html form using bs
102
  html = BeautifulSoup(response.content, "html.parser")
 
103
  # find where the content is
104
- content = html.find("div", class_= 'cb-col cb-col-100 cb-rank-tabs')
105
- return json.dumps({'output': content})
 
 
 
 
 
 
 
 
 
106
  else:
107
  return json.dumps({"error": "No Available details right now!"})
108
 
@@ -112,26 +120,15 @@ async def fetch_live_cricket_details(mode: Literal["live", "upcomming"])-> str:
112
  Args:
113
  mode : Either "live" or "upcomming"
114
  """
 
 
115
 
116
- response = await cricket_source(mode.strip().lower())
117
- data = json.loads(response)
118
-
119
- if data['error']:
120
- return response
121
- live_details = data['content'].get_text(separator = "\n", strip = True)
122
- return json.dumps({'output': str(live_details)})
123
 
124
  @mcp.tool()
125
  async def live_cricket_scorecard_herf()-> str:
126
- """Returns string of comma separated anchor tags contains herf attributes that pointing to live cricket scorecards """
127
-
128
- response = await cricket_source("live")
129
- data = json.loads(response)
130
- if data['error']:
131
- return response
132
- herfs_list = data["content"].find_all("a", class_ = "cb-text-link cb-mtch-lnks") # here don't know is it possible
133
- herfs_string = ",".join(str(tag) for tag in herfs_list)
134
- return json.dumps({'output': herfs_string})
135
 
136
 
137
  @mcp.tool()
@@ -145,8 +142,8 @@ async def live_cricket_scorecard(herf: str)-> str:
145
  scorecard_url = f"{BASE_CRICKET_URL}{herf}"
146
 
147
  try:
148
- with httpx.AsyncClient(timeout= 10.0) as client:
149
- response = client.get(url = scorecard_url)
150
  response.raise_for_status()
151
  except httpx.HTTPError as e:
152
  logger.error("\n%s", e)
 
13
  # Logging
14
  # ------------
15
  logger = logging.getLogger(__name__)
16
+ logger.setLevel("DEBUG")
17
 
18
  # formatter
19
  fmt = logging.Formatter("%(asctime)s -- %(name)s -- %(levelname)s -- %(message)s")
20
 
21
+ # handler
 
22
  file_handler = logging.FileHandler(filename= "multitools-server.log")
23
+ file_handler.setFormatter(fmt)
24
 
25
+ # add to logger
26
+ logger.addHandler(file_handler)
 
 
27
 
28
  # -------------------------
29
  # Initiating FastMCP server
 
72
  # Available tools for LLM
73
  # -----------------------
74
 
75
+ async def cricket_source(mode: str, want: str) -> str:
76
  """Fetches whole html from source url then extracts html container that contains necessary details"""
77
 
78
  if mode == "live":
 
98
  if response:
99
  # convert htmldoc content to proper html form using bs
100
  html = BeautifulSoup(response.content, "html.parser")
101
+
102
  # find where the content is
103
+ container = html.find("div", class_= 'cb-col cb-col-100 cb-rank-tabs')
104
+ if mode in ['live', 'upcomming'] and want == "text":
105
+ text = container.get_text(separator=" ", strip= True)
106
+ return json.dumps({"output": str(text)})
107
+ elif mode == 'live' and want == 'herf':
108
+ herfs_list = container.find_all("a", class_ = "cb-text-link cb-mtch-lnks")
109
+ herfs_string = ",".join(str(tag) for tag in herfs_list)
110
+ return json.dumps({"output": herfs_string})
111
+ else:
112
+ return json.dumps({"error": f"Not Implemented for {mode} with {want}"})
113
+
114
  else:
115
  return json.dumps({"error": "No Available details right now!"})
116
 
 
120
  Args:
121
  mode : Either "live" or "upcomming"
122
  """
123
+ response = await cricket_source(mode.strip().lower(), want= 'text')
124
+ return response
125
 
 
 
 
 
 
 
 
126
 
127
  @mcp.tool()
128
  async def live_cricket_scorecard_herf()-> str:
129
+ """String of comma separated anchor tags contains herf attributes that pointing to live cricket scorecards """
130
+ response = await cricket_source('live', 'herf')
131
+ return response
 
 
 
 
 
 
132
 
133
 
134
  @mcp.tool()
 
142
  scorecard_url = f"{BASE_CRICKET_URL}{herf}"
143
 
144
  try:
145
+ async with httpx.AsyncClient(timeout= 10.0) as client:
146
+ response = await client.get(url = scorecard_url)
147
  response.raise_for_status()
148
  except httpx.HTTPError as e:
149
  logger.error("\n%s", e)