nesca/FileDownloader.cpp

62 lines
1.9 KiB
C++
Raw Normal View History

2015-03-30 14:31:06 +00:00
#include "FileDownloader.h"
#include "mainResources.h"
2015-04-02 10:26:32 +00:00
#include "fstream"
2015-04-02 18:04:19 +00:00
#include <algorithm>
2015-03-30 14:31:06 +00:00
2015-04-02 10:26:32 +00:00
std::string FileDownloader::lastModifiedNeg = "";
std::string FileDownloader::lastModifiedL = "";
std::string FileDownloader::lastModifiedP = "";
std::string FileDownloader::lastModifiedSSH = "";
std::string FileDownloader::lastModifiedWFL = "";
std::string FileDownloader::lastModifiedWFP = "";
2015-04-01 19:23:52 +00:00
2015-04-02 10:26:32 +00:00
std::string getLM(std::string *buffer) {
2015-03-30 14:31:06 +00:00
2015-04-02 10:26:32 +00:00
std::size_t pos1 = buffer->find("Last-Modified:");
2015-03-30 14:31:06 +00:00
if(pos1 == std::string::npos) {
2015-04-02 10:26:32 +00:00
stt->doEmitionFoundData("<font color=\"Pink\">Cannot find Last-Modified.</font>");
return "";
2015-03-30 14:31:06 +00:00
}
int pos2 = buffer->find("\r\n", pos1);
if(pos2 == std::string::npos) {
stt->doEmitionFoundData("<font color=\"Pink\">Weird reply.</font>");
2015-04-02 10:26:32 +00:00
return "";
2015-03-30 14:31:06 +00:00
}
std::string res = buffer->substr(pos1 + 15, pos2 - pos1 - 15);
2015-04-02 10:26:32 +00:00
return res;
2015-03-30 14:31:06 +00:00
}
2015-04-02 10:26:32 +00:00
void checkWeb(const char *fileName, std::string *oldLM) {
2015-04-28 23:27:54 +00:00
std::string buffer;
Connector con;
con.nConnect(std::string("http://nesca.d3w.org/files/" + std::string(fileName)).c_str(), 80, &buffer);
2015-03-30 14:31:06 +00:00
2015-04-02 10:26:32 +00:00
const std::string &lm = getLM(&buffer);
if(lm.size() == 0) return;
if(lm.compare(*oldLM) != 0) {
*oldLM = lm;
std::string res(buffer.substr(buffer.find("\r\n\r\n") + 4).c_str());
2015-04-02 18:16:50 +00:00
res.erase(std::remove(res.begin(), res.end(), '\r'), res.end());
2015-04-02 10:26:32 +00:00
std::ofstream out(fileName);
out << std::string(res);
2015-03-30 14:31:06 +00:00
out.close();
stt->doEmitionFoundData("<font color=\"Pink\">File " + QString(fileName) + " downloaded.</font>");
}
}
void FileDownloader::checkWebFiles() {
2015-04-02 10:26:32 +00:00
while (true) {
2015-08-07 22:37:28 +00:00
checkWeb(NEGATIVE_FN, &lastModifiedNeg);
checkWeb(LOGIN_FN, &lastModifiedL);
checkWeb(PASS_FN, &lastModifiedP);
checkWeb(SSH_PASS_FN, &lastModifiedSSH);
checkWeb(WF_LOGIN_FN, &lastModifiedWFL);
checkWeb(WF_PASS_FN, &lastModifiedWFP);
2015-04-02 10:32:26 +00:00
Sleep(600000);
2015-04-02 10:26:32 +00:00
}
2015-03-30 14:31:06 +00:00
}