generated from pku-software/docman-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitation.cpp
177 lines (155 loc) · 4.98 KB
/
citation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "./citation.h"
#include "./utils.hpp"
std::string getFromWeb(const std::string& resource) {
/*
This function is used to get some information from the web.
*/
httplib::Client client{API_ENDPOINT};
auto res = client.Get(resource);
if (res && res->status == httplib::OK_200) {
return res->body;
} else {
std::exit(1);
return std::string();
}
}
// Citation class
Citation::Citation(const nlohmann::json& data) {
/*
This function is used to initialize a citation.
*/
if (!data.contains("id")) {
std::exit(1);
}
id = data["id"].get<std::string>();
this->data = data;
}
std::string Citation::getResource() const {
std::exit(1);
}
std::string Citation::toString() const {
return std::string("[" + id + "] ");
}
// Article class
Article::Article(const nlohmann::json& data) : Citation(data) {
/*
This function is used to initialize an article.
*/
if (!data.contains("journal") || !data.contains("year") || !data.contains("volume") || !data.contains("issue")) {
std::exit(1);
}
}
std::string Article::getResource() const {
std::exit(1);
}
std::string Article::toString() const {
/*
This function is used to describe an article.
*/
auto info = data;
if (data.is_null()) {
std::exit(1);
}
if (info.contains("title") &&
info.contains("author") &&
info.contains("journal") &&
info.contains("year") &&
info.contains("volume") &&
info.contains("issue") &&
info["title"].is_string() &&
info["author"].is_string() &&
info["journal"].is_string() &&
info["year"].is_number() &&
info["volume"].is_number() &&
info["issue"].is_number())
{
std::string title = info["title"].get<std::string>();
std::string author = info["author"].get<std::string>();
std::string journal = info["journal"].get<std::string>();
std::string year = std::to_string(info["year"].get<int>());
std::string volume = std::to_string(info["volume"].get<int>());
std::string issue = std::to_string(info["issue"].get<int>());
return std::string(
"[" + id + "] article: " + author + ", " + title + ", " + journal + ", " + year + ", " + volume + ", " + issue
);
} else {
std::exit(1);
}
}
// Book class
Book::Book(const nlohmann::json& data) : Citation(data) {
/*
This function is used to initialize a book.
*/
if (!data.contains("isbn") || !data["isbn"].is_string()) {
std::exit(1);
}
isbn = data["isbn"].get<std::string>();
}
std::string Book::getResource() const {
/*
This function is used to get book information from the web.
It sends a GET request to the API with the ISBN of the book.
If the response is OK (HTTP 200), it processes the response.
*/
return getFromWeb("/isbn/" + encodeUriComponent(isbn));
}
std::string Book::toString() const {
/*
This function is used to describe a book.
*/
nlohmann::json info = nlohmann::json::parse(getResource());
if (data.is_null()) {
std::exit(1);
}
if (info.contains("author") &&
info.contains("title") &&
info.contains("publisher") &&
info.contains("year") &&
info["author"].is_string() &&
info["title"].is_string() &&
info["publisher"].is_string() &&
info["year"].is_string())
{
std::string author = info["author"].get<std::string>();
std::string title = info["title"].get<std::string>();
std::string publisher = info["publisher"].get<std::string>();
std::string year = info["year"].get<std::string>();
return std::string("[" + id + "] book: " + author + ", " + title + ", " + publisher + ", " + year);
} else {
std::exit(1);
}
}
// WebPage class
WebPage::WebPage(const nlohmann::json& data) : Citation(data) {
/*
This function is used to initialize a webpage.
*/
if (!data.contains("url") || !data["url"].is_string()) {
std::exit(1);
}
url = data["url"].get<std::string>();
}
std::string WebPage::getResource() const {
/*
This function is used to get website information from the web.
It sends a GET request to the API with the URL of the website.
If the response is OK (HTTP 200), it processes the response.
*/
return getFromWeb("/title/" + encodeUriComponent(url));
}
std::string WebPage::toString() const {
/*
This function is used to describe a webpage.
*/
nlohmann::json info = nlohmann::json::parse(getResource());
if (data.is_null()) {
std::exit(1);
}
if (info.contains("title") && info["title"].is_string()) {
std::string title = info["title"].get<std::string>();
return std::string("[" + id + "] webpage: " + title + ". Available at " + url);
} else {
std::exit(1);
}
}