Instantly share code, notes, and snippets.

@Puciek /client.cpp
Last active Aug 29, 2015

Embed
What would you like to do?
#include <boost/asio/ip/tcp.hpp>
#include "jsoncpp/json/json.h"
#include <zlib.h>
#include <boost/python.hpp>
using namespace std;
std::string decompress_string(const std::string& str)
{
z_stream zs; // z_stream is zlib's control structure
memset(&zs, 0, sizeof(zs));
if (inflateInit(&zs) != Z_OK)
throw(std::runtime_error("inflateInit failed while decompressing."));
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size();
int ret;
char outbuffer[32768];
std::string outstring;
// get the decompressed bytes blockwise using repeated calls to inflate
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = inflate(&zs, 0);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
inflateEnd(&zs);
if (ret != Z_STREAM_END) { // an error occurred that was not EOF
std::ostringstream oss;
oss << "Exception during zlib decompression: (" << ret << ") "
<< zs.msg;
throw(std::runtime_error(oss.str()));
}
return outstring;
}
std::string query_google(string url, string referer, string region, string useragent, string priority, string key)
{
Json::Value details;
details["url"] = url;
details["referer"] = referer;
details["region"] = region;
details["useragent"] = useragent;
details["priority"] = priority;
Json::Value request;
request["key"] = key;
request["request"] = details;
const char* host = "api.proxyrotation.com";
const char* port = "9005";
using namespace boost::asio;
io_service io_service;
ip::tcp::resolver resolver(io_service);
ip::tcp::resolver::query query(host, port);
ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
ip::tcp::resolver::iterator end;
ip::tcp::socket socket(io_service);
boost::system::error_code error = boost::asio::error::host_not_found;
socket.connect(*endpoint_iterator++, error);
Json::StyledWriter writer;
socket.write_some(boost::asio::buffer(writer.write(request)));
boost::array<char, 120000> buf;
std::string response;
size_t len = 0;
while (len = socket.read_some(boost::asio::buffer(buf), error)) {
std::string sbuf(buf.begin(), len);
response.append(sbuf);
}
socket.close();
return decompress_string(response);
}
BOOST_PYTHON_MODULE(client)
{
using namespace boost::python;
def("query_google", query_google);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment