Forbid copy c'tor and assignment in ArticleCollection
[dueringa_WikiWalker.git] / src / CurlWikiGrabber.cpp
blobb9155795b0457003b744d2986786062043538938
1 #include "CurlWikiGrabber.h"
3 #ifndef DEBUG
4 #define NDEBUG
5 #endif
6 #include <cassert>
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);
11 return size * nmemb;
14 CurlWikiGrabber::CurlWikiGrabber()
16 int error = curl_global_init(CURL_GLOBAL_ALL);
17 if(error) {
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();
27 if(NULL == handle) {
28 throw WalkerException("error initiating curl");
31 CURLcode crv = curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
32 assert(crv == 0);
33 crv = curl_easy_setopt(handle, CURLOPT_USERAGENT, "WikiWalker/ test program");
34 assert(crv == 0);
35 crv = curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
36 assert(crv == 0);
38 std::string gotContent;
39 crv = curl_easy_setopt(handle, CURLOPT_WRITEDATA, &gotContent);
40 assert(crv == 0);
42 gotContent = "";
43 crv = curl_easy_perform(handle);
45 if(crv != 0) {
46 const char* err = curl_easy_strerror(crv);
47 std::string text(err);
48 throw WalkerException(text);
51 long httpcode = 0;
52 crv = curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &httpcode);
53 assert(crv == 0);
55 curl_easy_cleanup(handle);
57 handle = NULL;
59 if(httpcode != 200) {
60 return "";
63 return gotContent;
66 // note to self: API
67 // https://en.wikipedia.org/w/api.php
68 // /w/api.php?action=query&format=json&prop=links&plnamespace=0&titles=<title>
69 // maybe &pllimit=100