Created
March 3, 2017 18:26
-
-
Save kashimAstro/70714893deae88a609f605f4ec3fd3ed to your computer and use it in GitHub Desktop.
search image by google for popular trainer dlib
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <sstream> | |
#include <stdio.h> | |
#include <regex> | |
#include <fstream> | |
#include <curl/curl.h> | |
void write_file(std::string p, std::string d) | |
{ | |
std::ofstream file; | |
file.open(p.c_str()); | |
file << d; | |
file.close(); | |
} | |
std::string urlencode(const std::string &s) | |
{ | |
static const char lookup[]= "0123456789abcdef"; | |
std::stringstream e; | |
for(int i=0, ix=s.length(); i<ix; i++) | |
{ | |
const char& c = s[i]; | |
if ( (48 <= c && c <= 57) || | |
(65 <= c && c <= 90) || | |
(97 <= c && c <= 122) || | |
(c=='-' || c=='_' || c=='.' || c=='~') | |
) | |
{ | |
e << c; | |
} | |
else | |
{ | |
e << '%'; | |
e << lookup[ (c&0xF0)>>4 ]; | |
e << lookup[ (c&0x0F) ]; | |
} | |
} | |
return e.str(); | |
} | |
std::string replaced(std::string subject, const std::string& search, const std::string& replace) | |
{ | |
size_t pos = 0; | |
while ((pos = subject.find(search, pos)) != std::string::npos) { | |
subject.replace(pos, search.length(), replace); | |
pos += replace.length(); | |
} | |
return subject; | |
} | |
std::vector<std::string> explode(std::string const & s, char delim) | |
{ | |
std::vector<std::string> result; | |
std::istringstream iss(s); | |
for (std::string token; std::getline(iss, token, delim); ) | |
{ | |
result.push_back(std::move(token)); | |
} | |
return result; | |
} | |
static size_t parsing(void *contents, size_t size, size_t nmemb, void *userp) | |
{ | |
((std::string*)userp)->append((char*)contents, size * nmemb); | |
return size * nmemb; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if(argc > 2) | |
{ | |
std::vector<std::string> href; | |
std::vector<std::string> img; | |
curl_global_init(CURL_GLOBAL_DEFAULT); | |
std::string search_query = urlencode( argv[1] ); | |
std::string start = argv[2]; | |
std::string url = "https://www.google.it/search?q="+search_query+"&source=lnms&tbm=isch&start="+start; | |
std::string buff; | |
CURL *curl; | |
CURLcode res; | |
curl = curl_easy_init(); | |
if(curl) | |
{ | |
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | |
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, parsing); | |
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buff); | |
res = curl_easy_perform(curl); | |
curl_easy_cleanup(curl); | |
auto r = replaced(buff, "><", ">\n<"); | |
write_file("debug_all.log", r); | |
auto v = explode(r, '\n'); | |
std::string simg,shref; | |
for(int i = 0; i < v.size(); i++) | |
{ | |
std::size_t found = v[i].find("<a href"); | |
if (found!=std::string::npos) | |
{ | |
shref+=v[i]+"\n"; | |
href.push_back(v[i]); | |
} | |
std::size_t found1 = v[i].find("<img"); | |
if (found1!=std::string::npos) | |
{ | |
simg+=v[i]; | |
img.push_back(v[i]); | |
} | |
} | |
std::cout<< href.size() <<std::endl; | |
std::cout<< img.size() <<std::endl; | |
write_file("href.log", shref); | |
write_file("img.log", simg); | |
} | |
curl_global_cleanup(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment