Why the f*ck is missing return not an error by default?
[dueringa_WikiWalker.git] / src / JsonToArticleConverter.cpp
blob5619ac86c30c804122ada3e07ec5e92cf7e75973
1 #include "JsonToArticleConverter.h"
2 #include "WalkerException.h"
4 #include <json/json.h>
6 //! \todo really ugly workaround, passing in the ArticleCollection instance... :/
7 Article* JsonToArticleConverter::convertToArticle(std::string json, ArticleCollection& articleCache)
9 Json::Reader reader;
10 Json::Value document;
11 bool success = reader.parse(json, document, false);
13 if(!success) {
14 throw WalkerException("Error parsing JSON");
17 auto allPages = document.get("query", Json::Value::nullSingleton())
18 .get("pages", Json::Value::nullSingleton());
20 // only get first page
21 auto wantedPage = allPages.get(allPages.getMemberNames()[0],
22 Json::Value::nullSingleton());
24 if(wantedPage.isMember("missing")) {
25 throw WalkerException("Article doesn't exist");
26 } else if(wantedPage.isMember("invalid")) {
27 throw WalkerException("Invalid article");
30 //! get normalized title not necessary, "title" is already
31 std::string wantedArticleTitle = wantedPage.get("title", Json::Value::nullSingleton()).asString();
32 Article* wantedArticle = articleCache.get(wantedArticleTitle);
34 if(wantedArticle == nullptr) {
35 wantedArticle = new Article(wantedArticleTitle);
38 articleCache.add(wantedArticle);
40 // add links
41 for(const auto &linked : wantedPage.get("links", Json::Value::nullSingleton())) {
42 auto linkedPageTitle = linked.get("title", Json::Value::nullSingleton()).asString();
43 auto par = articleCache.get(linkedPageTitle);
45 if(par == nullptr) {
46 par = new Article(linkedPageTitle);
47 articleCache.add(par);
50 wantedArticle->addLink(par);
53 wantedArticle->setAnalyzed(true);
55 if(!document.isMember("batchcomplete")) {
56 moreData = true;
57 continueString =
58 document.get("continue", Json::Value::nullSingleton())
59 .get("plcontinue", Json::Value::nullSingleton())
60 .asString();
61 } else {
62 moreData = false;
63 continueString = "";
66 return wantedArticle;