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
event_registry_engine.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 from eventregistry import *
22 
24  NewsEngineBase,
25  RappUtilities,
26  RappError
27  )
28 
29 
30 ## @class EventRegistryEngine
31 # @brief EventRegistry news engine hndler
33 
34  ## @brief Constructor
35  def __init__(self):
36  NewsEngineBase.__init__(self)
37  self._event_handler = EventRegistry()
38 
39  key_path = os.path.join(os.environ['HOME'],
40  '.config/rapp_platform/api_keys/event_registry')
41  if os.path.isfile(key_path):
42  RappUtilities.rapp_print('Event Registry Login', 'DEBUG')
43  with open(key_path) as key_fd:
44  self._username = key_fd.readline().strip()
45  self._password = key_fd.readline().strip()
46  self._event_handler.login(self._username, self._password)
47  else:
48  RappUtilities.rapp_print('Using event registry without login', 'DEBUG')
49 
50 
51  # Some default parameter values
52  self._max_requests = 20
53  self._max_stories = 30
54 
55  ## @brief Fetch the news
56  #
57  # @param req
58  # [rapp_platform_ros_communications::NewsExplorer::NewsExplorerSrv]
59  # The service request
60  #
61  # @return [list<dict>] The server results containing the stories
62  def fetch_news(self, req):
63  if req.storyNum < 0:
64  error = 'Requested negative number of news stories.'
65  RappUtilities.rapp_print(error, 'ERROR')
66  raise RappError(error)
67  elif req.storyNum == 0:
68  warn = 'Requested zero news stories. Providing default number of 5'
69  RappUtilities.rapp_print(warn, 'DEBUG')
70  req.storyNum = 5
71 
72  if req.storyNum < self._max_stories:
73  max_stories = req.storyNum
74  else:
75  warn = 'Too many stories requested. Truncating to: ' + \
76  str(self._max_stories)
77  RappUtilities.rapp_print(warn, 'DEBUG')
78  max_stories = self._max_stories
79 
80  max_stories = req.storyNum if req.storyNum < self._max_stories else \
81  self._max_stories
82 
83  q = QueryArticles()
84 
85  for keyword in req.keywords:
86  q.addConcept(self._event_handler.getConceptUri(keyword))
87  q.addRequestedResult(RequestArticlesInfo(count=max_stories))
88 
89  stories = self._event_handler.execQuery(q)
90 
91  final_stories = []
92 
93  for story in stories['articles']['results']:
94  st = {}
95  st['title'] = story['title']
96  st['content'] = story['body']
97  st['publisher'] = story['source']['title']
98  st['publishedDate'] = story['date']
99  st['url'] = story['url']
100  final_stories.append(st)
101 
102  return final_stories