File size: 4,009 Bytes
15b55f0
 
 
 
 
 
 
 
 
 
 
2a0c42f
15b55f0
2a0c42f
15b55f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a0c42f
15b55f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from langchain.tools import tool
import requests
from bs4 import BeautifulSoup
from typing import Optional
import re
import sys
from constants import TAVILY_KEY
import os
from tavily import TavilyClient

# Initialize Tavily client (set your API key as an environment variable)
tavily = TavilyClient(TAVILY_KEY)

@tool
def get_npb_player_info(player_name: str) -> str:
    """
    Finds a Nippon Professional Baseball (NPB) player's jersey number and team name using Tavily search.

    Parameters:
    - player_name (str): Full name of the player (e.g., "Taisho Tamai")

    Returns:
    - str: The player's jersey number and team name, or an error message if not found.
    """
    try:
        # Search using Tavily for the NPB English profile page
        query = f"site:npb.jp/bis/eng/players {player_name}"
        search_results = tavily.search(query=query, include_answer=False)

        # Find first relevant link to the NPB player profile
        player_url = next(
            (result["url"] for result in search_results["results"]
            if re.match(r"https://npb\.jp/bis/eng/players/\d+\.html", result["url"])),
            None
        )

        if not player_url:
            return f"No player page found for '{player_name}'."

        # Fetch the player page and extract number and team
        headers = {"User-Agent": "Mozilla/5.0"}
        response = requests.get(player_url, headers=headers)
        soup = BeautifulSoup(response.text, "html.parser")

        number = soup.find("li", {"id": "pc_v_no"})
        team = soup.find("li", {"id": "pc_v_team"})

        if number and team:
            return f"{player_name} - #{number.text.strip()} - Team: {team.text.strip()}"
        else:
            return f"Player page found, but info not parsed properly: {player_url}"

    except Exception as e:
        return f"Error fetching info for '{player_name}': {e}"

#if __name__ == "__main__":
#    print(get_npb_player_info("Taisho Tamai"))

@tool
def get_team_players_by_season(team_name: str, year: int) -> list[str]:
    """
    Given a team name and year, returns the list of player names for that NPB season.

    Parameters:
    - team_name (str): Team name (e.g., "Yomiuri Giants")
    - year (int): Season year (e.g., 2023)

    Returns:
    - List of player names (strings) in the format last name, first name
    """
    try:
        query = f"site:npb.jp/bis/eng/{year}/stats {team_name}"
        search_results = tavily.search(query=query, include_answer=False)

        # Find the correct stats page URL for that year
        stats_url = next(
            (result["url"] for result in search_results["results"]
             if re.match(rf"https://npb\.jp/bis/eng/{year}/stats/.+\.html", result["url"])),
            None
        )

        if not stats_url:
            print(f"No stats page found for {team_name} in {year}.")
            return []
        
        print(stats_url)

        headers = {"User-Agent": "Mozilla/5.0"}
        response = requests.get(stats_url, headers=headers)
        safe_text = response.text.encode(sys.stdout.encoding, errors='replace').decode(sys.stdout.encoding)
        

        response = requests.get(stats_url, headers=headers)

        soup = BeautifulSoup(response.text, "html.parser")

        # Find all <td class="stplayer"> elements
        
        player_tds = soup.find_all("td", class_="stplayer")

        player_names = []
        for td in player_tds:
            player_name = td.get_text(strip=True)
            player_names.append(player_name)

        # Remove duplicates preserving order
        player_names = list(dict.fromkeys(player_names))

        return player_names

    except Exception as e:
        print(f"Error fetching players for {team_name} {year}: {e}")
        return []

if __name__ == "__main__":
    team = "Hokkaido Nippon-Ham Fighters"
    season_year = 2023
    players = get_team_players_by_season(team, season_year)
    print(f"Players for {team} in {season_year} season:\n", players)