Add documentation regarding storage
[dueringa_WikiWalker.git] / src / CurlWikiGrabber.cpp
blobf67bf88d47b352cd30e751fb2f95a61b844ed5e3
1 #include "CurlWikiGrabber.h"
3 static size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
5 static_cast<std::string*>(userdata)->append(ptr, size * nmemb);
6 return size * nmemb;
9 CurlWikiGrabber::CurlWikiGrabber()
11 int error = curl_global_init(CURL_GLOBAL_ALL);
12 if(error)
14 throw WalkerException("CURL init failed");
18 //! \todo change to passing page title?
19 //! \todo Curl return code checking
20 std::string CurlWikiGrabber::grabUrl(std::string url) const
22 CURL *handle = curl_easy_init();
24 if(NULL == handle)
26 throw WalkerException("error initiating curl");
29 curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
30 curl_easy_setopt(handle, CURLOPT_USERAGENT, "WikiWalker/ test program");
31 curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_callback);
32 std::string gotContent;
33 curl_easy_setopt(handle, CURLOPT_WRITEDATA, &gotContent);
36 gotContent = "";
37 curl_easy_perform(handle);
39 long httpcode = 0;
40 curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &httpcode);
42 curl_easy_cleanup(handle);
43 handle = NULL;
45 if(httpcode != 200) {
46 return "";
49 return gotContent;
52 // note to self: API
53 // https://en.wikipedia.org/w/api.php
54 // /w/api.php?action=query&format=json&prop=links&plnamespace=0&titles=<title>
55 // maybe &pllimit=100