-
Notifications
You must be signed in to change notification settings - Fork 592
Add MediaStack and NewsData.io connectors for custom news sources #133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime | ||
| import os | ||
|
|
||
| import requests | ||
|
|
||
| from agents.utils.objects import Article | ||
|
|
||
|
|
||
| class MediaStackNews: | ||
| def __init__(self) -> None: | ||
| self.api_key = os.getenv("MEDIASTACK_API_KEY") | ||
| self.base_url = "http://api.mediastack.com/v1/news" | ||
| self.language = "en" | ||
| self.country = "us" | ||
|
|
||
| def get_articles_for_cli_keywords(self, keywords: str) -> "list[Article]": | ||
| query_words = keywords.split(",") | ||
| all_articles = self.get_articles_for_options(query_words) | ||
| article_objects: list[Article] = [] | ||
| for _, articles in all_articles.items(): | ||
| for article in articles: | ||
| article_objects.append(Article(**article)) | ||
| return article_objects | ||
|
|
||
| def get_articles_for_options( | ||
| self, | ||
| market_options: "list[str]", | ||
| date_start: datetime = None, | ||
| date_end: datetime = None, | ||
| ) -> dict[str, list[dict]]: | ||
| all_articles: dict[str, list[dict]] = {} | ||
| for option in market_options: | ||
| params = { | ||
| "access_key": self.api_key, | ||
| "keywords": option.strip(), | ||
| "languages": self.language, | ||
| "countries": self.country, | ||
| "sort": "published_desc", | ||
| "limit": 10, | ||
| } | ||
| if date_start and date_end: | ||
| params["date"] = f"{date_start:%Y-%m-%d},{date_end:%Y-%m-%d}" | ||
| elif date_start: | ||
| params["date"] = f"{date_start:%Y-%m-%d}" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Date end parameter silently ignored without date startThe |
||
|
|
||
| response = requests.get(self.base_url, params=params, timeout=10) | ||
| response.raise_for_status() | ||
| data = response.json().get("data", []) | ||
| all_articles[option] = [self._normalize_article(item) for item in data] | ||
|
|
||
| return all_articles | ||
|
|
||
| def _normalize_article(self, item: dict) -> dict: | ||
| source_name = item.get("source") | ||
| return { | ||
| "source": {"name": source_name} if source_name else None, | ||
| "author": item.get("author"), | ||
| "title": item.get("title"), | ||
| "description": item.get("description"), | ||
| "url": item.get("url"), | ||
| "urlToImage": item.get("image"), | ||
| "publishedAt": item.get("published_at"), | ||
| "content": None, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from datetime import datetime | ||
| import os | ||
|
|
||
| import requests | ||
|
|
||
| from agents.utils.objects import Article | ||
|
|
||
|
|
||
| class NewsDataNews: | ||
| def __init__(self) -> None: | ||
| self.api_key = os.getenv("NEWSDATA_API_KEY") | ||
| self.base_url = "https://newsdata.io/api/1/news" | ||
| self.language = "en" | ||
| self.country = "us" | ||
|
|
||
| def get_articles_for_cli_keywords(self, keywords: str) -> "list[Article]": | ||
| query_words = keywords.split(",") | ||
| all_articles = self.get_articles_for_options(query_words) | ||
| article_objects: list[Article] = [] | ||
| for _, articles in all_articles.items(): | ||
| for article in articles: | ||
| article_objects.append(Article(**article)) | ||
| return article_objects | ||
|
|
||
| def get_articles_for_options( | ||
| self, | ||
| market_options: "list[str]", | ||
| date_start: datetime = None, | ||
| date_end: datetime = None, | ||
| ) -> dict[str, list[dict]]: | ||
| all_articles: dict[str, list[dict]] = {} | ||
| for option in market_options: | ||
| params = { | ||
| "apikey": self.api_key, | ||
| "q": option.strip(), | ||
| "language": self.language, | ||
| "country": self.country, | ||
| "size": 10, | ||
| } | ||
| if date_start: | ||
| params["from_date"] = f"{date_start:%Y-%m-%d}" | ||
| if date_end: | ||
| params["to_date"] = f"{date_end:%Y-%m-%d}" | ||
|
|
||
| response = requests.get(self.base_url, params=params, timeout=10) | ||
| response.raise_for_status() | ||
| results = response.json().get("results", []) | ||
| all_articles[option] = [self._normalize_article(item) for item in results] | ||
|
|
||
| return all_articles | ||
|
|
||
| def _normalize_article(self, item: dict) -> dict: | ||
| creator = item.get("creator") | ||
| if isinstance(creator, list): | ||
| creator = creator[0] if creator else None | ||
| source_id = item.get("source_id") | ||
| return { | ||
| "source": {"name": source_id} if source_id else None, | ||
| "author": creator, | ||
| "title": item.get("title"), | ||
| "description": item.get("description"), | ||
| "url": item.get("link"), | ||
| "urlToImage": item.get("image_url"), | ||
| "publishedAt": item.get("pubDate"), | ||
| "content": item.get("content"), | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
API key transmitted over unencrypted HTTP connection
The MediaStack connector uses
http://instead ofhttps://for the API endpoint. This causes theaccess_keyAPI credential to be transmitted in plaintext over the network, where it can be intercepted by attackers. MediaStack supports HTTPS on all plans (including free), and the other connectors in the codebase (news.pyandnewsdata.py) all use HTTPS. Thebase_urlneeds to usehttps://api.mediastack.com/v1/news.