-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
261 lines (238 loc) · 7.96 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
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
#include <vector>
#include <map>
#include <cstdlib>
#include <tidylib/include/tidy/tidy.h>
#include <tidylib/include/tidy/buffio.h>
#include <libxml2/include/libxml/parser.h>
#include <libxml2/include/libxml/tree.h>
#include <curl/include/curl/curl.h>
#include <libxl/include_cpp/libxl.h>
using namespace libxl;
using namespace std;
int level;
Book *book;
Sheet *sheet;
int parent;
int sno;
string xpath;
map<string, int> content;
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;
}
string Request(string url)
{
CURL *curl;
CURLcode res;
string result;
curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); // Passing the function pointer to LC
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result); // Passing our BufferStruct to LC
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
return result;
}
string CleanHTML(const std::string &html){
// Initialize a Tidy document
TidyDoc tidyDoc = tidyCreate();
TidyBuffer tidyOutputBuffer = {0};
// Configure Tidy
// The flags tell Tidy to output XML and disable showing warnings
bool configSuccess = tidyOptSetBool(tidyDoc, TidyXmlOut, yes)
&& tidyOptSetBool(tidyDoc, TidyQuiet, yes)
&& tidyOptSetBool(tidyDoc, TidyNumEntities, yes)
&& tidyOptSetBool(tidyDoc, TidyShowWarnings, no);
int tidyResponseCode = -1;
// Parse input
if (configSuccess)
tidyResponseCode = tidyParseString(tidyDoc, html.c_str());
// Process HTML
if (tidyResponseCode >= 0)
tidyResponseCode = tidyCleanAndRepair(tidyDoc);
// Output the HTML to our buffer
if (tidyResponseCode >= 0)
tidyResponseCode = tidySaveBuffer(tidyDoc, &tidyOutputBuffer);
// Any errors from Tidy?
if (tidyResponseCode < 0)
throw ("Tidy encountered an error while parsing an HTML response. Tidy response code: " + tidyResponseCode);
// Grab the result from the buffer and then free Tidy's memory
std::string tidyResult = (char*)tidyOutputBuffer.bp;
tidyBufFree(&tidyOutputBuffer);
tidyRelease(tidyDoc);
return tidyResult;
}
bool IsNewline(char c)
{
if(c == '\n')
return true;
return false;
}
bool PrintElementNames(xmlNode *node)
{
map<string, int> xp;
xmlNode *cur_node = NULL;
for(cur_node = node; cur_node; cur_node = cur_node->next)
{
/*if(cur_node->type == XML_ELEMEstring orig_xpath = xpath;
xpath.append("//");NT_NODE)
{
re << "node type: Element, name: " << cur_node->name << endl;
}*/
string orig_xpath = xpath;
xpath.append("//");
string temp = (const char *)cur_node->name;
xpath.append(temp);
if(xp.find(temp) != xp.end())
{
xp[temp]++;
char te[1000];
itoa(xp[temp], te, 10);
xpath.append("[");
xpath.append(te);
xpath.append("]");
}
else
xp[temp] = 1;
if(strcmp((const char *)cur_node->name, "head") != 0)
{
level++;
PrintElementNames(cur_node->children);
level--;
}
if(cur_node->type == 1)
{
if(xmlNodeGetContent(cur_node)[0] != '\0' && strcmp(
(const char *)cur_node->name, "style") != 0 &&
strcmp((const char *)cur_node->name, "script") != 0 &&
strcmp((const char *)cur_node->name, "head") != 0 &&
strcmp((const char *)cur_node->name, "li") != 0 &&
strcmp((const char *)cur_node->name, "div") &&
strcmp((const char *)cur_node->name, "head") != 0 &&
strcmp((const char *)cur_node->name, "ol") != 0 &&
strcmp((const char *)cur_node->name, "center") != 0 &&
strcmp((const char *)cur_node->name, "tr") != 0 &&
strcmp((const char *)cur_node->name, "td") != 0 &&
strcmp((const char *)cur_node->name, "html") != 0 &&
strcmp((const char *)cur_node->name, "font") != 0 &&
strcmp((const char *)cur_node->name, "form") != 0 &&
strcmp((const char *)cur_node->name, "body") != 0 &&
strcmp((const char *)cur_node->name, "ul") != 0)
{
/*re << xmlNodeGetContent(cur_node) << endl;
re << "node type: " << cur_node->type << " name: " << cur_node->name << endl;
re << "children: " << cur_node->children << endl;
re << level << endl;*/
//cout << cur_node->nsDef << endl;
sheet->writeNum(sno + 1, 0, sno);
string cont = (const char *)xmlNodeGetContent(cur_node);
cont.erase(remove_if(cont.begin(), cont.end(), &IsNewline), cont.end());
//remove(cont.begin(), cont.end(), '\a');
if(content.find(cont) != content.end())
{
char te[1000];
itoa(content[cont], te, 10);
content[cont]++;
cont += "_";
cont += te;
}
else
{
content[cont] = 1;
}
sheet->writeStr(sno + 1, 1, cont.c_str());
sheet->writeNum(sno + 1, 3, parent);
sheet->writeStr(sno + 1, 4, xpath.c_str());
sno++;
}
}
xpath = orig_xpath;
}
return true;
}
void ParseXML(const string &xml)
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
doc = xmlReadMemory(xml.c_str(), xml.length(), "noname.xml", NULL, 0);
if(doc == NULL)
{
printf("error: could not parse file\n");
}
root_element = xmlDocGetRootElement(doc);
PrintElementNames(root_element);
xmlFreeDoc(doc);
xmlCleanupParser();
xmlMemoryDump();
}
int main()
{
string file;
cout << "Enter filename of file containing URLs (example: URL.txt): ";
cin >> file;
ifstream in(file.c_str());string orig_xpath = xpath;
xpath.append("//");
book = xlCreateBook();
if(!book)
{
cout << "Error creating XLS!" << endl;
return 1;
}
sheet = book->addSheet("Sheet1");
if(!sheet)
{
cout << "Error creating XLS!" << endl;
return 1;
}
sheet->writeStr(1, 0, "Sno");
sheet->writeStr(1, 1, "Object Name");
sheet->writeStr(1, 2, "Object Type");
sheet->writeStr(1, 3, "Parent");
sheet->writeStr(1, 4, "ObjectPath");
sno = 1;
int orig = 1;
while(!in.eof())
{
content.clear();
xpath = "xpath=";
string url;
in >> url;
cout << "Connecting to " << url << ":" << endl;
string html = Request(url);
cout << "Converting HTML to XML... ";
string result = CleanHTML(html);
parent = sno;
sheet->writeNum(sno + 1, 0, sno);
sheet->writeStr(sno + 1, 1, url.c_str());
sheet->writeStr(sno + 1, 2, "Page");
sheet->writeNum(sno + 1, 3, 0);
sheet->writeStr(sno + 1, 4, "name=");
sno++;
cout << "Parsing XML... " ;
map<string, int> temp;
ParseXML(result);
int items = sno - orig + 1;
orig = sno;
cout << items << " items." << endl;
cout << endl;
}
book->save("result.xls");
book->release();
return 0;
}