-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
105 lines (79 loc) · 3.2 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
#include <boost/asio.hpp>
#include <iostream>
#include "ipinfo.hpp"
#include "rapidjson/document.h"
using namespace std;
int main() {
std::string result = pullData();
std::string json;
size_t start = result.find('{'); // Find the start of JSON
size_t end = result.rfind('}'); // Find the end of JSON
if (start != std::string::npos && end != std::string::npos) {
json = result.substr(start, end - start + 1); // Extract JSON
//printf("json data: %s\n", json.c_str());
} else {
printf("Failed to find valid JSON\n");
return 1;
}
//printf("json data: %s\n", json.c_str());
rapidjson::Document document;
document.Parse(json.c_str());
if (document.HasParseError()) {
printf("Failed to parse JSON\n");
return 1;
}
printOutput(document);
return 0;
}
std::string pullData(){
std::string response_string = "";
try {
boost::asio::io_context io_context;
boost::asio::ip::tcp::resolver resolver(io_context);
boost::asio::ip::tcp::socket socket(io_context);
// Resolve the host and port
auto endpoints = resolver.resolve("ipinfo.io", "80");
// Connect to the server
boost::asio::connect(socket, endpoints);
// Send an HTTP GET request
std::string request = "GET /json HTTP/1.1\r\nHost: ipinfo.io\r\n\r\n";
boost::asio::write(socket, boost::asio::buffer(request));
// Read the response
boost::asio::streambuf response;
boost::asio::read_until(socket, response, "}");
// Extract the data from the streambuf,and store it in a string
std::istream response_stream(&response);
std::string response_string((std::istreambuf_iterator<char>(response_stream)), std::istreambuf_iterator<char>());
// Print the response
// cout << &response;
return response_string;
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return response_string;
}
void printOutput(rapidjson::Document &document){
cout << "_____________________________________" << endl;
if (document.HasMember("ip") && document["ip"].IsString()) {
cout << "IP = " << document["ip"].GetString() << endl;
}
if (document.HasMember("city") && document["city"].IsString()) {
cout << "City = " << document["city"].GetString() << endl;
}
if (document.HasMember("region") && document["region"].IsString()) {
cout << "Region = " << document["region"].GetString() << endl;
}
if (document.HasMember("country") && document["country"].IsString()) {
cout << "Country = " << document["country"].GetString() << endl;
}
if (document.HasMember("org") && document["org"].IsString()) {
cout << "Org = " << document["org"].GetString() << endl;
}
if (document.HasMember("loc") && document["loc"].IsString()) {
cout << "Location = " << document["loc"].GetString() << endl;
}
if (document.HasMember("timezone") && document["timezone"].IsString()) {
cout << "Timezone = " << document["timezone"].GetString() << endl;
}
cout << "_____________________________________" << endl;
}