-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathurls.cpp
More file actions
113 lines (92 loc) · 2.61 KB
/
urls.cpp
File metadata and controls
113 lines (92 loc) · 2.61 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
#include <stdio.h>
#include <stdlib.h>
#include "curl/curl.h"
#include <string>
#include <iostream>
#include <fstream>
#include "json.hpp"
using json = nlohmann::json;
using namespace std;
static std::string readBuffer;
//these are what it keep hold of the data
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
int answer(const char* url)
{
//initiating the curl
CURL *curl;
CURLcode result;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, (url)); //This URL would need to be Malleable
//follow URLS
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
//sets a waiting time before it times out waiting for a response form the website
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
//decodes weird charcters
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip");
//Sends data to a fucntion i can access
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
//actually getting the data
result = curl_easy_perform(curl);
//making sure that the curl is ok and worked right
if(result != CURLE_OK)
{
cout << curl_easy_strerror(result) << endl;
return 1;
}
//making that the website gave info
int httpCode(0);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &httpCode);
curl_easy_cleanup(curl);
//if website worked it will be equal to 200
if(httpCode == 200)
{
//parseing data that we read in
json data = json::parse(readBuffer);
json newdata = data["items"];
data = newdata[0];
if(data["body"] != NULL)
{
newdata = data["body"]; //If this ends here then one should recive the answer back
return newdata;
}
else //answer not found in first index of array checking the nex two
{
int flag = 0;
for(int i = 1; i < 3; i++)
{
//checking second and third arrays for an accepted answer
data = newdata[i];
if(data["body"] != NULL)
{
newdata = data["body"];
return newdata;
}
//Flag is checking only three times for a valid answer if no valid answer then it terminates program
flag++;
}
//seeing if we got a valid answer or not from one of the other indexes
if(flag == 2)
{
cout << "Unable to locate answer" << endl;
return 1;
}
}
}
//This Happens If URL Fails to get info
else
{
cout << "URL FAILED TO LOAD" << endl;
return 1;
}
}
//if we got here then curl failed to iniitlaize
cout << "Request Failed CURL not TRUE" << endl;
return 0;
}