Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- prepareForDownload();
- // This is the previous method, it works, but it is slow
- /*string nextUrl = galleryInfo->firstPicURL();
- for (int i = 0; i < galleryInfo->getAmountOfPics(); ++i) {
- Image * image = new Image(nextUrl, this->downloadDir);
- nextUrl = image->getNextURL();
- image->saveToDisk();
- delete(image);
- }*/
- // Apparently, curlpp doesn't implement curl_multi_wait
- // Create a vector that contains a ofstream and a curl_easy_handle
- CURLM* multi = curl_multi_init();
- vector<std::pair<CURL *, std::ofstream* > > handles;
- string nextUrl = galleryInfo->firstPicURL();
- for (int i = 0; i < 5; i++) { // Dis is just a test, 5 for now
- // Create required variables
- CURL* handle = curl_easy_init();
- Image* image = new Image(nextUrl, downloadDir);
- std::ofstream* of = new std::ofstream(downloadDir + image->getFilename(), std::ios::binary);
- // Set requred options...
- curl_easy_setopt(handle, CURLOPT_URL, image->getSourceURL().c_str());
- curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, [=](const char* p, std::size_t size, std::size_t nmemb) {
- of->write(p, size*nmemb);
- return size * nmemb;
- });
- handles.push_back(std::make_pair(handle, of));
- nextUrl = image->getNextURL();
- delete(image);
- }
- // Now push all the handles to the multi handle
- for (auto it = handles.begin(); it != handles.end(); ++it) {
- curl_multi_add_handle(multi, it->first);
- }
- // now start the process
- int stillRunning;
- do {
- int numfds;
- int repeats = 0;
- CURLMcode mc;
- mc = curl_multi_perform(multi, &stillRunning);
- if (mc == CURLM_OK) {
- mc = curl_multi_wait(multi, NULL, 0, 1000, &numfds);
- }
- if (mc != CURLM_OK) {
- std::cerr << "Multi failed, code " << mc;
- }
- if (!numfds) {
- repeats++;
- if (repeats > 1) {
- std::this_thread::sleep_for(std::chrono::milliseconds(100));
- }
- } else {
- repeats = 0;
- }
- } while (stillRunning);
- // Now remove the handles...
- for (auto it = handles.begin(); it != handles.end(); ++it) {
- curl_multi_remove_handle(multi, it->first);
- }
- // Now delet everything
- for (auto it = handles.begin(); it != handles.end(); ++it) {
- delete(it->second);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement