Skip to content

Commit

Permalink
[newsapi_ros] Add newsapi package
Browse files Browse the repository at this point in the history
  • Loading branch information
mqcmd196 committed Dec 19, 2022
1 parent 65453a5 commit 1539725
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 0 deletions.
47 changes: 47 additions & 0 deletions newsapi_ros/CMakeLists.txt
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}
)
11 changes: 11 additions & 0 deletions newsapi_ros/action/GetTopHeadlines.action
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. Choose from business entertainment general health science sports technology
---
# Define the result
newsapi_ros/News[] news_list
bool done
---
# Define the feedback message
string status
15 changes: 15 additions & 0 deletions newsapi_ros/launch/newsapi_ros.launch
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>
8 changes: 8 additions & 0 deletions newsapi_ros/msg/News.msg
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
74 changes: 74 additions & 0 deletions newsapi_ros/node_scripts/newsapi_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/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):

CATEGORIES = ["business", "entertainment", "general", "health", "science", "sports", "technology"]

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
if sources:
rospy.logwarn("Sources param was set. Category and country are ignored")
# sources param cannot be mixed with the ``country`` or ``category`` params
if not goal.category in self.CATEGORIES:
raise KeyError("Invalid categories. Choose from {}".format(",".join(self.CATEGORIES)))
category = goal.category if (goal.category and not sources) else None
country = self.country if (not sources) else None
try:
res = self._newsapi.get_top_headlines(q=keyword,
sources=sources,
category=category,
language=self.language,
country=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()
26 changes: 26 additions & 0 deletions newsapi_ros/package.xml
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="obinata@jsk.imi.i.u-tokyo.ac.jp">Yoshiki Obinata</maintainer>
<maintainer email="k-okada@jsk.t.u-tokyo.ac.jp">Kei Okada</maintainer>

<author email="obinata@jsk.imi.u-tokyo.ac.jp">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>
1 change: 1 addition & 0 deletions newsapi_ros/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
newsapi-python

0 comments on commit 1539725

Please sign in to comment.