-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,47 @@ | ||
cmake_minimum_required(VERSION 2.8.3) | ||
project(newsapi_ros) | ||
|
||
find_package( | ||
catkin REQUIRED COMPONENTS | ||
catkin_virtualenv REQUIRED | ||
rospy | ||
actionlib_msgs | ||
std_msgs | ||
message_generation | ||
) | ||
|
||
add_message_files( | ||
FILES | ||
News.msg | ||
) | ||
|
||
add_action_files( | ||
FILES | ||
GetTopHeadlines.action | ||
) | ||
|
||
generate_messages( | ||
DEPENDENCIES | ||
std_msgs | ||
actionlib_msgs | ||
) | ||
|
||
catkin_package() | ||
|
||
catkin_generate_virtualenv( | ||
PYTHON_INTERPRETER python3 | ||
) | ||
|
||
file(GLOB PYTHON_NODES node_scripts/*.py) | ||
catkin_install_python( | ||
PROGRAMS ${PYTHON_NODES} | ||
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} | ||
) | ||
|
||
install(FILES requirements.txt | ||
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} | ||
) | ||
|
||
install(DIRECTORY launch | ||
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} | ||
) |
This file contains 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,11 @@ | ||
# Define the goal | ||
string keyword # Keywords or a phrase to search for in the article title and body | ||
string[] sources # A string list of identifiers for the news sources or blogs you want headlines from | ||
string category # Find sources that display news of this category | ||
--- | ||
# Define the result | ||
newsapi_ros/News[] news_list | ||
bool done | ||
--- | ||
# Define the feedback message | ||
string status |
This file contains 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,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<launch> | ||
<arg name="api_key" /> | ||
<arg name="country" default="jp" /> | ||
<arg name="respawn" default="true" /> | ||
|
||
<node name="newsapi_ros" pkg="newsapi_ros" type="newsapi_node.py" | ||
respawn="$(arg respawn)" output="screen"> | ||
<rosparam subst_value="true"> | ||
api_key: $(arg api_key) | ||
country: $(arg country) | ||
</rosparam> | ||
</node> | ||
|
||
</launch> |
This file contains 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,8 @@ | ||
string source | ||
string author | ||
string title | ||
string description | ||
string url | ||
string url_to_image | ||
string published_at | ||
string content |
This file contains 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,65 @@ | ||
#!/usr/bin/env python | ||
|
||
import actionlib | ||
from newsapi import NewsApiClient | ||
from newsapi.newsapi_client import NewsAPIException | ||
from newsapi_ros.msg import GetTopHeadlinesAction, GetTopHeadlinesFeedback, GetTopHeadlinesResult, News | ||
import rospy | ||
|
||
class NewsApiNode(object): | ||
def __init__(self): | ||
self.language = rospy.get_param("~language", None) | ||
self.country = rospy.get_param("~country", "jp") | ||
_api_key = rospy.get_param("~api_key") | ||
self._newsapi = NewsApiClient(api_key=_api_key) | ||
self._top_headlines_as = actionlib.SimpleActionServer("~get_top_headlines", | ||
GetTopHeadlinesAction, | ||
execute_cb=self._top_headlines_cb, | ||
auto_start=False) | ||
# TODO add EVERYTHING. c.f. https://newsapi.org/docs/endpoints/everything | ||
self._top_headlines_as.start() | ||
|
||
|
||
def _top_headlines_cb(self, goal): | ||
feedback = GetTopHeadlinesFeedback() | ||
result = GetTopHeadlinesResult() | ||
success = False | ||
# Request | ||
keyword = goal.keyword if goal.keyword else None | ||
sources = ",".join(goal.sources) if ",".join(goal.sources) else None | ||
category = goal.category if goal.category else None | ||
try: | ||
res = self._newsapi.get_top_headlines(q=keyword, | ||
sources=sources, | ||
category=category, | ||
language=self.language, | ||
country=self.country) | ||
news_infos = [] | ||
for article in res['articles']: | ||
news = News() | ||
news.source = str(article.get("source").get("name")) | ||
news.author = str(article.get("author")) | ||
news.title = str(article.get("title")) | ||
news_infos.append(news.title) | ||
news.description = str(article.get("description")) | ||
news.url = str(article.get("url")) | ||
news.url_to_image = str(article.get("urlToImage")) | ||
news.published_at = str(article.get("published_at")) | ||
news.content = str(article.get("content")) | ||
result.news_list.append(news) | ||
success = True | ||
rospy.loginfo("Got Top Headlines news. {}".format(", ".join(news_infos))) | ||
except (NewsAPIException, SyntaxError, ValueError) as e: | ||
rospy.logerr(e) | ||
feedback.status = str(e) | ||
success = False | ||
finally: | ||
self._top_headlines_as.publish_feedback(feedback) | ||
result.done = success | ||
self._top_headlines_as.set_succeeded(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
rospy.init_node("newsapi_ros") | ||
node = NewsApiNode() | ||
rospy.spin() |
This file contains 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,26 @@ | ||
<?xml version="1.0"?> | ||
<package format="3"> | ||
<name>newsapi_ros</name> | ||
<version>0.0.0</version> | ||
<description>The ROS package for news API</description> | ||
|
||
<maintainer email="[email protected]">Yoshiki Obinata</maintainer> | ||
<maintainer email="[email protected]">Kei Okada</maintainer> | ||
|
||
<author email="[email protected]">Yoshiki Obinata</author> | ||
|
||
<license>BSD</license> | ||
|
||
<buildtool_depend>catkin</buildtool_depend> | ||
|
||
<build_depend>message_generation</build_depend> | ||
<build_depend>catkin_virtualenv</build_depend> | ||
|
||
<exec_depend>message_runtime</exec_depend> | ||
<exec_depend>rospy</exec_depend> | ||
<exec_depend>std_msgs</exec_depend> | ||
|
||
<export> | ||
<pip_requirements>requirements.txt</pip_requirements> | ||
</export> | ||
</package> |
This file contains 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 @@ | ||
newsapi-python |