RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
news_explorer_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- encode: utf-8 -*-
3 
4 #Copyright 2015 RAPP
5 
6 #Licensed under the Apache License, Version 2.0 (the "License");
7 #you may not use this file except in compliance with the License.
8 #You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 #Unless required by applicable law or agreed to in writing, software
13 #distributed under the License is distributed on an "AS IS" BASIS,
14 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 #See the License for the specific language governing permissions and
16 #limitations under the License.
17 
18 # Authors: Aris Thallas
19 # contact: aris.thallas@{iti.gr, gmail.com}
20 
21 import rospy
22 
23 from rapp_news_explorer.engine_factory import EngineFactory
24 
25 from rapp_utilities import RappUtilities
26 from rapp_exceptions import RappError
27 
28 from rapp_platform_ros_communications.msg import NewsStoryMsg
29 
30 from rapp_platform_ros_communications.srv import (
31  NewsExplorerSrv,
32  NewsExplorerSrvResponse
33  )
34 
35 
36 ## @class NewsExplorerNode
37 # @brief Fetches news from various News sites' APIs
38 class NewsExplorerNode(object):
39 
40  ## @brief Constructor
41  def __init__(self):
42  ## Factory that returns proper news engine
44 
45  if rospy.has_param('rapp_news_explorer_fetch_news_topic'):
46  srv_topic = \
47  rospy.get_param("rapp_news_explorer_fetch_news_topic")
48  else:
49  srv_topic = ''
50  RappUtilities.rapp_print('Fetch News topic not found!', 'ERROR')
51 
52  fetch_service = rospy.Service(
53  srv_topic, NewsExplorerSrv, self.fetch_news_srv_callback
54  )
55 
56  ## @brief The callback to fetch news
57  #
58  # @param req
59  # [rapp_platform_ros_communications::NewsExplorer::NewsExplorerSrv]
60  # The service request
61  #
62  # @return res
63  # [rapp_platform_ros_communications::NewsExplorer::NewsExplorerSrvResponse]
64  # The service response
65  def fetch_news_srv_callback(self, req):
66  response = NewsExplorerSrvResponse()
67 
68  try:
69  engine = self._engine_factory.select_news_engine(req.newsEngine)
70  except RappError as err:
71  response.error = str(err)
72  return response
73  try:
74  results = engine.fetch_news(req)
75  except RappError as err:
76  response.error = str(err)
77  return response
78 
79  return self._create_service_response(results)
80 
81  ## @brief The callback to fetch news
82  #
83  # @param results [list<dict>] The server results containing the stories
84  #
85  # @return res
86  # [rapp_platform_ros_communications::NewsExplorer::NewsExplorerSrvResponse]
87  # The service response
88  def _create_service_response(self, results):
89  response = NewsExplorerSrvResponse()
90  for result in results:
91  msg = NewsStoryMsg()
92  msg.title = result['title']
93  msg.content = result['content']
94  msg.publisher = result['publisher']
95  msg.publishedDate = result['publishedDate']
96  msg.url = result['url']
97  response.stories.append(msg)
98  return response
99 
100 
101 if __name__ == "__main__":
102  rospy.init_node('NewsExplorer')
103  news_explorer_node = NewsExplorerNode()
104  RappUtilities.rapp_print("News Explorer node initialized")
105  rospy.spin()
Fetches news from various News sites' APIs.
_engine_factory
Factory that returns proper news engine.
Creates and returns the class of the news engine requested.