1 //! \file CurlWikiGrabber.cpp
3 #include "CurlWikiGrabber.h"
4 #include "WalkerException.h"
11 static size_t write_callback(char *ptr
, size_t size
, size_t nmemb
, void *userdata
)
13 static_cast<std::string
*>(userdata
)->append(ptr
, size
* nmemb
);
17 CurlWikiGrabber::CurlWikiGrabber()
19 int error
= curl_global_init(CURL_GLOBAL_ALL
);
21 throw WalkerException("CURL init failed");
25 //! \todo Curl return code checking
26 std::string
CurlWikiGrabber::grabUrl(std::string url
) const
28 CURL
*handle
= curl_easy_init();
31 throw WalkerException("error initiating curl");
34 CURLcode crv
= curl_easy_setopt(handle
, CURLOPT_URL
, url
.c_str());
36 crv
= curl_easy_setopt(handle
, CURLOPT_USERAGENT
, "WikiWalker/ test program");
38 crv
= curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, write_callback
);
41 std::string gotContent
;
42 crv
= curl_easy_setopt(handle
, CURLOPT_WRITEDATA
, &gotContent
);
46 crv
= curl_easy_perform(handle
);
49 const char* err
= curl_easy_strerror(crv
);
50 std::string
text(err
);
51 throw WalkerException(text
);
55 crv
= curl_easy_getinfo(handle
, CURLINFO_RESPONSE_CODE
, &httpcode
);
58 curl_easy_cleanup(handle
);
70 // https://en.wikipedia.org/w/api.php
71 // /w/api.php?action=query&format=json&prop=links&plnamespace=0&titles=<title>