-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
363 lines (290 loc) · 9.77 KB
/
main.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
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
#include <iostream>
#include <string>
#include <sstream>
#include <asio.hpp>
#include <nlohmann/json.hpp>
const std::string ENDPOINT = "/v1/stickers/search";
const std::string API_KEY = "H9Tiu5xjz9nCpPvESXQ5XP07SXlzEyiy";
const std::string SDK_KEY = "eyeNPLntyMGOc7UldUFMM7fTVKFgz8YW";
const std::string HOST = "api.giphy.com";
const std::string HTTP_VERSION = "HTTP/1.1";
std::vector<std::string> convertStringToVector(const std::string& str)
{
std::vector<std::string> words;
std::string word;
std::istringstream iss(str);
// Extract words from the string
while (iss >> std::ws >> word)
{
words.push_back(word);
}
return words;
}
std::vector<std::string> pullStickers(std::string request)
{
std::vector<std::string> urls;
// Define context for connecting
asio::io_context io_context;
asio::ip::tcp::resolver resolver(io_context);
asio::ip::tcp::resolver::results_type endpoints;
try
{
endpoints = resolver.resolve(HOST, "http");
}
catch (const std::exception& e)
{
std::cerr << "\nEndpoint resolution failed: \t" << e.what() << std::endl;
}
asio::ip::tcp::socket socket(io_context);
try
{
asio::connect(socket, endpoints);
}
catch (const std::exception& e)
{
std::cerr << "Endpoint resolution failed: \t" << e.what() << std::endl;
}
try
{
// send request
asio::write(socket, asio::buffer(request));
}
catch (const std::exception& e)
{
std::cerr << "Endpoint resolution failed: \t" << e.what() << std::endl;
}
// receive response
asio::streambuf response_buffer;
try
{
asio::read(socket, response_buffer, asio::transfer_all());
}
catch (const asio::system_error& e)
{
if (e.code() == asio::error::eof)
{
std::cerr << "\nConnection closed by the server.\n" << std::endl;
}
else
{
std::cerr << "Error occurred during read: " << e.what() << std::endl;
}
}
// Convert response data to char*
std::string response_string = asio::buffer_cast<const char*>(response_buffer.data());
// Get the response status code
unsigned int status_code = 0;
if (response_string.substr(0, 9) == "HTTP/1.1 ")
{
status_code = std::stoi(response_string.substr(9, 3));
}
// Print the response status code
std::cout << "\nResponse Status Code: " << status_code << "\n" << std::endl;
// Find the position of the JSON data within the response string (HTTP response always ends with \r\n\r\n)
auto pos = response_string.find("\r\n\r\n");
if (pos != std::string::npos)
{
// Extract the JSON data from the response string
std::string json_data = response_string.substr(pos + 4);
// std::cout << "JSON Data: " << json_data << std::endl; // Print the JSON data for debugging purposes
// Parse the JSON data
try
{
nlohmann::json json_object = nlohmann::json::parse(json_data);
// Extract urls from JSON data
for (const auto& gif : json_object["data"])
{
urls.push_back(gif["images"]["original"]["url"]);
}
}
catch (const nlohmann::json::parse_error& e)
{
std::cerr << "Failed to parse JSON: " << e.what() << std::endl;
}
}
return urls;
}
std::vector<std::vector<std::string>> splitUrlsIntoPages(const std::vector<std::string>& urls, int page_size)
{
std::vector<std::vector<std::string>> pages;
// Evaluate the total number of pages
int total_pages = urls.size() / page_size;
if (urls.size() % page_size != 0)
{
total_pages++;
}
// Split URLs into pages
for (int i = 0; i < total_pages; i++)
{
std::vector<std::string> page;
for (int j = i * page_size; j < (i + 1) * page_size && j < urls.size(); j++)
{
page.push_back(urls[j]);
}
pages.push_back(page);
}
return pages;
}
void nextPage(const std::vector<std::vector<std::string>>& search_result, unsigned int& current_page_index, const std::string& query)
{
if (current_page_index >= search_result.size())
{
std::cout << "\n\tYou have viewed the last page" << std::endl;
}
else
{
std::cout << "\n\tSearch Reasults for \"" << query << "\"\n\tPage " << current_page_index << ":\n" << std::endl;
// Display the data page at the current index
const std::vector<std::string>& current_page = search_result[current_page_index];
for (const std::string& result : current_page)
{
std::cout << result << "\n" << std::endl;
}
// Increment the current page index
current_page_index++;
}
}
int main()
{
std::cout << R"( ====================
GIF SLEUTH
====================
Find and share a world of animated GIFs.)" << std::endl;
std::cout << "\n\t";
std::string query;
// std::string limit = "100";
// std::string offset = "0";
// std::string rating = "g";
// std::string lang = "en";
// std::string bundle = "messaging_non_clips";
// Number of search results per page
int page_size = 5;
std::string command;
std::vector<std::string> search_results;
bool search_in_progress = false;
unsigned int current_page;
std::vector<std::vector<std::string>> pages;
while (true)
{
std::cout << R"(
Command List:
SEARCH <keyword> - Search gifs using specified keyword.
NEXT - Present the next search page.
RANK - Display how many stickers with the same rank in search results.
CANCEL - Cancel ongoing search.
EXIT or QUIT - Close app.)" << std::endl;
std::cout << "\n\t>>>";
std::getline(std::cin, command);
std::transform(command.begin(), command.end(), command.begin(), [](unsigned char c) {
return std::tolower(c);
});
if (command.substr(0, 7) == "search ")
{
try
{
query = convertStringToVector(command)[1];
}
catch (const std::exception& e)
{
std::cerr << "\n\tINVALID COMMAND! \t" << e.what() << std::endl;
}
search_in_progress = false;
current_page = 1;
std::string request;
// Cancel ongoing search if any
if (search_in_progress)
{
std::cout << "\n\tCanceling ongoing search..." << std::endl;
search_results.clear();
search_in_progress = false;
}
request = "GET " + ENDPOINT;
request += "?api_key=" + API_KEY;
request += "&q=" + query;
// request += "&limit=" + limit;
// request += "&offset=" + offset;
// request += "&rating=" + rating;
// request += "&lang=" + lang;
// request += "&bundle=" + bundle;
request += " " + HTTP_VERSION + "\r\n";
request += "Host: " + HOST + "\r\n";
request += "Connection: close\r\n";
request += "\r\n";
// Pull stickers from API Endpoint
// Store the search results in a vector
search_results = pullStickers(request);
pages = splitUrlsIntoPages(search_results, page_size);
nextPage(pages, current_page, query);
search_in_progress = true;
}
else if (command == "next")
{
if (search_in_progress)
{
// Display next pay if there is
if (search_results.empty())
{
std::cout << "\n\tYou have reached the end of the search results." << std::endl;
}
else
{
nextPage(pages, current_page, query);
}
}
else
{
std::cout << "\n\tNo ongoing search. Please perform a search first." << std::endl;
}
}
else if (command == "cancel")
{
if (search_in_progress)
{
std::cout << "Canceling search..." << std::endl;
search_results.clear();
search_in_progress = false;
}
else
{
std::cout << "\n\tNo ongoing search to cancel." << std::endl;
}
}
else if (command == "rank")
{
if(search_in_progress)
{
// Map to store rank counts
std::map<int, int> rank_count;
for(const std::vector<std::string>& page : pages)
{
for(const std::string& url : page)
{
// Extract the rank from the URL
// and increment the count for that rank
int rank = url[13];
rank_count[rank]++;
}
}
// Print the rank counts
for (const auto& [rank, count] : rank_count)
{
std::cout << "\tRank " << static_cast<char>(rank) << ": " << count << " URLs" << std::endl;
}
}
else
{
std::cout << "\n\tNo ongoing search. Please perform a search first." << std::endl;
}
}
else if (command == "exit" || command == "quit")
{
// Exit the program
std::exit(0);
}
else
{
std::cout << "\n\tINVALID COMMAND!" << std::endl;
}
}
return 0;
}