news_agent = Agent( name="NewsAgent", role="News Analyst", goal="Collect and analyze news articles from various sources.", backstory="Expert in news gathering and content analysis.", tools=[get_article, get_news_sources, get_articles_from_source, get_trending_topics], self_reflect=False)
4
Define Task
Define the news task:
Copy
news_task = Task( description="Analyze news articles about 'AI developments' from major tech news sources.", expected_output="Summary of key AI developments with source articles.", agent=news_agent, name="ai_news")
get_articles_from_source(source_url: str, limit: int = 10, language: str = ‘en’)
Gets recent articles from a source:
Configurable limit
Language support
Full article parsing
Error handling
Copy
# Get latest articlesarticles = get_articles_from_source( "https://techcrunch.com", limit=5)# Get articles in specific languagearticles = get_articles_from_source( "https://lemonde.fr", limit=10, language="fr")# Returns: List[Dict[str, Any]]# Each article has the same structure as get_article()
# Monitor tech newssources = get_news_sources(category="technology")for source in sources: articles = get_articles_from_source( source["url"], limit=5 ) for article in articles: if "ai" in article.get("keywords", []): print(f"AI article found: {article['title']}")
Trend Analysis:
Copy
# Analyze trending topicstopics = get_trending_topics(limit=10)print("Current hot topics:")for i, topic in enumerate(topics, 1): print(f"{i}. {topic}")
Content Aggregation:
Copy
# Aggregate news from multiple sourcesdef aggregate_news(categories): all_articles = [] for category in categories: sources = get_news_sources(category=category) for source in sources[:3]: # Top 3 sources per category articles = get_articles_from_source( source["url"], limit=3 ) all_articles.extend(articles) return all_articlesnews = aggregate_news(["technology", "business", "science"])