34 NewsEngineBase.__init__(self)
35 self.
_url =
'https://ajax.googleapis.com/ajax/services/search/news'
54 error =
'Requested negative number of news stories.'
55 RappUtilities.rapp_print(error,
'ERROR')
56 raise RappError(error)
57 elif req.storyNum == 0:
58 warn =
'Requested zero news stories. Providing default number of 5'
59 RappUtilities.rapp_print(warn,
'DEBUG')
63 max_stories = req.storyNum
65 warn =
'Too many stories requested. Truncating to: ' + \
67 RappUtilities.rapp_print(warn,
'DEBUG')
70 max_stories = req.storyNum
if req.storyNum < self.
_max_stories else \
77 len(final_stories) < max_stories:
85 response = self._http_request.perform_request(self.
_url,
87 except RappError
as err:
88 RappUtilities.rapp_print(err,
'ERROR')
91 if response[
'responseStatus'] != 200:
92 err =
'Http request failed. Error code: ' + str(response[
'responseStatus'])
93 RappUtilities.rapp_print(err,
'ERROR')
100 response, final_stories, req.excludeTitles)
101 except RappError
as err:
102 RappUtilities.rapp_print(err,
'ERROR')
106 final_stories = final_stories[:max_stories]
117 keys = {
'titleNoFormatting':
'title',
118 'content':
'content',
119 'publisher':
'publisher',
120 'publishedDate':
'publishedDate',
121 'unescapedUrl':
'url'}
123 new_story_list = list(story_list)
124 for result
in response[
'responseData'][
'results']:
127 story = self.rapp_http_json_parser.find_values(keys, result)
129 if story[
'title'].encode(
'utf-8')
in exclude_list:
133 new_story_list.append(story)
134 new_story_list = {v[
'title']: v
for v
in new_story_list}.values()
135 return new_story_list
146 query_str =
' '.join(req.keywords)
149 RappUtilities.rapp_print(
'Wrong query provided.' +
150 ' Falling back to default topic',
152 params[
'topic'] =
'h'
155 RappUtilities.rapp_print(
'Provided both query and topic. ' +
158 params[
'q'] = query_str
160 if req.regionEdition !=
'':
161 params[
'ned'] = req.regionEdition
163 params[
'start'] = str(iters)
def _handle_server_response
Handles the server's response.
Google news engine hndler.
Base class for news engines.
def fetch_news
Fetch the news.
def _handle_params
Create parameter dictionary for request module.