Created
January 13, 2014 14:48
-
-
Save Gocrazy/8401594 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "smaato_bid_request.h" | |
| #include "smaato.h" | |
| #include "smaato_parsing.h" | |
| #include "jml/utils/json_parsing.h" | |
| using namespace std; | |
| namespace RTBKIT { | |
| BidRequest * | |
| fromSmaato(Smaato::BidRequest && req, | |
| const std::string & provider, | |
| const std::string & exchange) | |
| { | |
| std::unique_ptr<BidRequest> result(new BidRequest()); | |
| std::cerr << "from Smaato..." << std::endl; | |
| result->auctionId = std::move(req.id); | |
| result->timestamp = Date::now(); | |
| result->provider = provider; | |
| result->exchange = (exchange.empty() ? provider : exchange); | |
| result->ipAddress = "127.0.0.1"; | |
| //device | |
| if (req.device) { | |
| result->device.reset(req.device.release()); | |
| result->language = result->device->language; | |
| result->userAgent = result->device->ua; | |
| if (!result->device->ip.empty()) | |
| result->ipAddress = result->device->ip; | |
| else if (!result->device->ipv6.empty()) | |
| result->ipAddress = result->device->ipv6; | |
| if (result->device->geo) { | |
| const auto & g = *result->device->geo; | |
| auto & l = result->location; | |
| l.countryCode = g.country; | |
| if (!g.region.empty()) | |
| l.regionCode = g.region; | |
| else l.regionCode = g.regionfips104; | |
| l.cityName = g.city; | |
| } | |
| } | |
| if (req.user) { | |
| result->user.reset(req.user.release()); | |
| for (auto & d: result->user->data) { | |
| string key; | |
| if (d.id) | |
| key = d.id.toString(); | |
| else key = d.name; | |
| vector<string> values; | |
| for (auto & v: d.segment) { | |
| if (v.id) | |
| values.push_back(v.id.toString()); | |
| else if (!v.name.empty()) | |
| values.push_back(v.name); | |
| } | |
| result->segments.addStrings(key, values); | |
| } | |
| if (result->user->tz.val != -1) | |
| result->location.timezoneOffsetMinutes = result->user->tz.val; | |
| if (result->user->id) | |
| result->userIds.add(result->user->id, ID_EXCHANGE); | |
| if (result->user->buyeruid) | |
| result->userIds.add(result->user->buyeruid, ID_PROVIDER); | |
| } | |
| if (!req.cur.empty()) { | |
| for (unsigned i = 0; i < req.cur.size(); ++i) { | |
| result->bidCurrency.push_back(Amount::parseCurrency(req.cur[i])); | |
| } | |
| } | |
| else { | |
| result->bidCurrency.push_back(CurrencyCode::CC_USD); | |
| } | |
| auto onImpression = [&] (Smaato::Impression && imp) | |
| { | |
| AdSpot spot(std::move(imp)); | |
| result->imp.emplace_back(std::move(spot)); | |
| }; | |
| result->imp.reserve(req.imp.size()); | |
| for (auto & i: req.imp) | |
| onImpression(std::move(i)); | |
| if (req.site && req.app) | |
| throw ML::Exception("can't have site and app"); | |
| if (req.site) { | |
| result->site.reset(req.site.release()); | |
| if (!result->site->page.empty()) | |
| result->url = result->site->page; | |
| else if (result->site->id) | |
| result->url = Url("http://" + result->site->id.toString() + ".siteid/"); | |
| } | |
| else if (req.app) { | |
| result->app.reset(req.app.release()); | |
| if (!result->app->bundle.empty()) | |
| result->url = Url(result->app->bundle); | |
| else if (result->app->id) | |
| result->url = Url("http://" + result->app->id.toString() + ".appid/"); | |
| } | |
| result->ext = std::move(req.ext); | |
| result->segments.addStrings("openrtb-wseat", req.wseat); | |
| return result.release(); | |
| } | |
| namespace { | |
| static DefaultDescription<Smaato::BidRequest> desc; | |
| } // file scope | |
| BidRequest * | |
| SmaatoBidRequestParser:: | |
| parseBidRequest(const std::string & jsonValue, | |
| const std::string & provider, | |
| const std::string & exchange){ | |
| const char * strStart = jsonValue.c_str(); | |
| StreamingJsonParsingContext jsonContext(jsonValue, strStart, | |
| strStart + jsonValue.size()); | |
| std::cerr << "SmaatoBidRequestParser::parseBidRequest1" << std::endl; | |
| Smaato::BidRequest req; | |
| desc.parseJson(&req, jsonContext); | |
| return fromSmaato(std::move(req), provider, exchange); | |
| } | |
| BidRequest * | |
| SmaatoBidRequestParser:: | |
| parseBidRequest(ML::Parse_Context & context, | |
| const std::string & provider, | |
| const std::string & exchange) | |
| { | |
| StreamingJsonParsingContext jsonContext(context); | |
| std::cerr << "SmaatoBidRequestParser::parseBidRequest2" << std::endl; | |
| Smaato::BidRequest req; | |
| desc.parseJson(&req, jsonContext); | |
| return fromSmaato(std::move(req), provider, exchange); | |
| } | |
| } // namespace RTBKIT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment