from smolagents import Tool from typing import Any, Optional class SimpleTool(Tool): name = "get_holidays" description = "A tool that returns holidays for a given day and month." inputs = {"day":{"type":"integer","description":"The day of the month (1-31) in integer format"},"month":{"type":"integer","description":"The month of the year (1-12) in integer format"}} output_type = "string" def forward(self, day: int, month: int) -> str: """A tool that returns holidays for a given day and month. Args: day: The day of the month (1-31) in integer format month: The month of the year (1-12) in integer format Returns: A string listing holidays for the specified date, or an error message if something goes wrong. """ #Imports import requests from bs4 import BeautifulSoup #Mapping for request months = { 1:"yanvar", 2:"fevral", 3:"mart", 4:"aprel", 5:"may", 6:"iyun", 7:"iyul", 8:"avgust", 9:"sentyabr", 10:"oktyabr", 11:"noyabr", 12:"dekabr" } # Validate the month and day if month is None or day is None: return "Invalid month/day" # Validate month range if not (1 <= month <= 12): return "Invalid month" # Validate day range (assuming non-leap year for simplicity) if month in {4, 6, 9, 11} and day > 30: return "Invalid day for this month" elif month == 2 and day > 28: return "Invalid day for February" elif day > 31: return "Invalid day" # Map the month to its Russian name russian_month = months[month] # Construct the query URL url = f"https://kakoysegodnyaprazdnik.ru/baza/{russian_month}/{day}" # Define headers to mimic a browser headers = { "User-Agent": "Mozilla/5.0 (Windows; Windows NT 10.3; WOW64; en-US) AppleWebKit/603.45 (KHTML, like Gecko) Chrome/51.0.2756.132 Safari/601", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Language": "ru-RU,en;q=0.9", "Accept-Encoding": "gzip, deflate" } try: # Send GET request response = requests.get(url, headers=headers) response.raise_for_status() # Raises an exception for 4xx/5xx errors response.encoding = 'utf-8' # Ensure proper decoding of Russian text # Parse HTML content soup = BeautifulSoup(response.text, "html.parser") listing_wr_div = soup.find("div", class_="listing_wr") if not listing_wr_div: return "No holiday data found for this date." # Extract holiday names holidays = [] for answer_div in listing_wr_div.find_all("div", itemprop=["suggestedAnswer", "acceptedAnswer"]): span_tag = answer_div.find("span", itemprop="text") if span_tag: holiday = span_tag.get_text(strip=True) holidays.append(holiday) # Return results if not holidays: return "No holidays found for this date." return "\n".join(holidays) # Join holidays with newlines for readability except requests.RequestException as e: return f"Failed to retrieve holidays. Error: {str(e)}"