#include "i2psam.h"
#define EEPSITE "identiguy.i2p"
int main(int argc, char * argv[])
{
try
{
std::ofstream file;
bool wfile = false;
if(argc > 1)
{
file.open(argv[1]);
wfile = true;
if(! file.is_open()) throw std::runtime_error("error ofstream 1 - Can not open file!");
}
std::clog << "Establish a tunnel ..." << std::endl;
SAM::StreamSession s("eepstatus-get");
std::clog << "Naming lookup ..." << std::endl;
auto lookup = s.namingLookup(EEPSITE);
if(! lookup.isOk) throw std::runtime_error("Error namingLookup");
std::clog << "Connect to " EEPSITE " ..." << std::endl;
auto connRes = s.connect(lookup.value, false);
if(! connRes.isOk) throw std::runtime_error("Error Connection Result");
auto conn = connRes.value.get();
std::clog << "Write request ..." << std::endl;
conn->write("GET / HTTP/1.1\r\n"
"Host: " EEPSITE "\r\n"
"\r\n");
std::clog << "Read result ..." << std::endl;
std::string tmpfil_fn = tmpnam(NULL);
std::clog << "Temp file: " << tmpfil_fn << std::endl;
std::ofstream tmpfilo(tmpfil_fn);
if(! tmpfilo.is_open()) throw std::runtime_error("error ofstream 2");
auto reply = conn->read();
while(! reply.empty())
{
tmpfilo << reply << "\r\n";
reply = conn->read();
}
tmpfilo.close();
std::clog << "Evaluate result ..." << std::endl;
std::ifstream tmpfili(tmpfil_fn);
if(! tmpfili.is_open()) throw std::runtime_error("error ifstream");
std::regex reg1("a<\\/a> b<\\/a><\\/td>");
bool res;
std::smatch m;
long counter = 0;
std::string html;
while(std::getline(tmpfili, html))
{
res = regex_search(html, m, reg1);
if(res && m.size() > 1)
{
std::cout << m.str(1) << "; " << std::flush;
if(wfile)
file << m.str(1) << " " << m.str(2) << " " << m.str(3) << "\r\n";
counter++;
}
}
std::cout << std::endl << counter << " eepsites" << std::endl;
remove(tmpfil_fn.c_str());
conn->close();
file.close();
tmpfili.close();
}
catch(const std::exception & e)
{
std::cerr << "Error: " << e.what() << std::endl;
exit(EXIT_SUCCESS);
}
return EXIT_SUCCESS;
}
|