1 //! \file WikiWalker.cpp
3 #include "WikiWalker.h"
9 #include "LUrlParser.h"
11 #include "JsonSerializer.h"
12 #include "StringUtils.h"
13 #include "WalkerException.h"
14 #include "WikimediaApi.h"
16 // since the class is names like the namespace, this is a bit awkward...
20 void WikiWalker::start(const std::string
& url
)
22 auto info
= WikimediaApiUtils::parseArticleUrl(url
);
23 WikimediaApi
wapi(info
.apiBaseUrl
);
25 wapi
.fetchForwardLinks(info
.articleTitle
,
26 WikimediaApi::WikimediaGenerator::NoGenerator
,
30 wapi
.fetchForwardLinks(
32 WikimediaApi::WikimediaGenerator::ForwardLinkGenerator
,
37 void WikiWalker::readCache(const std::string
& cacheFile
)
40 std::ifstream
cache(cacheFile
);
42 // assumption: having write-only access to a file is so rare that I don't
43 // care also, currently the file is used for both read and write, so
44 // initially it won't exist.
45 if(!cache
.is_open()) {
49 jser
.deserialize(articleSet_
, cache
);
51 // doesn't work anymore since jsoncpp class changed
52 // assert(cache.eof());
56 throw WalkerException("Error reading from file");
60 void WikiWalker::writeCache(const std::string
& cacheFile
)
64 std::ofstream
cache(cacheFile
, std::ios::trunc
);
66 if(!cache
.is_open()) {
67 throw WalkerException("Error writing to cache file. Check permissions.");
70 w
.serialize(articleSet_
, cache
);
72 if(cache
.fail() || cache
.bad()) {
74 throw WalkerException("I/O eception when writing to cache file");
80 } // namespace WikiWalker