Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def pkg_config_env
existing_path = ENV['PKG_CONFIG_PATH']
pkg_paths << existing_path if existing_path && !existing_path.empty?

"PKG_CONFIG_PATH='#{pkg_paths.join(':')}'"
require 'shellwords'
"PKG_CONFIG_PATH=#{Shellwords.escape(pkg_paths.join(':'))}"
end

desc('initialize meson build')
Expand Down Expand Up @@ -78,30 +79,34 @@ end

desc('release current library')
task release: [:default] do
require 'shellwords'

# Extract version from meson.build
meson_build = File.read('meson.build')
version = meson_build.match(/version\s*:\s*'([^']+)'/)[1]
tag = "#{version}"
version_match = File.read('meson.build').match(/version\s*:\s*'([^']+)'/)
abort "Unable to extract version from meson.build. Check version format." unless version_match

version = version_match[1]
tag = "v#{version}"

puts "Releasing #{tag}..."

# Check if tag already exists
if `git tag -l #{tag}`.strip == tag
if `git tag -l #{Shellwords.escape(tag)}`.strip == tag
puts "Tag #{tag} already exists. Skipping git tag."
else
# Ensure working directory is clean
if !`git status --porcelain`.strip.empty?
puts "Working directory is not clean. Please commit or stash changes."
exit 1
unless `git status --porcelain`.strip.empty?
abort "Working directory is not clean. Please commit or stash changes."
end

sh "git tag -a #{tag} -m 'Release #{tag}'"
sh "git tag -a #{Shellwords.escape(tag)} -m #{Shellwords.escape("Release #{tag}")}"
puts "Created tag #{tag}."
end

# Create distribution package
sh "meson dist -C build"

# meson dist performs a build and test in a temporary directory, so it needs the env
sh "#{pkg_config_env} meson dist -C build"

puts "\nRelease #{tag} completed successfully!"
puts "Next steps:"
puts "1. git push origin #{tag}"
Expand Down
25 changes: 14 additions & 11 deletions src/callback.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#include <cstdint>
#include <iostream>
#include <memory>
#include <limits>
#include <string>

