Agentic-AI-CHAT / my_functions.py
ginipick's picture
Update my_functions.py
38f3be9 verified
raw
history blame
1.56 kB
import yfinance as yf
# youtube-search-python ์ถ”๊ฐ€ ์ž„ํฌํŠธ
from youtubesearchpython import VideosSearch
product_catalog = {
"807ZPKBL9V": "SuperWidget",
"1234567890": "MegaGadget"
}
def get_product_name_by_PID(PID: str) -> str:
"""
Finds the name of a product by its Product ID
"""
return product_catalog.get(PID, "Unknown product")
def get_stock_price(ticker: str) -> float:
"""
Retrieves the latest stock price for a given ticker using yfinance.
"""
stock = yf.Ticker(ticker)
data = stock.history(period="1d")
if not data.empty:
return data['Close'].iloc[-1]
return float('nan')
def search_youtube(query: str) -> str:
"""
Searches YouTube for the given query and returns top 3 video titles & links.
"""
# youtube-search-python ์ด์šฉ
try:
videosSearch = VideosSearch(query, limit=3)
results = videosSearch.result()["result"] # ์‹ค์ œ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ
if not results:
return "No videos found for your query."
# ๊ฒฐ๊ณผ์—์„œ ํƒ€์ดํ‹€๊ณผ ๋งํฌ๋ฅผ ์ถ”์ถœ
formatted_list = []
for idx, item in enumerate(results, start=1):
title = item.get("title", "No Title")
link = item.get("link", "#")
formatted_list.append(f"{idx}) [{title}]({link})")
# ์—ฌ๋Ÿฌ ์ค„๋กœ ๋ฌถ์–ด์„œ ๋ฐ˜ํ™˜
return "\n".join(formatted_list)
except Exception as e:
# ์–ด๋–ค ์—๋Ÿฌ๋“  ๋ฐœ์ƒ ์‹œ ์•ˆ์ „ํ•˜๊ฒŒ ๋ฉ”์‹œ์ง€ ๋ฐ˜ํ™˜
return f"Error searching YouTube: {str(e)}"