Gabandino commited on
Commit
ce49f55
·
verified ·
1 Parent(s): df3cec5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -1
app.py CHANGED
@@ -49,7 +49,10 @@ def get_latest_news(source: str) -> str:
49
  return f"Error: {source} is not a supported news source"
50
 
51
  # Fetch and parse the page
52
- response = requests.get(url, timeout=10)
 
 
 
53
  response.raise_for_status()
54
  soup = BeautifulSoup(response.text, 'html.parser')
55
 
@@ -61,6 +64,16 @@ def get_latest_news(source: str) -> str:
61
  headlines = [div.text.strip() for div in soup.select('div[class^="container__headline"]')][:5]
62
  elif source == "Reuters":
63
  headlines = [a.text.strip() for a in soup.select('a[data-testid="Heading"]')][:5]
 
 
 
 
 
 
 
 
 
 
64
  # Add other sources' parsing logic here...
65
 
66
  return f"Latest headlines from {source}:\n- " + "\n- ".join(headlines[:5])
@@ -68,6 +81,7 @@ def get_latest_news(source: str) -> str:
68
  except Exception as e:
69
  return f"Error fetching news from {source}: {str(e)}"
70
 
 
71
  @tool
72
  def get_current_time_in_timezone(timezone: str) -> str:
73
  """Fetches the current local time in a specified timezone and provides a human-friendly description.
 
49
  return f"Error: {source} is not a supported news source"
50
 
51
  # Fetch and parse the page
52
+ headers = {
53
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
54
+ }
55
+ response = requests.get(url, headers=headers, timeout=10)
56
  response.raise_for_status()
57
  soup = BeautifulSoup(response.text, 'html.parser')
58
 
 
64
  headlines = [div.text.strip() for div in soup.select('div[class^="container__headline"]')][:5]
65
  elif source == "Reuters":
66
  headlines = [a.text.strip() for a in soup.select('a[data-testid="Heading"]')][:5]
67
+ elif source == "AP":
68
+ headlines = [a.text.strip() for a in soup.select('a[class*="Component-headline"]')][:5]
69
+ elif source == "Fox News":
70
+ headlines = [h2.text.strip() for h2 in soup.select('h2[class*="title"]')][:5]
71
+ elif source == "CBS News":
72
+ headlines = [h3.text.strip() for h3 in soup.select('h3[class*="item__hed"]')][:5]
73
+ elif source == "Wall Street Journal":
74
+ headlines = [h3.text.strip() for h3 in soup.select('h3[class*="WSJTheme--headline"]')][:5]
75
+ elif source == "Daily Mail":
76
+ headlines = [h2.text.strip() for h2 in soup.select('h2[class*="linkro-darkred"]')][:5]
77
  # Add other sources' parsing logic here...
78
 
79
  return f"Latest headlines from {source}:\n- " + "\n- ".join(headlines[:5])
 
81
  except Exception as e:
82
  return f"Error fetching news from {source}: {str(e)}"
83
 
84
+
85
  @tool
86
  def get_current_time_in_timezone(timezone: str) -> str:
87
  """Fetches the current local time in a specified timezone and provides a human-friendly description.