Welcome to mirror list, hosted at ThFree Co, Russian Federation.

booking_api.cpp « partners_api - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ad94437cf36ef2c513a030d412aec4111cc21c7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include "partners_api/booking_api.hpp"
#include "partners_api/utils.hpp"

#include "platform/http_client.hpp"
#include "platform/platform.hpp"

#include "coding/url_encode.hpp"

#include "base/get_time.hpp"
#include "base/logging.hpp"
#include "base/thread.hpp"
#include "base/url_helpers.hpp"

#include <chrono>
#include <iostream>
#include <numeric>
#include <sstream>
#include <utility>

#include "3party/jansson/myjansson.hpp"

#include "private.h"

using namespace base;
using namespace booking;
using namespace platform;
using namespace std;
using namespace std::chrono;

namespace
{
string const kBookingApiBaseUrlV1 = "https://distribution-xml.booking.com/json/bookings";
string const kBookingApiBaseUrlV2 = "https://distribution-xml.booking.com/2.0/json";
string const kExtendedHotelInfoBaseUrl = "https://hotels.maps.me/getDescription";
string const kPhotoOriginalUrl = "http://aff.bstatic.com/images/hotel/max500/";
string const kPhotoSmallUrl = "http://aff.bstatic.com/images/hotel/max300/";
string const kSearchBaseUrl = "https://www.booking.com/search.html";
string const kDeepLinkBaseUrl = "booking://hotel/";
string g_BookingUrlForTesting = "";

booking::AvailabilityParams::UrlFilter const kAvailabilityParamsForUniversalLink =
{
  "checkin",
  "checkout",
  "room"
};
booking::AvailabilityParams::UrlFilter const kAvailabilityParamsForDeepLink =
{
  "checkin",
  "checkout"
};

bool RunSimpleHttpRequest(bool const needAuth, string const & url, string & result)
{
  HttpClient request(url);

  if (needAuth)
    request.SetUserAndPassword(BOOKING_KEY, BOOKING_SECRET);

  return request.RunHttpRequest(result);
}

std::string FormatTime(system_clock::time_point p)
{
  return partners_api::FormatTime(p, "%Y-%m-%d");
}

string MakeUrlForTesting(string const & func, url::Params const & params, string const & divider)
{
  ASSERT(!g_BookingUrlForTesting.empty(), ());

  auto funcForTesting = func;
  if (funcForTesting == "hotelAvailability")
  {
    auto const it = find_if(params.cbegin(), params.cend(), [](url::Param const & param)
    {
      return param.m_name == "show_only_deals";
    });

    if (it != params.cend())
      funcForTesting = "deals";
  }

  return url::Make(g_BookingUrlForTesting + divider + funcForTesting, params);
}

string MakeApiUrlImpl(string const & baseUrl, string const & func, url::Params const & params,
                      string const & divider)
{
  if (!g_BookingUrlForTesting.empty())
    return MakeUrlForTesting(func, params, divider);

  return url::Make(baseUrl + divider + func, params);
}

string MakeApiUrlV1(string const & func, url::Params const & params)
{
  return MakeApiUrlImpl(kBookingApiBaseUrlV1, func, params, ".");
}

string MakeApiUrlV2(string const & func, url::Params const & params)
{
  return MakeApiUrlImpl(kBookingApiBaseUrlV2, func, params, "/");
}

void ClearHotelInfo(HotelInfo & info)
{
  info.m_hotelId.clear();
  info.m_description.clear();
  info.m_photos.clear();
  info.m_facilities.clear();
  info.m_reviews.clear();
  info.m_score = 0.0;
  info.m_scoreCount = 0;
}

vector<HotelFacility> ParseFacilities(json_t const * facilitiesArray)
{
  vector<HotelFacility> facilities;

  if (facilitiesArray == nullptr || !json_is_array(facilitiesArray))
    return facilities;

  size_t sz = json_array_size(facilitiesArray);

  for (size_t i = 0; i < sz; ++i)
  {
    auto itemArray = json_array_get(facilitiesArray, i);
    ASSERT(json_is_array(itemArray), ());
    ASSERT_EQUAL(json_array_size(itemArray), 2, ());

    HotelFacility facility;
    FromJSON(json_array_get(itemArray, 0), facility.m_type);
    FromJSON(json_array_get(itemArray, 1), facility.m_name);

    facilities.push_back(move(facility));
  }

  return facilities;
}

vector<HotelPhotoUrls> ParsePhotos(json_t const * photosArray)
{
  if (photosArray == nullptr || !json_is_array(photosArray))
    return {};

  vector<HotelPhotoUrls> photos;
  size_t sz = json_array_size(photosArray);
  string photoId;

  for (size_t i = 0; i < sz; ++i)
  {
    auto item = json_array_get(photosArray, i);

    // Sometimes booking.com returns photo ids as strings, sometimes as integers.
    photoId = FromJSONToString(item);

    // First three digits of id are used as part of path to photo on the server.
    if (photoId.size() < 3)
    {
      LOG(LWARNING, ("Incorrect photo id =", photoId));
      continue;
    }

    string url(photoId.substr(0, 3) + "/" + photoId + ".jpg");
    photos.push_back({kPhotoSmallUrl + url, kPhotoOriginalUrl + url});
  }

  return photos;
}

vector<HotelReview> ParseReviews(json_t const * reviewsArray)
{
  if (reviewsArray == nullptr || !json_is_array(reviewsArray))
    return {};

  vector<HotelReview> reviews;
  size_t sz = json_array_size(reviewsArray);
  string date;

  for (size_t i = 0; i < sz; ++i)
  {
    auto item = json_array_get(reviewsArray, i);
    HotelReview review;

    FromJSONObject(item, "date", date);
    istringstream ss(date);
    tm t = {};
    ss >> base::get_time(&t, "%Y-%m-%d %H:%M:%S");
    if (ss.fail())
    {
      LOG(LWARNING, ("Incorrect review date =", date));
      continue;
    }
    review.m_date = system_clock::from_time_t(mktime(&t));

    double score;
    FromJSONObject(item, "score", score);
    review.m_score = static_cast<float>(score);

    FromJSONObject(item, "author", review.m_author);
    FromJSONObject(item, "pros", review.m_pros);
    FromJSONObject(item, "cons", review.m_cons);

    reviews.push_back(move(review));
  }

  return reviews;
}

void FillHotelInfo(string const & src, HotelInfo & info)
{
  my::Json root(src.c_str());

  FromJSONObjectOptionalField(root.get(), "description", info.m_description);
  double score;
  FromJSONObjectOptionalField(root.get(), "score", score);
  info.m_score = static_cast<float>(score);

  int64_t scoreCount = 0;
  FromJSONObjectOptionalField(root.get(), "score_count", scoreCount);
  info.m_scoreCount = static_cast<uint32_t>(scoreCount);

  auto const facilitiesArray = json_object_get(root.get(), "facilities");
  info.m_facilities = ParseFacilities(facilitiesArray);

  auto const photosArray = json_object_get(root.get(), "photos");
  info.m_photos = ParsePhotos(photosArray);

  auto const reviewsArray = json_object_get(root.get(), "reviews");
  info.m_reviews = ParseReviews(reviewsArray);
}

void FillPriceAndCurrency(json_t * src, string const & currency, BlockInfo & result)
{
  FromJSONObject(src, "currency", result.m_currency);
  FromJSONObject(src, "price", result.m_minPrice);

  if (currency.empty() || result.m_currency == currency)
    return;

  // Try to get price in requested currency.
  auto other = json_object_get(src, "other_currency");
  if (!json_is_object(other))
    return;

  string otherCurrency;
  FromJSONObject(other, "currency", otherCurrency);
  if (otherCurrency == currency)
  {
    result.m_currency = otherCurrency;
    FromJSONObject(other, "price", result.m_minPrice);
    return;
  }
}

BlockInfo MakeBlock(json_t * src, string const & currency)
{
  BlockInfo result;

  FromJSONObject(src, "block_id", result.m_blockId);
  FromJSONObject(src, "name", result.m_name);
  FromJSONObject(src, "room_description", result.m_description);
  FromJSONObject(src, "max_occupancy", result.m_maxOccupancy);
  FromJSONObject(src, "breakfast_included", result.m_breakfastIncluded);
  FromJSONObject(src, "deposit_required", result.m_depositRequired);

  auto minPriceRoot = json_object_get(src, "min_price");
  if (!json_is_object(minPriceRoot))
    MYTHROW(my::Json::Exception, ("The min_price must contain a json object."));

  FillPriceAndCurrency(minPriceRoot, currency, result);

  auto photosArray = json_object_get(src, "photos");
  size_t sz = json_array_size(photosArray);
  string photoUrl;
  for (size_t i = 0; i < sz; ++i)
  {
    auto photoItem = json_array_get(photosArray, i);
    FromJSONObject(photoItem, "url_original", photoUrl);
    result.m_photos.emplace_back(photoUrl);
  }

  auto & deals = result.m_deals;
  bool lastMinuteDeal = false;
  FromJSONObjectOptionalField(src, "is_last_minute_deal", lastMinuteDeal);
  if (lastMinuteDeal)
  {
    deals.m_types.emplace_back(Deals::Type::LastMinute);
    FromJSONObject(src, "last_minute_deal_percentage", deals.m_discount);
  }
  bool smartDeal = false;
  FromJSONObjectOptionalField(src, "is_smart_deal", smartDeal);
  if (smartDeal)
    deals.m_types.emplace_back(Deals::Type::Smart);

  string refundableUntil;
  auto refundableUntilObject = json_object_get(src, "refundable_until");
  if (json_is_string(refundableUntilObject))
  {
    FromJSON(refundableUntilObject, refundableUntil);

    if (!refundableUntil.empty())
    {
      istringstream ss(refundableUntil);
      tm t = {};
      ss >> base::get_time(&t, "%Y-%m-%d %H:%M:%S");
      if (ss.fail())
        LOG(LWARNING, ("Incorrect refundable_until date =", refundableUntil));
      else
        result.m_refundableUntil = system_clock::from_time_t(mktime(&t));
    }
  }

  return result;
}

void FillBlocks(string const & src, string const & currency, Blocks & blocks)
{
  my::Json root(src.c_str());
  if (!json_is_object(root.get()))
    MYTHROW(my::Json::Exception, ("The answer must contain a json object."));

  auto rootArray = json_object_get(root.get(), "result");
  if (!json_is_array(rootArray))
    MYTHROW(my::Json::Exception, ("The \"result\" field must contain a json array."));

  size_t const rootSize = json_array_size(rootArray);
  ASSERT_LESS(rootSize, 2, ("Several hotels is not supported in this method"));
  if (rootSize == 0)
    return;

  auto rootItem = json_array_get(rootArray, 0);
  if (!json_is_object(rootItem))
    MYTHROW(my::Json::Exception, ("The root item must contain a json object."));

  auto blocksArray = json_object_get(rootItem, "block");
  if (!json_is_array(blocksArray))
    MYTHROW(my::Json::Exception, ("The \"block\" field must contain a json array."));

  size_t const blocksSize = json_array_size(blocksArray);
  for (size_t i = 0; i < blocksSize; ++i)
  {
    auto block = json_array_get(blocksArray, i);

    if (!json_is_object(block))
      MYTHROW(my::Json::Exception, ("The block item must contain a json object."));

    blocks.Add(MakeBlock(block, currency));
  }
}

void FillHotelIds(string const & src, vector<std::string> & ids)
{
  my::Json root(src.c_str());
  auto const resultsArray = json_object_get(root.get(), "result");

  auto const size = json_array_size(resultsArray);

  ids.resize(size);
  for (size_t i = 0; i < size; ++i)
  {
    auto const obj = json_array_get(resultsArray, i);
    uint64_t id = 0;
    FromJSONObject(obj, "hotel_id", id);
    ids[i] = std::to_string(id);
  }
}

string ApplyAvailabilityParamsUniversal(string const & url, AvailabilityParams const & params)
{
  auto p = params.Get(kAvailabilityParamsForUniversalLink);

  auto const pos = url.find('#');

  if (pos == string::npos)
    return url::Make(url, p);

  string result = url::Make(url.substr(0, pos), p);
  result.append(url.substr(pos));
  return result;
}

string ApplyAvailabilityParamsDeep(string const & url, AvailabilityParams const & params)
{
  auto p = params.Get(kAvailabilityParamsForDeepLink);

  auto const sum = std::accumulate(
      params.m_rooms.cbegin(), params.m_rooms.cend(), 0 /* sum start value */,
      [](auto const s, auto const & room) { return s + room.GetAdultsCount(); });

  p.emplace_back("numberOfGuests", std::to_string(sum));

  return url::Make(url, p);
}
}  // namespace