using namespace std;
namespace serpapi {
size_t callback(const char* in, size_t size, size_t num, std::string* out) {
if (!out || !in) return 0;
if (size == 0 || num == 0) return 0;
if (size > std::numeric_limits<size_t>::max() / num) return 0;

namespace serpapi
{
size_t callback(const char* in, size_t size, size_t num, string* out)
{
const size_t totalBytes(size*num);
out->append(in, totalBytes);
return totalBytes;
}
const size_t totalBytes = size * num;
try {
out->append(in, totalBytes);
return totalBytes;
} catch (const std::exception&) {
return 0;
}
}
} // namespace serpapi
109 changes: 60 additions & 49 deletions src/serpapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,101 +4,105 @@

#include "callback.hpp"
#include <serpapi.hpp>
#include <mutex>
#include <sstream>

namespace serpapi {

const static std::string HOST = "https://serpapi.com";
const static std::string NAME = "serpapi-cpp";
const static std::string VERSION = "0.3.0";

using namespace rapidjson;
using namespace std;
static std::once_flag curl_init_flag;

Client::Client(const map<string, string> &parameter) {
Client::Client(const std::map<std::string, std::string> &parameter) {
this->parameter = parameter;
}

Client::~Client() {}

/***
* Get HTML search results
*/
string Client::html(const map<string, string> &parameter) {
std::string Client::html(const std::map<std::string, std::string> &parameter) {
GetResponse gr = Client::get("/search", "html", parameter);
return gr.payload;
}

Document Client::search(const map<string, string> &parameter) {
rapidjson::Document Client::search(const std::map<std::string, std::string> &parameter) {
return Client::json("/search", parameter);
}

Document Client::search_archive(const string &id) {
return Client::json("/searches/" + id + ".json", map<string, string>());
rapidjson::Document Client::search_archive(const std::string &id) {
return Client::json("/searches/" + id + ".json", std::map<std::string, std::string>());
}

Document Client::account(const map<string, string> &parameter) {
rapidjson::Document Client::account(const std::map<std::string, std::string> &parameter) {
return Client::json("/account.json", parameter);
}

Document Client::location(const map<string, string> &parameter) {
rapidjson::Document Client::location(const std::map<std::string, std::string> &parameter) {
return Client::json("/locations.json", parameter);
}

Document Client::json(const string &uri, const map<string, string> &parameter) {
rapidjson::Document Client::json(const std::string &uri, const std::map<std::string, std::string> &parameter) {
GetResponse gr = get(uri, "json", parameter);
const char *json_payload = gr.payload.c_str();
Document d;
d.Parse(json_payload);
rapidjson::Document d;
d.Parse(gr.payload.c_str());
if (d.HasParseError()) {
d.SetObject();
d.AddMember("error", "JSON parse error", d.GetAllocator());
}
return d;
}

string encodeUrl(CURL *curl, const map<string, string> &parameter) {
string s = "";
map<string, string>::const_iterator it;
for (it = parameter.begin(); it != parameter.end(); ++it) {
if (s != "") {
s += "&";
}
std::string encodeUrl(CURL *curl, const std::map<std::string, std::string> &parameter) {
std::ostringstream oss;
bool first = true;

for (const auto& entry : parameter) {
char *escaped_key = curl_easy_escape(curl, entry.first.c_str(), entry.first.length());
char *escaped_value = curl_easy_escape(curl, entry.second.c_str(), entry.second.length());

char *escaped_key =
curl_easy_escape(curl, it->first.c_str(), it->first.length());
char *escaped_value =
curl_easy_escape(curl, it->second.c_str(), it->second.length());
if (!escaped_key || !escaped_value) {
if (escaped_key) curl_free(escaped_key);
if (escaped_value) curl_free(escaped_value);
return "";
}

s += string(escaped_key) + "=" + string(escaped_value);
if (!first) oss << "&";
oss << escaped_key << "=" << escaped_value;
first = false;

curl_free(escaped_key);
curl_free(escaped_value);
}
return s;
return oss.str();
}

string Client::url(CURL *curl, const string &output,
const map<string, string> &parameter) {
// encode parameter
string url_str = encodeUrl(curl, parameter);
std::string Client::url(CURL *curl, const std::string &output,
const std::map<std::string, std::string> &parameter) {
std::string url_str = encodeUrl(curl, parameter);
if (this->parameter.size() > 0) {
if (!url_str.empty())
url_str += "&";
if (!url_str.empty()) url_str += "&";
url_str += encodeUrl(curl, this->parameter);
}
// append output format
url_str += "&output=" + output;
// append source language
url_str += "&source=" + NAME + ":" + VERSION;
return url_str;
}

GetResponse Client::get(const string &uri, const string &output,
const map<string, string> &parameter) {
static bool initialized = false;
if (!initialized) {
curl_global_init(CURL_GLOBAL_DEFAULT);
initialized = true;
}
GetResponse Client::get(const std::string &uri, const std::string &output,
const std::map<std::string, std::string> &parameter) {
std::call_once(curl_init_flag, []() { curl_global_init(CURL_GLOBAL_DEFAULT); });

CURL *curl = curl_easy_init();
const string url_params = this->url(curl, output, parameter);
const string full_url = HOST + uri + "?" + url_params;
if (!curl) {
GetResponse gr;
gr.httpCode = 0;
gr.payload = "CURL initialization failed";
return gr;
}

const std::string url_params = this->url(curl, output, parameter);
const std::string full_url = HOST + uri + "?" + url_params;

curl_easy_setopt(curl, CURLOPT_URL, full_url.c_str());
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
Expand All @@ -108,12 +112,19 @@ GetResponse Client::get(const string &uri, const string &output,
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);

long httpCode(0);
string httpData;
std::string httpData;
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &httpData);

// execute search
curl_easy_perform(curl);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
curl_easy_cleanup(curl);
GetResponse gr;
gr.httpCode = 0;
gr.payload = std::string("CURL error: ") + curl_easy_strerror(res);
return gr;
}

curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
curl_easy_cleanup(curl);

Expand Down
2 changes: 2 additions & 0 deletions src/serpapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Client {
explicit Client(const std::map<std::string, std::string> &parameter);
~Client();

void setTimeout(int seconds) { timeout = seconds; }

rapidjson::Document
search(const std::map<std::string, std::string> &parameter = {});

Expand Down
Loading