This repository was archived by the owner on May 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 824
Add MediaStack and NewsData.io connectors for custom news sources #133
Open
Coooder-Crypto
wants to merge
1
commit into
Polymarket:main
Choose a base branch
from
Coooder-Crypto:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"), | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.