-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlineFilesDownload.cpp
406 lines (307 loc) · 11.9 KB
/
OnlineFilesDownload.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
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
//
// Created by akshi on 1/18/2017.
//
#include <openssl/md5.h>
#include <curl/curl.h>
#include <sys/stat.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <set>
#include <unistd.h>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <dirent.h>
#include "OnlineFilesDownload.h"
#include "GlobalSetting.h"
#include "InformationExtractor.h"
using namespace std;
OnlineFilesDownload::OnlineFilesDownload() {
struct stat st = {0};
GlobalSetting * gs = GlobalSetting::get();
_mkdir((gs->getWorkingLocation()+"/pdfs").c_str());
_mkdir((gs->getWorkingLocation()+"/html").c_str());
}
void OnlineFilesDownload::filesAvailable(){
vector<string> websiteLinks = GlobalSetting::get()->getWebsiteLinks();
vector<string>::iterator iter = websiteLinks.begin();
std::set<std::string> list_of_files;
if(!GlobalSetting::get()->skip_request_pdf_list){
while (iter != websiteLinks.end()){
string readBuffer = sendRequest((*iter).c_str());
trimPDFLinks(list_of_files, readBuffer, (*iter).c_str());
cout<<"Pass "<<*iter<<" length of set "<<list_of_files.size()<<endl;
iter++;
}
}else{
DIR *dir;
struct dirent *ent;
std::string downloadLoc = std::string(GlobalSetting::get()->getWorkingLocation()+"/pdfs");
if ((dir = opendir (downloadLoc.c_str())) != NULL) {
while ((ent = readdir (dir)) != NULL) {
std::string file_name (ent->d_name);
string temp_name = (file_name).substr((file_name).find_last_of("\\/")+1);
if(temp_name.find(".pdf") == std::string::npos)
continue;
if(temp_name.substr(temp_name.length()-4, 4).compare(".pdf") == 0){
temp_name = temp_name.substr(0, temp_name.length()-4);
list_of_files.insert(temp_name);
}
}
closedir (dir);
}
}
//Download JDK
//https://gist.githubusercontent.com/hgomez/4697585/raw/5ba0e8d5a726df92ee069aceac917de4e680bfd0/oracledownload >> shell script >> jdk.tar >> extract
//http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-linux-x64.tar.gz >> tar >> extract
string fileName = GlobalSetting::get()->getWorkingLocation();
chdir(fileName.c_str());
if(!GlobalSetting::get()->skip_jdk_download) {
system("wget --no-cookies --no-check-certificate --header \"Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense\" \"http://download.oracle.com/otn-pub/java/jdk/8u112-b15/jdk-8u112-linux-x64.tar.gz\"");
}
if(!GlobalSetting::get()->skip_jdk_extract){
system("tar xf jdk-8u112-linux-x64.tar.gz");
}
if(!GlobalSetting::get()->skip_pdfbox_download) {
// Download http://www-us.apache.org/dist/pdfbox/2.0.4/pdfbox-app-2.0.4.jar
fileName = GlobalSetting::get()->getWorkingLocation();
fileName = fileName + "/pdfbox-app-2.0.4.jar";
char temp[FILENAME_MAX];
strcpy(temp, fileName.c_str());
const char *pdfbox_url = "http://www-us.apache.org/dist/pdfbox/2.0.4/pdfbox-app-2.0.4.jar";
download_file(temp, pdfbox_url);
}
string command_name = "jdk1.8.0_112/bin/java -jar pdfbox-app-2.0.4.jar ";
string command_cmd = "ExtractText ";
string command_options = "-html ";
for(set<string>::iterator iter = list_of_files.begin();
iter != list_of_files.end(); ++iter) {
int returnValue;
string temp_name = (*iter).substr((*iter).find_last_of("\\/")+1);
if(!GlobalSetting::get()->skip_file_download)
temp_name = fileName_encode(temp_name);
string downloadFileName = GlobalSetting::get()->getWorkingLocation()+"/pdfs/"+temp_name+".pdf";
string outputFile = GlobalSetting::get()->getWorkingLocation()+"/html/"+temp_name+".html";
string command_full = command_name + command_cmd + command_options + downloadFileName + " " + outputFile;
if (!GlobalSetting::get()->skip_file_download) {
char temp[FILENAME_MAX];
strcpy(temp, downloadFileName.c_str());
cout << "Downloading " << *iter << endl;
cout << " to " << downloadFileName << endl;
download_file(temp, (*iter).c_str());
}
returnValue = 0;
if (!GlobalSetting::get()->skip_file_convert) {
std::cout<<"Executing cmd : "<<std::endl;
std::cout<<command_full<<std::endl;
returnValue = system(command_full.c_str());
if(returnValue!=0){
std::cout<<"Value of return is "<<returnValue<<std::endl;
if(!GlobalSetting::get()->skip_interactive_mode){
char input;
std::fflush(stderr);
std::cout<<"Press c to continue : ";
std::fflush(stdout);
std::cin>>input;
if(input!='c' || input!='C')
break;
}else{
perror("Handing parsing error and continuing next ... \n");
std::fflush(stderr);
continue;
}
}
}
if(returnValue==0) {
try {
InformationExtractor informationExtractor(outputFile);
informationExtractor.start();
} catch (runtime_error) {
std::string message_err = "Error from parsing file " + *iter + "\n";
perror(message_err.c_str());
if (!GlobalSetting::get()->skip_interactive_mode) {
char input;
std::fflush(stderr);
std::cout << "Press c to continue : ";
std::fflush(stdout);
std::cin >> input;
if (input != 'c' || input != 'C')
break;
} else {
perror("Handing runtime error and continuing next ... \n");
std::fflush(stderr);
continue;
}
}
}
if (exists_test1(downloadFileName)) {
if (remove(downloadFileName.c_str()) != 0)
perror("Error deleting pdf file");
}
if (exists_test1(outputFile)) {
if (remove(outputFile.c_str()) != 0)
perror("Error deleting html file");
}
}
}
inline bool OnlineFilesDownload::exists_test1 (const std::string& name) {
if (FILE *file = fopen(name.c_str(), "r")) {
fclose(file);
return true;
} else {
return false;
}
}
std::string OnlineFilesDownload::sendRequest(const char * site) {
CURL *curl;
CURLcode res;
string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, site);
/* example.com is redirected, so we tell libcurl to follow redirection */
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
/* 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 readBuffer;
}
size_t OnlineFilesDownload::WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
int OnlineFilesDownload::download_file(char outfilename[FILENAME_MAX], const char * url) {
CURL *curl;
FILE *fp;
CURLcode res;
curl = curl_easy_init();
if (curl) {
fp = fopen(outfilename,"wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
fclose(fp);
}
return 0;
}
void OnlineFilesDownload::_mkdir(const char *dir) {
char tmp[256];
char *p = NULL;
size_t len;
snprintf(tmp, sizeof(tmp),"%s",dir);
len = strlen(tmp);
if(tmp[len - 1] == '/')
tmp[len - 1] = 0;
for(p = tmp + 1; *p; p++)
if(*p == '/') {
*p = 0;
mkdir(tmp, S_IRWXU);
*p = '/';
}
mkdir(tmp, S_IRWXU);
}
void OnlineFilesDownload::trimPDFLinks(std::set<std::string> &list_of_files, std::string response, const char *url) {
size_t starting;
vector<string> ret;
size_t start,end;
size_t length;
start = response.find("<table>")+ sizeof("<table>");
end = response.find("</table>",start);
std::string table = response.substr(start, end-start);
length = end-start;
response.clear();
const char * buffer = table.c_str();
size_t breakpoint = 0;
for(size_t i=0; i<length; i++){
char c = buffer[i];
if(c=='h' && buffer[i+1]=='r' && buffer[i+2]=='e' && buffer[i+3]=='f'){
start = table.find('"',i) + 1;
end = table.find('"',start);
string partialName = table.substr(start,end-start);
if(partialName.find("pdf")!=std::string::npos)
list_of_files.insert(getCorrectFileURL(partialName,url));
i = end;
}
}
}
std::string OnlineFilesDownload::getCorrectFileURL(std::string file, const char * url) {
std::string url_home = std::string(url);
std::string web_home = std::string(url);
url_home = url_home.substr(0, url_home.find("ExamResultsmain.htm"));
web_home = web_home.substr(0, web_home.find("/",7));
std::string full_url;
OnlineFilesDownload::replaceAll(file,"&","&");
OnlineFilesDownload::replaceAll(file,"–","-");
if(file.find("http://") != std::string::npos ){
full_url = file;
}else if(file[0]=='/'){
full_url = web_home.append(file);
}else{
full_url = url_home.append(file);
}
// cout<<"adding "<<full_url<<endl;
return url_encode(full_url);
}
std::string OnlineFilesDownload::url_encode(const std::string &value) {
ostringstream escaped;
escaped.fill('0');
escaped << hex;
for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) {
string::value_type c = (*i);
// Keep alphanumeric and other accepted characters intact
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~' || c == ':' || c == '/') {
escaped << c;
continue;
}
if(c == '%'){
escaped << c;
escaped << *(i+1);
escaped << *(i+2);
i += 2;
continue;
}
// Any other characters are percent-encoded
escaped << uppercase;
escaped << '%' << setw(2) << int((unsigned char) c);
escaped << nouppercase;
}
return escaped.str();
}
std::string OnlineFilesDownload::fileName_encode(const std::string &value) {
unsigned char result[MD5_DIGEST_LENGTH];
MD5((unsigned char*)value.c_str(), value.size(), result);
std::ostringstream sout;
sout<<std::hex<<std::setfill('0');
for(long long c: result)
{
sout<<std::setw(2)<<(long long)c;
}
return sout.str();
}
void OnlineFilesDownload::replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}