File size: 3,551 Bytes
bbd506b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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)}"