-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
474 lines (418 loc) · 12.3 KB
/
Copy pathmain.cpp
File metadata and controls
474 lines (418 loc) · 12.3 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
#include <curl/curl.h>
#include <string.h>
#include <unordered_set>
#include <iostream>
#include <fstream>
#include <map>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// used to have a dynamic string
typedef struct Response
{
char *string;
size_t size;
};
struct TestCaseResponse
{
std::vector<std::string> testCases;
std::vector<std::pair<std::string, std::string>> testCaseParams;
};
size_t write_chunk(void *data, size_t size, size_t nmemb, void *userData);
void formatResponse(char *response);
std::string FormatHTMLToString(const std::string &response);
TestCaseResponse GetTestCases(const std::string &content);
std::pair<std::string, std::string> GetParamName(const std::string ¶m);
void CreateJSON(json *response, const TestCaseResponse &testCases);
int main()
{
std::string questionName = "";
std::cout << "Enter Leetcode question name: " << std::endl;
std::cin >> questionName;
CURL *curl;
CURLcode result;
// Initialize CURL
curl = curl_easy_init();
if (curl == nullptr)
{
std::cerr << "HTTP REQUEST FAILED: curl_easy_init() failed!" << std::endl;
return -1;
}
else
{
std::cout << "Curl initialized successfully!" << std::endl;
}
Response response;
response.string = (char *)malloc(1);
response.size = 0;
// Set options for the HTTP request
curl_easy_setopt(curl, CURLOPT_URL,
"https://leetcode.com/graphql");
// Set Post data (like JSON body) to match leetcode graph ql query
json query = {
{"query", "query questionData($titleSlug: String!) { question(titleSlug: $titleSlug) { title content difficulty topicTags { name } hints } }"},
{"variables", {
{"titleSlug", questionName} // This can now be easily modified
}}};
const std::string postData = query.dump();
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
// Set headers for JSON data
struct curl_slist *headers = nullptr;
headers = curl_slist_append(headers, "Content-Type: application/json");
std::string referer = "Referrer: https://leetcode.com/problems/" + questionName + "/";
headers = curl_slist_append(headers, referer.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
/**
* WriteFunction allows for specifying a callback function
* Curl_easy_perfrom will call this function repeatedly
* Each time it is called the pointer is passed to a new chunk of response
* string
*/
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_chunk);
// Address of response string is passed in write_chunk as userData
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&response);
// Perform the HTTP request
result = curl_easy_perform(curl);
if (result != CURLE_OK)
{
std::cerr << "Error: " << curl_easy_strerror(result) << std::endl;
curl_easy_cleanup(curl);
return -1;
}
formatResponse(response.string);
free(response.string);
// Cleanup
curl_easy_cleanup(curl);
return 0;
}
// returns number of bytes in the chunk
// data is set to a ptr that points to block of data recieved in this chunk
// nmemb is the number of bytes in the block of data
// userData points to what we want (points to where the response string is stored)
size_t write_chunk(void *data, size_t size, size_t nmemb, void *userData)
{
// size is always 1
size_t real_size = size * nmemb;
Response *response = (Response *)userData;
// allocate more space for chunk that was recieved
// response->size is size of existing mem and real_size is the size recieved and +1 accounts for null
char *ptr = (char *)realloc(response->string, response->size + real_size + 1);
if (ptr == nullptr)
{
std::cerr << "Problem reallocating space for chunk recieved" << std::endl;
return 0;
}
// set response string to the new (larger) memory address
response->string = ptr;
// append new porition onto existing string
memcpy(&(response->string[response->size]), data, real_size);
// update strings size
response->size += real_size;
// append null character
response->string[response->size] = '\0';
return real_size;
}
/**
* Returns a map containing the following tags stored as keys
* and their description as their value.
*
* title content difficulty topicTags { name } hints
*
* Assumes json response will use the tags in the given order above.
*/
void formatResponse(char *response)
{
std::vector<std::string> currentTags = {"title", "content", "difficulty", "topicTags", "hints"};
try
{
json parsed = json::parse(response);
json question = parsed["data"]["question"];
TestCaseResponse testCases;
for (const auto &tag : currentTags)
{
if (question.contains(tag) && tag == "topicTags")
{
std::vector<std::string> topics;
for (auto topic : question[tag])
{
topics.push_back(topic["name"]);
}
question[tag] = topics;
continue;
}
if (question.contains(tag) && tag == "hints")
{
if (question[tag][0].size() == 0)
{
continue;
}
question[tag][0] = FormatHTMLToString(question[tag][0]);
continue;
}
if (question.contains(tag))
{
question[tag] = FormatHTMLToString(question[tag]);
// Get testcases from given content
if (tag == "content")
{
testCases = GetTestCases(question[tag]);
}
}
}
CreateJSON(&question, testCases);
}
catch (json::parse_error &e)
{
std::cerr << "Parse error: " << e.what() << std::endl;
return;
}
}
// check for <code> tag
std::string FormatHTMLToString(const std::string &response)
{
int i = 0;
std::string result = "";
while (i < response.length())
{
// check for HTML elements
if (response[i] == '<')
{
while (response[i] != '>')
{
i++;
}
i++;
continue;
}
// check for < (<) , > (>);
if (i < response.length() - 4 && (response.substr(i, 4) == "<" || response.substr(i, 4) == ">"))
{
std::string expression = response.substr(i, 4);
if (expression == "<")
{
result += "<";
}
else if (expression == ">")
{
result += ">";
}
i += 4;
continue;
}
// check for & (&)
if (i < response.length() - 5 && (response.substr(i, 5) == "&"))
{
result += "&";
i += 5;
continue;
}
// check for 's
if (i < response.length() - 6 && response.substr(i, 6) == "'s")
{
i += 6;
continue;
}
// check for tags
if (i < response.length() - 6 && response.substr(i, 6) == " ")
{
i += 6;
continue;
}
// check for multiple whitespace characters
// want to keep 1 where there are multiple
if (response[i] == '\n')
{
result += "\n";
while (i + 1 < response.length() && response[i + 1] == '\n')
{
i++;
}
i++;
continue;
}
if (response[i] == '\t')
{
while (i + 1 < response.length() && response[i + 1] == '\t')
{
i++;
}
i++;
continue;
}
result += (response[i]);
i++;
}
return result;
}
/**
* Basic test cases given by leetcode are given in a string of the form. Example case & output.
* Should always be at least 2 test cases given.
* @returns array of oxpected outputs for the test cases.
*/
TestCaseResponse GetTestCases(const std::string &content)
{
TestCaseResponse tests;
int i = 0;
while (i < content.length())
{
if (i < content.length() - 7 && content.substr(i, 7) == "Example")
{
i += 7;
while (i < content.length())
{
if (i <= content.length() - 6 && content.substr(i, 6) == "Input:")
{
i += 6;
std::string input = "";
std::string paramName = "";
std::string paramRes = "";
int j = -1;
while (i < content.length() - 7 && content.substr(i, 7) != "\nOutput")
{
// check if new param is being searched
if (i < content.length() - 1 && (content[i] == ',' && content[i + 1] == ' '))
{
tests.testCaseParams.push_back({paramName, paramRes});
paramName = "";
paramRes = "";
j = -1;
i++;
continue;
}
// now looking for paramResult so set j (flag for where = is)
if (content[i] == '=')
{
j = i;
i++;
continue;
}
if (j == -1 && content[i] != ' ')
{
paramName += content[i];
}
else if (j != -1 && content[i] != ' ')
{
paramRes += content[i];
}
i++;
}
if (paramName.length() != 0 && paramRes.length() != 0)
{
tests.testCaseParams.push_back({paramName, paramRes});
}
// std::cout << paramName << " " << paramRes << std::endl;
}
if (i <= content.length() - 6 && content.substr(i, 6) == "Output")
{
i += 6;
std::string testCase = "";
while (i < content.length() && content[i] != '\n')
{
if (content[i] != ' ' && content[i] != ':')
{
testCase += content[i];
}
i++;
}
tests.testCases.push_back(testCase);
break;
}
i++;
}
}
else
{
i++;
}
}
return tests;
}
void CreateJSON(json *response, const TestCaseResponse &tests)
{
// filter out invalid characters from title
std::string title = (*response)["title"];
const std::string invalid_chars = "\\/:*?\"<>|";
for (char c : invalid_chars)
{
std::replace(title.begin(), title.end(), c, '_');
}
std::string jsonName = "../../../Questions/" + title + ".txt";
std::ofstream outputJSON;
outputJSON.open(jsonName);
// should have to create the file so always should open
if (!outputJSON.is_open())
{
std::cerr << "Error creating output file for JSON response" << std::endl;
return;
}
outputJSON << "{\n";
// iterates through json response inserting key and value as pair into output file
for (auto it = (*response).begin(); it != (*response).end(); ++it)
{
outputJSON << "\"" << it.key() << "\"" << ": " << it.value() << ',' << "\n";
}
// handle situation where testCases might not generate
// Insert testcases
outputJSON << "\"testCases\"" << ": [" << "\n";
int j = 0;
int size = tests.testCases.size();
for (int i = 0; i < size; i++)
{
// start inserting new object into array inside json file
outputJSON << "{\n";
std::string expectedResult = tests.testCases[i]; // testcase expected outputs
outputJSON << "\"expectedResult\": " << "\"" << expectedResult << "\",\n";
int numParams = tests.testCaseParams.size() / tests.testCases.size();
for (int x = 0; x < numParams; x++)
{
std::pair<std::string, std::string> fixedParam = tests.testCaseParams[j++];
if (x == numParams - 1)
{
outputJSON << "\"" << fixedParam.first << "\": " << "\"" << fixedParam.second << "\"\n";
}
else
{
outputJSON << "\"" << fixedParam.first << "\": " << "\"" << fixedParam.second << "\",\n";
}
}
// if i is at the end then we need to close off the obj
if (i == size - 1)
{
outputJSON << "}\n";
}
else
{
outputJSON << "},\n";
}
}
outputJSON << "]\n";
outputJSON << "}";
outputJSON.close();
}
/**
* params are taken from the json as a string containing 'paramName'='param'
* This function splits the paramName and param seperately to label them in the output JSON easier.
* (the problem function calls explicility used by the users will contain the same paramNames so makes using them easier as well)
*/
std::pair<std::string, std::string> GetParamName(const std::string ¶m)
{
std::string paramName = "";
std::string paramResult = "";
bool nameParsed = false;
for (int i = 0; i < param.length(); i++)
{
if (param[i] == '=')
{
nameParsed = true;
continue;
}
if (param[i] != ' ' && !nameParsed)
{
paramName += param[i];
}
else if (param[i] != ' ' && nameParsed)
{
paramResult += param[i];
}
}
return {paramName, paramResult};
}