Debian Codegraph @

Release/samples/CasaLens/datafetcher.cpp

raw
1 /***
2 * Copyright (C) Microsoft. All rights reserved.
3 * Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
4 *
5 * =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
6 *
7 * datafetcher.cpp: Listener code: Given a location/postal code, the listener queries different services
8 * for weather, things to do: events, movie and pictures and returns it to the client.
9 *
10 * =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
11 ****/
12
13 #include "stdafx.h"
14
15 #include "casalens.h"
16
17 using namespace utility;
18 using namespace concurrency;
19 using namespace concurrency::streams;
20
21 using namespace web;
22 using namespace web::http;
23 using namespace web::http::client;
24 using namespace web::http::experimental;
25 using namespace web::http::experimental::listener;
26
27 // In case of any failure to fetch data from a service,
28 // create a json object with "error" key and value containing the exception details.
29 pplx::task<json::value> CasaLens::handle_exception(pplx::task<json::value>& t, const utility::string_t& field_name)
30 {
31 try
32 {
33 t.get();
34 }
35 catch (const std::exception& ex)
36 {
37 json::value error_json = json::value::object();
38 error_json[field_name] = json::value::object();
39 error_json[field_name][error_json_key] = json::value::string(utility::conversions::to_string_t(ex.what()));
40 return pplx::task_from_result<json::value>(error_json);
41 }
42
43 return t;
44 }
45
46 // Given postal code, query eventful service and obtain num_events upcoming events at that location.
47 // Returns a task of json::value of event data
48 // JSON result Format:
49 // {"events":["title":"event1","url":"http://event1","starttime":"ddmmyy",..]}
50 pplx::task<json::value> CasaLens::get_events(const utility::string_t& postal_code)
51 {
52 uri_builder ub(casalens_creds::events_url + postal_code);
53
54 ub.append_query(casalens_creds::events_keyname, casalens_creds::events_key);
55 auto event_url = ub.to_string();
56
57 http_client ev_client(event_url);
58 return ev_client.request(methods::GET, event_url)
59 .then([](http_response resp) { return resp.extract_json(); })
60 .then([](json::value event_json) {
61 json::value event_result_node = json::value::object();
62
63 if (!event_json[U("events")][U("event")].is_null())
64 {
65 event_result_node[U("events")] = json::value::array();
66
67 int i = 0;
68 for (auto& iter : event_json[U("events")][U("event")].as_array())
69 {
70 const auto& event = iter;
71 auto iTitle = event.as_object().find(U("title"));
72 if (iTitle == event.as_object().end())
73 {
74 throw web::json::json_exception(U("title key not found"));
75 }
76 event_result_node[events_json_key][i][U("title")] = iTitle->second;
77 auto iUrl = event.as_object().find(U("url"));
78 if (iUrl == event.as_object().end())
79 {
80 throw web::json::json_exception(U("url key not found"));
81 }
82 event_result_node[events_json_key][i][U("url")] = iUrl->second;
83 auto iStartTime = event.as_object().find(U("start_time"));
84 if (iStartTime == event.as_object().end())
85 {
86 throw web::json::json_exception(U("start_time key not found"));
87 }
88 event_result_node[events_json_key][i][U("starttime")] = iStartTime->second;
89 auto iDescription = event.as_object().find(U("description"));
90 if (iDescription == event.as_object().end())
91 {
92 throw web::json::json_exception(U("descriotion key not found"));
93 }
94 event_result_node[events_json_key][i][U("description")] = iDescription->second;
95 auto iVenueAddress = event.as_object().find(U("venue_address"));
96 if (iVenueAddress == event.as_object().end())
97 {
98 throw web::json::json_exception(U("venue_address key not found"));
99 }
100 auto iCityName = event.as_object().find(U("city_name"));
101 if (iCityName == event.as_object().end())
102 {
103 throw web::json::json_exception(U("city_name key not found"));
104 }
105 event_result_node[events_json_key][i][U("venue_address")] =
106 json::value::string(iVenueAddress->second.as_string() + U(" ") + iCityName->second.as_string());
107
108 if (i++ > num_events) break;
109 }
110 }
111 else
112 {
113 // Event data is null, we hit an error.
114 event_result_node[events_json_key] = json::value::object();
115 event_result_node[events_json_key][error_json_key] = event_json[U("events")][U("description")];
116 }
117
118 return event_result_node;
119 })
120 .then([=](pplx::task<json::value> t) { return handle_exception(t, events_json_key); });
121 }
122
123 // Query openweathermap API and obtain weather information at the given location.
124 // Returns a task of json::value containing the weather data.
125 // Format:
126 // {"weather":{"temperature":"49","pressure":"xxx"..}}
127 pplx::task<json::value> CasaLens::get_weather(const utility::string_t& postal_code)
128 {
129 utility::string_t weather_url(casalens_creds::weather_url);
130
131 uri_builder ub_weather(weather_url.append(postal_code));
132 ub_weather.append_query(U("units"), U("imperial"));
133
134 http_client weather_client(ub_weather.to_string());
135 return weather_client.request(methods::GET)
136 .then([](http_response resp) { return resp.extract_string(); })
137 .then([](utility::string_t weather_str) {
138 json::value weather_json = json::value::parse(weather_str);
139
140 auto& j = weather_json[U("list")][0][U("main")];
141
142 json::value weather_result_node = json::value::object();
143 weather_result_node[weather_json_key] = json::value::object();
144 auto& w = weather_result_node[U("weather")];
145 w[U("temperature")] = j[U("temp")];
146 w[U("pressure")] = j[U("pressure")];
147 w[U("temp_min")] = j[U("temp_min")];
148 w[U("temp_max")] = j[U("temp_max")];
149 w[U("image")] =
150 json::value::string(U("http://openweathermap.org/img/w/") +
151 weather_json[U("list")][0][U("weather")][0][U("icon")].as_string() + U(".png"));
152 w[U("description")] = weather_json[U("list")][0][U("weather")][0][U("description")];
153 return weather_result_node;
154 })
155 .then([=](pplx::task<json::value> t) { return handle_exception(t, weather_json_key); });
156 }
157
158 // Query bing images to fetch some image URLs of the given location
159 // Returns a task of json::Value
160 // JSON result format:
161 // {"images":["url1", "url2"]}
162 pplx::task<json::value> CasaLens::get_pictures(const utility::string_t& location, const utility::string_t& count)
163 {
164 http_client_config config;
165 credentials cred(casalens_creds::images_keyname, casalens_creds::images_key);
166 config.set_credentials(cred);
167 utility::string_t bing_url(casalens_creds::images_url);
168 uri_builder ub_bing(bing_url);
169 ub_bing.append_query(U("Query"), U("'") + location + U("'"));
170 ub_bing.append_query(U("$top"), count);
171 ub_bing.append_query(U("ImageFilters"), U("'Size:Medium'"));
172
173 http_client bing_client(ub_bing.to_string(), config);
174 return bing_client.request(methods::GET)
175 .then([](http_response resp) { return resp.extract_json(); })
176 .then([](json::value image_json) {
177 json::value image_result_node = json::value::object();
178 image_result_node[images_json_key] = json::value::array();
179
180 int i = 0;
181 for (auto& val : image_json[U("d")][U("results")].as_object())
182 {
183 const json::object& image = val.second.as_object();
184 auto iMediaUrl = image.find(U("MediaUrl"));
185 if (iMediaUrl == image.end())
186 {
187 throw web::json::json_exception(U("MediaUrl key not found"));
188 }
189 image_result_node[images_json_key][i] = iMediaUrl->second;
190 if (i++ > num_images) break;
191 }
192 return image_result_node;
193 })
194 .then([=](pplx::task<json::value> t) { return handle_exception(t, images_json_key); });
195 }
196
197 // Get the current date
198 std::wstring CasaLens::get_date()
199 {
200 time_t t = time(0);
201 struct tm now;
202 std::wostringstream date;
203 if (0 == localtime_s(&now, &t))
204 {
205 date << (now.tm_year + 1900) << '-' << (now.tm_mon + 1) << '-' << now.tm_mday;
206 }
207 return date.str();
208 }
209
210 // Query tmsapi and fetch current movie showtimes at local theaters, for the given postal code
211 // Also quert bing images for movie posters
212 // Returns a task of JSON value
213 // JSON result format:
214 // "movies":[{"title":"abc","theatre":[{"name":"theater1","datetime":["dd-mm-yyThh:mm"]},{"name":"theater2","datetime":["ddmmyy"]}],"poster":"img-url"}}]}..
215 pplx::task<json::value> CasaLens::get_movies(const utility::string_t& postal_code)
216 {
217 uri_builder ub_movie(casalens_creds::movies_url);
218
219 ub_movie.append_query(U("startDate"), get_date());
220 ub_movie.append_query(U("zip"), postal_code);
221 ub_movie.append_query(casalens_creds::movies_keyname, casalens_creds::movies_key);
222 ub_movie.append_query(U("imageSize"), U("Sm"));
223
224 http_client tms_client(ub_movie.to_string());
225 return tms_client.request(methods::GET)
226 .then([](http_response resp) { return resp.extract_json(); })
227 .then([](json::value movie_json) {
228 // From the data obtained from tmsapi, construct movie JSON value to be sent to the client
229 // We will collect data for 5 movies, and 5 showtime info per movie.
230 json::value movie_result_node = json::value::object();
231
232 if (movie_json.size())
233 {
234 auto temp = json::value::array();
235 int i = 0;
236 for (auto iter = movie_json.as_array().cbegin(); iter != movie_json.as_array().cend() && i < num_movies;
237 iter++, i++)
238 {
239 auto iShowTimes = iter->as_object().find(U("showtimes"));
240 if (iShowTimes == iter->as_object().end())
241 {
242 throw web::json::json_exception(U("showtimes key not found"));
243 }
244 auto& showtimes = iShowTimes->second;
245
246 int j = 0;
247 int showtime_index = 0;
248 int theater_index = -1;
249 utility::string_t current_theater;
250 auto iTitle = iter->as_object().find(U("title"));
251 if (iTitle == iter->as_object().end())
252 {
253 throw web::json::json_exception(U("title key not found"));
254 }
255 temp[i][U("title")] = iTitle->second;
256 for (auto iter2 = showtimes.as_array().cbegin();
257 iter2 != showtimes.as_array().cend() && j < num_movies;
258 iter2++, j++)
259 {
260 auto iTheatre = iter2->as_object().find(U("theatre"));
261 if (iTheatre == iter2->as_object().end())
262 {
263 throw web::json::json_exception(U("theatre key not found"));
264 }
265 auto iName = iTheatre->second.as_object().find(U("name"));
266 if (iName == iTheatre->second.as_object().end())
267 {
268 throw web::json::json_exception(U("name key not found"));
269 }
270 auto theater = iName->second.as_string();
271 if (0 != theater.compare(current_theater)) // new theater
272 {
273 temp[i][U("theatre")][++theater_index][U("name")] =
274 json::value::string(theater); // Add theater entry
275 showtime_index = 0;
276 current_theater = theater;
277 }
278 auto iDateTime = iter2->as_object().find(U("dateTime"));
279 if (iDateTime == iter2->as_object().end())
280 {
281 throw web::json::json_exception(U("dateTime key not found"));
282 }
283 temp[i][U("theatre")][theater_index][U("datetime")][showtime_index++] =
284 iDateTime->second; // Update the showtime for same theater
285 }
286 }
287 movie_result_node[movies_json_key] = std::move(temp);
288 }
289 else
290 {
291 movie_result_node[movies_json_key] = json::value::object();
292 movie_result_node[movies_json_key][error_json_key] =
293 json::value::string(U("Failed to fetch movie data"));
294 }
295
296 return pplx::task_from_result(movie_result_node);
297 })
298 .then([=](json::value movie_result) {
299 try
300 {
301 // For every movie, obtain movie poster URL from bing
302 std::vector<utility::string_t> movie_list;
303 std::vector<pplx::task<json::value>> poster_tasks;
304 auto date = get_date();
305 std::wstring year = date.substr(0, date.find(U("-")));
306
307 for (auto& iter : movie_result[movies_json_key].as_object())
308 {
309 auto title = iter.second[U("title")].as_string();
310 auto searchStr = title + U(" ") + year + U(" new movie poster");
311 poster_tasks.push_back(get_pictures(searchStr, U("1")));
312 movie_list.push_back(title);
313 }
314
315 pplx::when_all(poster_tasks.begin(), poster_tasks.end()).wait();
316 json::value resp_data = json::value::object();
317
318 for (unsigned int i = 0; i < poster_tasks.size(); i++)
319 {
320 auto jval = poster_tasks[i].get();
321 auto poster_url = jval.as_array().begin()[0];
322 movie_result[movies_json_key][i][U("poster")] = poster_url;
323 }
324 }
325 catch (...)
326 {
327 // Ignore any failure in fetching movie posters. Just return
328 // the movie data to the client.
329 }
330 return pplx::task_from_result(movie_result);
331 })
332 .then([=](pplx::task<json::value> t) { return handle_exception(t, movies_json_key); });
333 }
334
335 bool CasaLens::is_number(const std::string& s)
336 {
337 return !s.empty() &&
338 std::find_if(s.begin(), s.end(), [](char c) { return !std::isdigit(c, std::locale()); }) == s.end();
339 }
340
341 void CasaLens::fetch_data(http_request message, const std::wstring& postal_code, const std::wstring& location)
342 {
343 json::value resp_data;
344
345 try
346 {
347 m_rwlock.lock_read();
348 resp_data = m_data[postal_code];
349 m_rwlock.unlock();
350
351 if (resp_data.is_null())
352 {
353 std::vector<pplx::task<json::value>> tasks;
354
355 tasks.push_back(get_events(postal_code));
356 tasks.push_back(get_weather(postal_code));
357 tasks.push_back(get_pictures(location, U("4")));
358 tasks.push_back(get_movies(postal_code));
359
360 pplx::when_all(tasks.begin(), tasks.end()).wait();
361 resp_data = json::value::object();
362
363 for (auto& iter : tasks)
364 {
365 auto jval = iter.get();
366 auto key = jval.as_object().begin()->first;
367 resp_data[key] = jval.as_object().begin()->second;
368 }
369
370 m_rwlock.lock();
371 m_data[postal_code] = resp_data;
372 m_rwlock.unlock();
373 }
374
375 // Reply with the aggregated JSON data
376 message.reply(status_codes::OK, resp_data).then([](pplx::task<void> t) { handle_error(t); });
377 }
378 catch (...)
379 {
380 message.reply(status_codes::InternalError).then([](pplx::task<void> t) { handle_error(t); });
381 }
382 }
383
384 // Check if the input text is a number or string.
385 // If string => city name, use bing maps API to obtain the postal code for that city
386 // number => postal code, use google maps API to obtain city name (location data) for that postal code.
387 // then call fetch_data to query different services, aggregate movie, images, events, weather etc for that city and
388 // respond to the request.
389 void CasaLens::get_data(http_request message, const std::wstring& input_text)
390 {
391 if (!is_number(utility::conversions::to_utf8string(input_text)))
392 {
393 std::wstring bing_maps_url(casalens_creds::bmaps_url);
394 uri_builder maps_builder;
395 maps_builder.append_query(U("locality"), input_text);
396 maps_builder.append_query(casalens_creds::bmaps_keyname, casalens_creds::bmaps_key);
397 auto s = maps_builder.to_string();
398 http_client bing_client(bing_maps_url);
399 bing_client.request(methods::GET, s)
400 .then([=](http_response resp) { return resp.extract_json(); })
401 .then([=](json::value maps_result) mutable {
402 auto coordinates = maps_result[U("resourceSets")][0][U("resources")][0][U("point")];
403 auto lattitude = coordinates[U("coordinates")][0].serialize();
404 auto longitude = coordinates[U("coordinates")][1].serialize();
405 uri_builder ub;
406 ub.append_path(lattitude + U(",") + longitude)
407 .append_query(casalens_creds::bmaps_keyname, casalens_creds::bmaps_key);
408 auto s2 = ub.to_string();
409 return bing_client.request(methods::GET, s2);
410 })
411 .then([](http_response resp) { return resp.extract_json(); })
412 .then([=](json::value maps_result) {
413 auto postal_code =
414 maps_result[U("resourceSets")][0][U("resources")][0][U("address")][U("postalCode")].as_string();
415 fetch_data(message, postal_code, input_text);
416 })
417 .then([=](pplx::task<void> t) {
418 try
419 {
420 t.get();
421 }
422 catch (...)
423 {
424 message.reply(status_codes::InternalError, U("Failed to fetch the postal code"));
425 }
426 });
427 }
428 else // get location from postal code
429 {
430 http_client client(casalens_creds::gmaps_url);
431
432 uri_builder ub;
433 ub.append_query(U("address"), input_text);
434 ub.append_query(U("sensor"), U("false"));
435
436 client.request(methods::GET, ub.to_string())
437 .then([](http_response resp) { return resp.extract_json(); })
438 .then([=](json::value jval) {
439 auto locationstr = jval[U("results")][0][U("address_components")][1][U("long_name")].as_string();
440 fetch_data(message, input_text, locationstr);
441 })
442 .then([=](pplx::task<void> t) {
443 try
444 {
445 t.get();
446 }
447 catch (...)
448 {
449 message.reply(status_codes::InternalError, U("Failed to fetch the location from postal code"));
450 }
451 });
452 }
453 return;
454 }