import openai import requests import json import os from bs4 import BeautifulSoup from IPython.display import Markdown from datetime import date today_date = date.today() # OpenAI API Key (Load from environment variable for security) api_key= os.getenv("OPENAI_API_KEY") openai.api_key = api_key # User-Agent Headers HEADERS = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36" } class Website: """A utility class to scrape and process a website.""" def __init__(self, url): self.url = url response = requests.get(url, headers=HEADERS) self.body = response.content soup = BeautifulSoup(self.body, 'html.parser') self.title = soup.title.string if soup.title else "No title found" if soup.body: for irrelevant in soup.body(["script", "style", "img", "input"]): irrelevant.decompose() self.text = soup.body.get_text(separator="\n", strip=True) else: self.text = "" links = [link.get('href') for link in soup.find_all('a')] self.links = [link for link in links if link] def get_contents(self): return f"Webpage Title:\n{self.title}\nWebpage Contents:\n{self.text}\n\n" # System prompt for extracting news links link_system_prompt = """ You are provided with a list of links found on a webpage. You will decide which links provide technical analysis ignore all links which don't start like this: "https://www.forexlive.com" Respond in JSON format like this: { "links": [ {"type": "news article", "url": "https://example.com/news1"}, {"type": "news article", "url": "https://example.com/news2"} ] } """ def get_links_user_prompt(website): """Generate a prompt for filtering news article links.""" user_prompt = f"Here are the links found on {website.url}. Extract only articles about technical analysis :\n" user_prompt += "\n".join(website.links) return user_prompt def get_links(url): """Fetch and filter news article links from a webpage.""" website = Website(url) response = openai.chat.completions.create( model='gpt-4o-mini', messages=[ {"role": "system", "content": link_system_prompt}, {"role": "user", "content": get_links_user_prompt(website)} ], response_format={"type": "json_object"} ) result = response.choices[0].message.content return json.loads(result) def get_all_details(url): """Retrieve content from the main page and its news article links.""" result = f"Landing page:\n{Website(url).get_contents()}\n" links = get_links(url) for link in links["links"]: result += f"\n\n{link['type']}\n" result += Website(link["url"]).get_contents() return result system_prompt = "You are a master technical analysis.you respond truthfully. do not make up prices of assets. if you don't know the price, just say so." def get_brochure_user_prompt(url): """Generate a prompt for summarizing recent news articles.""" user_prompt = f"Fetch only the news articles related to technical analysis \n" user_prompt += "Here are the page contents:\n" user_prompt += get_all_details(url) return user_prompt def create_brochure(url): """Generate a markdown-formatted news summary.""" response = openai.chat.completions.create( model='gpt-4o-mini', messages=[ {"role": "system", "content": system_prompt}, {"role": "user", "content": get_brochure_user_prompt(url)} ] ) result = response.choices[0].message.content return result # Return markdown text # Function to call externally def fetch_technical_analysis(url): """Fetch a summarized news report from a given URL.""" return create_brochure(url)