1 #include "CurlWikiGrabber.h"
8 static size_t write_callback(char *ptr
, size_t size
, size_t nmemb
, void *userdata
)
10 static_cast<std::string
*>(userdata
)->append(ptr
, size
* nmemb
);
14 CurlWikiGrabber::CurlWikiGrabber()
16 int error
= curl_global_init(CURL_GLOBAL_ALL
);
18 throw WalkerException("CURL init failed");
22 //! \todo Curl return code checking
23 std::string
CurlWikiGrabber::grabUrl(std::string url
) const
25 CURL
*handle
= curl_easy_init();
28 throw WalkerException("error initiating curl");
31 CURLcode crv
= curl_easy_setopt(handle
, CURLOPT_URL
, url
.c_str());
33 crv
= curl_easy_setopt(handle
, CURLOPT_USERAGENT
, "WikiWalker/ test program");
35 crv
= curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, write_callback
);
38 std::string gotContent
;
39 crv
= curl_easy_setopt(handle
, CURLOPT_WRITEDATA
, &gotContent
);
43 crv
= curl_easy_perform(handle
);
46 const char* err
= curl_easy_strerror(crv
);
47 std::string
text(err
);
48 throw WalkerException(text
);
52 crv
= curl_easy_getinfo(handle
, CURLINFO_RESPONSE_CODE
, &httpcode
);
55 curl_easy_cleanup(handle
);
67 // https://en.wikipedia.org/w/api.php
68 // /w/api.php?action=query&format=json&prop=links&plnamespace=0&titles=<title>