|
1 | 1 | // Copyright 2025 Omkar Prabhu
|
| 2 | +#include "worker/places.h" |
| 3 | + |
| 4 | +#include <cpr/cpr.h> |
| 5 | +#include <simdjson.h> |
| 6 | +#include <spdlog/spdlog.h> |
| 7 | + |
| 8 | +#include <memory> |
| 9 | +#include <optional> |
| 10 | +#include <string> |
| 11 | +#include <unordered_map> |
| 12 | +#include <utility> |
| 13 | + |
| 14 | +class HttpClient : public HttpClientInterface { |
| 15 | + public: |
| 16 | + cpr::Response Get(const cpr::Url& url, const cpr::Header& headers) override { |
| 17 | + return cpr::Get(url, headers); |
| 18 | + } |
| 19 | +}; |
| 20 | + |
| 21 | +std::unordered_map<std::string, std::string> Places::ReverseGeocode( |
| 22 | + const std::string& user_id, const std::string& mediaitem_id, |
| 23 | + std::optional<double> latitude, std::optional<double> longitude) { |
| 24 | + return {}; |
| 25 | +} |
| 26 | + |
| 27 | +std::unordered_map<std::string, std::string> OpenStreetMap::ReverseGeocode( |
| 28 | + const std::string& user_id, const std::string& mediaitem_id, |
| 29 | + std::optional<double> latitude, std::optional<double> longitude) { |
| 30 | + if (!latitude.has_value() || !longitude.has_value()) { |
| 31 | + return {}; |
| 32 | + } |
| 33 | + |
| 34 | + cpr::Response r = http_client_->Get( |
| 35 | + cpr::Url{ |
| 36 | + Format(url_, {{"latitude", std::to_string(latitude.value())}, |
| 37 | + {"longitude", std::to_string(longitude.value())}})}, |
| 38 | + cpr::Header{{"User-Agent", "smriti-worker"}, |
| 39 | + {"Accept-Language", "en-GB,en-US"}}); |
| 40 | + if (r.error.message != "") { |
| 41 | + spdlog::error("error in openstreetmap response: {}", r.error.message); |
| 42 | + return {}; |
| 43 | + } |
| 44 | + if (r.status_code != 200) { |
| 45 | + return {}; |
| 46 | + } |
| 47 | + |
| 48 | + spdlog::info("success in openstreetmap response: {} {}", r.status_code, |
| 49 | + r.text); |
| 50 | + |
| 51 | + simdjson::ondemand::parser parser; |
| 52 | + simdjson::padded_string padded_body(r.text); |
| 53 | + simdjson::ondemand::document doc = parser.iterate(padded_body); |
| 54 | + |
| 55 | + std::unordered_map<std::string, std::string> result; |
| 56 | + |
| 57 | + if (doc["address"].error() == simdjson::SUCCESS) { |
| 58 | + simdjson::ondemand::object address = doc["address"].get_object(); |
| 59 | + |
| 60 | + if (address["postcode"].error() == simdjson::SUCCESS) { |
| 61 | + result["postcode"] = |
| 62 | + std::string(address["postcode"].get_string().value()); |
| 63 | + } |
| 64 | + if (address["country"].error() == simdjson::SUCCESS) { |
| 65 | + result["country"] = std::string(address["country"].get_string().value()); |
| 66 | + } |
| 67 | + |
| 68 | + std::string locality = ""; |
| 69 | + if (address["city"].error() == simdjson::SUCCESS) { |
| 70 | + locality = std::string(address["city"].get_string().value()); |
| 71 | + } |
| 72 | + if (locality == "" && address["town"].error() == simdjson::SUCCESS) { |
| 73 | + locality = std::string(address["town"].get_string().value()); |
| 74 | + } |
| 75 | + if (locality == "" && address["village"].error() == simdjson::SUCCESS) { |
| 76 | + locality = std::string(address["village"].get_string().value()); |
| 77 | + } |
| 78 | + if (locality == "" && |
| 79 | + address["municipality"].error() == simdjson::SUCCESS) { |
| 80 | + locality = std::string(address["municipality"].get_string().value()); |
| 81 | + } |
| 82 | + result["locality"] = locality; |
| 83 | + |
| 84 | + std::string area; |
| 85 | + if (address["suburb"].error() == simdjson::SUCCESS) { |
| 86 | + area = std::string(address["suburb"].get_string().value()); |
| 87 | + } |
| 88 | + if (area == "" && address["quarter"].error() == simdjson::SUCCESS) { |
| 89 | + area = std::string(address["quarter"].get_string().value()); |
| 90 | + } |
| 91 | + if (area == "" && address["neighbourhood"].error() == simdjson::SUCCESS) { |
| 92 | + area = std::string(address["neighbourhood"].get_string().value()); |
| 93 | + } |
| 94 | + if (area == "" && address["road"].error() == simdjson::SUCCESS) { |
| 95 | + area = std::string(address["road"].get_string().value()); |
| 96 | + } |
| 97 | + result["area"] = area; |
| 98 | + } else { |
| 99 | + spdlog::error("error in openstreetmap response: {}", |
| 100 | + simdjson::error_message(doc["address"].error())); |
| 101 | + return {}; |
| 102 | + } |
| 103 | + |
| 104 | + return result; |
| 105 | +} |
| 106 | + |
| 107 | +std::string OpenStreetMap::Format( |
| 108 | + std::string input, |
| 109 | + const std::unordered_map<std::string, std::string>& values) { |
| 110 | + for (const auto& [key, value] : values) { |
| 111 | + std::string placeholder = "{" + key + "}"; |
| 112 | + size_t pos = input.find(placeholder); |
| 113 | + if (pos != std::string::npos) { |
| 114 | + input.replace(pos, placeholder.length(), value); |
| 115 | + } |
| 116 | + } |
| 117 | + return input; |
| 118 | +} |
0 commit comments