Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. #pragma comment(lib, "libcurl_a.lib")
  2. #pragma comment(lib, "wldap32.lib" )
  3. #pragma comment(lib, "crypt32.lib" )
  4. #pragma comment(lib, "Ws2_32.lib")
  5.  
  6. #define CURL_STATICLIB
  7. #include <fstream>
  8. #include <regex>
  9. #include <iostream>
  10. #include <string>
  11. #include <vector>
  12. #include <curl/curl.h>
  13.  
  14.  
  15. namespace
  16. {
  17. static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
  18. {
  19. ((std::string*)userp)->append((char*)contents, size * nmemb);
  20. return size * nmemb;
  21. }
  22.  
  23. std::vector<std::string> Parse()
  24. {
  25. std::ifstream file;
  26. file.open("code.txt", std::ios::in);
  27.  
  28. std::string buffer;
  29. std::vector<std::string> v;
  30. std::regex reg("(https?:\/\/.*sticker.png)");
  31. std::smatch match;
  32. while (std::getline(file, buffer))
  33. {
  34. if (std::regex_search(buffer, match, reg))
  35. {
  36. v.push_back(match[0]);
  37. }
  38. }
  39. file.close();
  40. std::remove("code.txt");
  41. return v;
  42. }
  43.  
  44.  
  45. void downloadPage(const char* url)
  46. {
  47. CURL* curl;
  48. CURLcode res;
  49. std::fstream code;
  50. std::string buffer;
  51.  
  52. curl = curl_easy_init();
  53. code.open("code.txt", std::ios::out | std::ios::in | std::ios::trunc);
  54.  
  55. if (!code.is_open())
  56. {
  57. std::cout << "Nie mozna otworzyc pliku" << std::endl;
  58.  
  59. Sleep(2000);
  60. _exit(-1);
  61. }
  62.  
  63. if (curl)
  64. {
  65. curl_easy_setopt(curl, CURLOPT_URL, url);
  66. curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
  67. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
  68. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
  69. res = curl_easy_perform(curl);
  70. code << buffer;
  71. }
  72. curl_easy_cleanup(curl);
  73. code.close();
  74. }
  75.  
  76. void downloadIMG(std::vector<std::string>& links)
  77. {
  78. for (auto &v : links)
  79. {
  80. std::string temp;
  81. const char* tmp = v.c_str();
  82. CURL* curl;
  83. FILE* img;
  84. CURLcode res;
  85. std::regex reg("([[:digit:]]{2,})");
  86. std::smatch match;
  87. std::regex_search(v, match, reg);
  88. temp = match[0];
  89. temp.append(".png");
  90.  
  91. /*
  92. std::cout << v << " " << temp << std::endl;
  93. Sleep(500);
  94. */
  95.  
  96. fopen_s(&img, temp.c_str(), "wb");
  97. if (!img)
  98. {
  99. std::cout << "Nie mozna zapisac obrazka" << std::endl;
  100. _exit(-1);
  101. }
  102.  
  103.  
  104. curl = curl_easy_init();
  105. if (curl)
  106. {
  107. curl_easy_setopt(curl, CURLOPT_URL, tmp);
  108. curl_easy_setopt(curl, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36");
  109. curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
  110. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
  111. curl_easy_setopt(curl, CURLOPT_WRITEDATA, img);
  112. res = curl_easy_perform(curl);
  113. }
  114. curl_easy_cleanup(curl);
  115. fclose(img);
  116. }
  117. }
  118.  
  119. void Download(const char* url)
  120. {
  121. downloadPage(url);
  122. std::vector<std::string> links = Parse();
  123. downloadIMG(links);
  124. }
  125.  
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement