|
| 1 | +#include<format> |
| 2 | +#include<list> |
| 3 | +#include<sstream> |
| 4 | +#include<string> |
| 5 | +#include<curlpp/cURLpp.hpp> |
| 6 | +#include<curlpp/Easy.hpp> |
| 7 | +#include<curlpp/Form.hpp> |
| 8 | +#include<curlpp/Options.hpp> |
| 9 | +#include<nlohmann/json.hpp> |
| 10 | +#include"wiki.hpp" |
| 11 | +std::string wiki::get(curlpp::Easy &request){ |
| 12 | + std::stringstream response; |
| 13 | + request.setOpt<curlpp::Options::WriteStream>(&response); |
| 14 | + request.perform(); |
| 15 | + return response.str(); |
| 16 | +} |
| 17 | +std::string wiki::get(const std::string &url,const std::list<std::string> &header){ |
| 18 | + curlpp::Easy request; |
| 19 | + request.setOpt<curlpp::Options::Url>(url); |
| 20 | + request.setOpt<curlpp::Options::HttpHeader>(header); |
| 21 | + return get(request); |
| 22 | +} |
| 23 | +std::string wiki::get(const std::string &url,const std::string &cookiejar_path,const std::list<std::string> &header){ |
| 24 | + curlpp::Easy request; |
| 25 | + request.setOpt<curlpp::Options::Url>(url); |
| 26 | + request.setOpt<curlpp::Options::CookieJar>(cookiejar_path); |
| 27 | + request.setOpt<curlpp::Options::HttpHeader>(header); |
| 28 | + return get(request); |
| 29 | +} |
| 30 | +std::string wiki::get_token(const std::string &url,const std::string &cookiejar_path,const std::list<std::string> &header){ |
| 31 | + nlohmann::json response=nlohmann::json::parse(wiki::get(url,cookiejar_path,header)); |
| 32 | + const std::string token=response["query"]["tokens"]["logintoken"]; |
| 33 | + return token; |
| 34 | +} |
| 35 | +std::string wiki::login(const std::string &url,const std::string &cookie_path,const std::string &username,const std::string &password,const std::string &login_token,const std::list<std::string> &header){ |
| 36 | + curlpp::Easy request; |
| 37 | + request.setOpt<curlpp::Options::Url>(url); |
| 38 | + request.setOpt<curlpp::Options::CookieFile>(cookie_path); |
| 39 | + request.setOpt<curlpp::Options::CookieJar>(cookie_path); |
| 40 | + curlpp::Forms form; |
| 41 | + form.push_back(new curlpp::FormParts::Content("action","login")); |
| 42 | + form.push_back(new curlpp::FormParts::Content("lgname",username)); |
| 43 | + form.push_back(new curlpp::FormParts::Content("lgpassword",password)); |
| 44 | + form.push_back(new curlpp::FormParts::Content("lgtoken",login_token)); |
| 45 | + request.setOpt<curlpp::Options::HttpPost>(form); |
| 46 | + request.setOpt<curlpp::Options::HttpHeader>(header); |
| 47 | + return wiki::get(url); |
| 48 | +} |
0 commit comments