|
| 1 | +/*** |
| 2 | + * Run search engine powered by SerpApi |
| 3 | + */ |
| 4 | + |
| 5 | +#include <serpapi.hpp> |
| 6 | + |
| 7 | +namespace serpapi |
| 8 | +{ |
| 9 | + |
| 10 | +const static string JSON_FORMAT = "json"; |
| 11 | +const static string HTML_FORMAT = "html"; |
| 12 | +const static string HOST = "https://serpapi.com"; |
| 13 | + |
| 14 | +using namespace rapidjson; |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +Client::Client(map<string, string> parameter) |
| 18 | +{ |
| 19 | + this->parameter = parameter; |
| 20 | +} |
| 21 | + |
| 22 | +/*** |
| 23 | +* Get HTML search results |
| 24 | +*/ |
| 25 | +string Client::html(map<string, string> parameter) |
| 26 | +{ |
| 27 | + return Client::get("/search", HTML_FORMAT, parameter); |
| 28 | +} |
| 29 | + |
| 30 | +Document Client::search(map<string, string> parameter) |
| 31 | +{ |
| 32 | + return Client::json("/search", parameter); |
| 33 | +} |
| 34 | + |
| 35 | +Document Client::searchArchive(string searchID) |
| 36 | +{ |
| 37 | + return Client::json("/searches/" + searchID + ".json", map<string, string>()); |
| 38 | +} |
| 39 | + |
| 40 | +Document Client::account(map<string, string> parameter) |
| 41 | +{ |
| 42 | + return Client::json("/account.json", parameter); |
| 43 | +} |
| 44 | + |
| 45 | +Document Client::location(map<string, string> parameter) |
| 46 | +{ |
| 47 | + return Client::json("/locations.json", parameter); |
| 48 | +} |
| 49 | + |
| 50 | +Document Client::json(string uri, map<string, string> parameter) |
| 51 | +{ |
| 52 | + string buf = get(uri, JSON_FORMAT, parameter); |
| 53 | + const char* json = buf.c_str(); |
| 54 | + Document d; |
| 55 | + d.Parse(json); |
| 56 | + return d; |
| 57 | +} |
| 58 | + |
| 59 | +string Client::url(string output, map<string, string> parameter) |
| 60 | +{ |
| 61 | + map<string, string> param(parameter); |
| 62 | + string s = ""; |
| 63 | + map<string,string>::iterator it; |
| 64 | + for (it = param.begin(); it != param.end(); ++it) |
| 65 | + { |
| 66 | + if (s != "") |
| 67 | + { |
| 68 | + s += "&"; |
| 69 | + } |
| 70 | + // encode each value in case of special character |
| 71 | + s += it->first + "=" + it->second; |
| 72 | + } |
| 73 | + |
| 74 | + // append output format |
| 75 | + s += "&output=" + output; |
| 76 | + |
| 77 | + // append source language |
| 78 | + s += "&source=serpapi-cpp:1.0.0"; |
| 79 | + |
| 80 | + return s; |
| 81 | +} |
| 82 | + |
| 83 | +string Client::get(string uri, string output, map<string, string> parameter) |
| 84 | +{ |
| 85 | + curl_global_init(CURL_GLOBAL_DEFAULT); |
| 86 | + curl = curl_easy_init(); |
| 87 | + const string url = HOST + uri + "?" + this->url(output, parameter); |
| 88 | + cout << "url: " + url << endl; |
| 89 | + curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); |
| 90 | + curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); |
| 91 | + curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout); |
| 92 | + curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); |
| 93 | + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); |
| 94 | + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); |
| 95 | + |
| 96 | + long httpCode(0); |
| 97 | + unique_ptr<string> httpData(new string()); |
| 98 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback); |
| 99 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, httpData.get()); |
| 100 | + |
| 101 | + // execute search |
| 102 | + curl_easy_perform(curl); |
| 103 | + curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode); |
| 104 | + curl_easy_cleanup(curl); |
| 105 | + |
| 106 | + if(httpCode != 200) { |
| 107 | + // TODO what is the best solution in C++ |
| 108 | + return nullptr; |
| 109 | + } |
| 110 | + curl_global_cleanup(); |
| 111 | + return *httpData.get(); |
| 112 | +} |
| 113 | + |
| 114 | +} |
| 115 | + |
0 commit comments