Skip to content

Commit 66310ff

Browse files
committed
added login_token module
modified: .gitignore new file: uploader/CMakeLists.txt new file: uploader/include/wiki.hpp new file: uploader/lib/libwiki.cpp new file: uploader/test/token.cpp
1 parent 0045396 commit 66310ff

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# CMake
2+
3+
build/
4+
15
# tex files not in archive
26
*.tex
37
!archive/*.tex

uploader/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(byrdocs_wikigraph_uploader VERSION 0.1)
3+
set(CMAKE_CXX_FLAGS "-Wall -Wextra")
4+
set(CMAKE_CXX_FLAGS_DEBUG "-g")
5+
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
6+
enable_testing()
7+
set(CMAKE_CXX_STANDARD 20)
8+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9+
file(GLOB_RECURSE HeaderFileList "include/*")
10+
file(GLOB_RECURSE LibraryFileList "lib/*")
11+
include_directories(include /usr/include /usr/local/include)
12+
link_directories(lib /usr/lib /usr/local/lib)
13+
add_library(wikibot ${HeaderFileList} ${LibraryFileList})
14+
find_package(curlpp NAMES curlpp curl)
15+
find_package(nlohmann_json 3.11.3 REQUIRED)
16+
target_link_libraries(wikibot PUBLIC nlohmann_json::nlohmann_json curlpp curl)
17+
add_executable(.token test/token.cpp)
18+
target_link_libraries(.token wikibot)
19+
add_test(NAME token COMMAND .token)

uploader/include/wiki.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include<list>
3+
#include<string>
4+
#include<curlpp/Easy.hpp>
5+
#include<nlohmann/json.hpp>
6+
namespace wiki{
7+
std::string get(curlpp::Easy &request);
8+
std::string get(const std::string &url,const std::list<std::string> &header={});
9+
std::string get(const std::string &url,const std::string &cookiejar_path,const std::list<std::string> &header={});
10+
std::string get_token(const std::string &url,const std::string &cookiejar_path,const std::list<std::string> &header={});
11+
std::string 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={});
12+
}

uploader/lib/libwiki.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

uploader/test/token.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include<exception>
2+
#include<format>
3+
#include<iostream>
4+
#include<list>
5+
#include<string>
6+
#include<nlohmann/json.hpp>
7+
#include"wiki.hpp"
8+
int main(){
9+
try{
10+
const std::string api="http://localhost/api.php?";
11+
const std::string login_token=wiki::get_token(api+"format=json&action=query&meta=tokens&type=*","/tmp/wiki_login_token");
12+
std::clog<<login_token<<std::endl;
13+
assert(login_token.length()==42);
14+
assert(login_token.ends_with("+\\"));
15+
}catch(const curlpp::RuntimeError &e){
16+
std::cerr<<e.what()<<std::endl;
17+
assert(false);
18+
}catch(const curlpp::LogicError &e){
19+
std::cerr<<e.what()<<std::endl;
20+
assert(false);
21+
}catch(const std::exception &e){
22+
std::cerr<<e.what()<<std::endl;
23+
assert(false);
24+
}
25+
}

0 commit comments

Comments
 (0)