namespace booking
{
// static
bool RawApi::GetHotelAvailability(string const & hotelId, string const & currency, string & result)
{
  system_clock::time_point p = system_clock::from_time_t(time(nullptr));

  string url = MakeApiUrlV1("getHotelAvailability", {{"hotel_ids", hotelId},
                                                     {"currency_code", currency},
                                                     {"arrival_date", FormatTime(p)},
                                                     {"departure_date", FormatTime(p + hours(24))}});
  return RunSimpleHttpRequest(true, url, result);
}

// static
bool RawApi::GetExtendedInfo(string const & hotelId, string const & lang, string & result)
{
  ostringstream os;
  os << kExtendedHotelInfoBaseUrl << "?hotel_id=" << hotelId << "&lang=" << lang;
  return RunSimpleHttpRequest(false, os.str(), result);
}

// static
bool RawApi::HotelAvailability(AvailabilityParams const & params, string & result)
{
  string url = MakeApiUrlV2("hotelAvailability", params.Get());

  return RunSimpleHttpRequest(true, url, result);
}

// static
bool RawApi::BlockAvailability(BlockParams const & params, string & result)
{
  string url = MakeApiUrlV2("blockAvailability", params.Get());

  return RunSimpleHttpRequest(true, url, result);
}

string Api::GetBookHotelUrl(string const & baseUrl) const
{
  ASSERT(!baseUrl.empty(), ());
  return GetDescriptionUrl(baseUrl);
}

string Api::GetDeepLink(string const & hotelId) const
{
  ASSERT(!hotelId.empty(), ());

  ostringstream os;
  os << kDeepLinkBaseUrl << hotelId << "?affiliate_id=" << BOOKING_AFFILIATE_ID;

  return os.str();
}

string Api::GetDescriptionUrl(string const & baseUrl) const
{
  ASSERT(!baseUrl.empty(), ());
  return baseUrl + string("?aid=") + BOOKING_AFFILIATE_ID;
}

string Api::GetHotelReviewsUrl(string const & hotelId, string const & baseUrl) const
{
  ASSERT(!baseUrl.empty(), ());
  ASSERT(!hotelId.empty(), ());
  ostringstream os;
  os << GetDescriptionUrl(baseUrl) << "&tab=4&label=hotel-" << hotelId << "_reviews";
  return os.str();
}

string Api::GetSearchUrl(string const & city, string const & name) const
{
  if (city.empty() || name.empty())
    return "";

  ostringstream paramStream;
  paramStream << city << " " << name;

  auto const urlEncodedParams = UrlEncode(paramStream.str());

  ostringstream resultStream;
  if (!urlEncodedParams.empty())
    resultStream << kSearchBaseUrl << "?aid=" << BOOKING_AFFILIATE_ID << ";" << "ss="
                 << urlEncodedParams << ";";

  return resultStream.str();
}

string Api::ApplyAvailabilityParams(string const & url, AvailabilityParams const & params) const
{
  ASSERT(!url.empty(), ());

  if (params.IsEmpty())
    return url;

  if (strings::StartsWith(url, "booking"))
    return ApplyAvailabilityParamsDeep(url, params);

  return ApplyAvailabilityParamsUniversal(url, params);
}

void Api::GetBlockAvailability(BlockParams && params,
                               BlockAvailabilityCallback const & fn) const
{
  GetPlatform().RunTask(Platform::Thread::Network, [params = move(params), fn]()
  {
    string httpResult;
    if (!RawApi::BlockAvailability(params, httpResult))
    {
      fn(params.m_hotelId, {});
      return;
    }

    Blocks blocks;
    try
    {
      FillBlocks(httpResult, params.m_currency, blocks);
    }
    catch (my::Json::Exception const & e)
    {
      LOG(LERROR, (e.Msg()));
      blocks = {};
    }
    fn(params.m_hotelId, blocks);
  });
}

void Api::GetHotelInfo(string const & hotelId, string const & lang,
                       GetHotelInfoCallback const & fn) const
{
  GetPlatform().RunTask(Platform::Thread::Network, [hotelId, lang, fn]()
  {
    HotelInfo info;
    info.m_hotelId = hotelId;

    string result;
    if (!RawApi::GetExtendedInfo(hotelId, lang, result))
    {
      fn(info);
      return;
    }

    try
    {
      FillHotelInfo(result, info);
    }
    catch (my::Json::Exception const & e)
    {
      LOG(LINFO, ("Failed to parse json:", hotelId, result, e.what()));
      ClearHotelInfo(info);
    }

    fn(info);
  });
}

void Api::GetHotelAvailability(AvailabilityParams const & params,
                               GetHotelAvailabilityCallback const & fn) const
{
  GetPlatform().RunTask(Platform::Thread::Network, [params, fn]()
  {
    std::vector<std::string> result;
    string httpResult;
    if (!RawApi::HotelAvailability(params, httpResult))
    {
      fn(std::move(result));
      return;
    }

    try
    {
      FillHotelIds(httpResult, result);
    }
    catch (my::Json::Exception const & e)
    {
      LOG(LERROR, (e.Msg()));
      result.clear();
    }

    fn(std::move(result));
  });
}

void SetBookingUrlForTesting(string const & url)
{
  g_BookingUrlForTesting = url;
}
}  // namespace booking