diff --git a/src/system/ytparser.cpp b/src/system/ytparser.cpp index 7e1b87550..db7e17d42 100644 --- a/src/system/ytparser.cpp +++ b/src/system/ytparser.cpp @@ -96,9 +96,9 @@ size_t cYTFeedParser::CurlWriteToString(void *ptr, size_t size, size_t nmemb, vo return size*nmemb; } -bool cYTFeedParser::getUrl(std::string &url, std::string &answer) +bool cYTFeedParser::getUrl(std::string &url, std::string &answer, CURL *_curl_handle) { - CURL * curl_handle = curl_easy_init(); + CURL * curl_handle = _curl_handle ? _curl_handle : curl_easy_init(); curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &cYTFeedParser::CurlWriteToString); curl_easy_setopt(curl_handle, CURLOPT_FILE, (void *)&answer); @@ -112,7 +112,8 @@ bool cYTFeedParser::getUrl(std::string &url, std::string &answer) printf("try to get [%s] ...\n", url.c_str()); CURLcode httpres = curl_easy_perform(curl_handle); - curl_easy_cleanup(curl_handle); + if (!_curl_handle) + curl_easy_cleanup(curl_handle); printf("http: res %d size %d\n", httpres, answer.size()); @@ -123,14 +124,14 @@ bool cYTFeedParser::getUrl(std::string &url, std::string &answer) return true; } -bool cYTFeedParser::DownloadUrl(std::string &url, std::string &file) +bool cYTFeedParser::DownloadUrl(std::string &url, std::string &file, CURL *_curl_handle) { FILE * fp = fopen(file.c_str(), "wb"); if (fp == NULL) { perror(file.c_str()); return false; } - CURL * curl_handle = curl_easy_init(); + CURL * curl_handle = _curl_handle ? _curl_handle : curl_easy_init(); curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl_handle, CURLOPT_FILE, fp); curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1); @@ -145,7 +146,8 @@ bool cYTFeedParser::DownloadUrl(std::string &url, std::string &file) double dsize; curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &dsize); - curl_easy_cleanup(curl_handle); + if (!_curl_handle) + curl_easy_cleanup(curl_handle); fclose(fp); printf("http: res %d size %f.\n", httpres, dsize); @@ -537,6 +539,7 @@ bool cYTFeedParser::ParseVideoInfo(cYTVideoInfo &vinfo) estr.push_back("&el=vevo"); estr.push_back("&el=detailpage"); + CURL *curl_handle = curl_easy_init(); for (unsigned i = 0; i < estr.size(); i++) { std::string vurl = "http://www.youtube.com/get_video_info?video_id="; vurl += vinfo.id; @@ -544,12 +547,13 @@ bool cYTFeedParser::ParseVideoInfo(cYTVideoInfo &vinfo) vurl += "&ps=default&eurl=&gl=US&hl=en"; printf("cYTFeedParser::ParseVideoInfo: get [%s]\n", vurl.c_str()); std::string answer; - if (!getUrl(vurl, answer)) + if (!getUrl(vurl, answer, curl_handle)) continue; ret = decodeVideoInfo(answer, vinfo); if (ret) break; } + curl_easy_cleanup(curl_handle); return ret; } @@ -560,6 +564,7 @@ bool cYTFeedParser::DownloadThumbnails() perror(thumbnail_dir.c_str()); //return ret; } + CURL *curl_handle = curl_easy_init(); for (unsigned i = 0; i < videos.size(); i++) { if (!videos[i].thumbnail.empty()) { std::string fname = thumbnail_dir; @@ -568,12 +573,13 @@ bool cYTFeedParser::DownloadThumbnails() fname += ".jpg"; bool found = !access(fname.c_str(), F_OK); if (!found) - found = DownloadUrl(videos[i].thumbnail, fname); + found = DownloadUrl(videos[i].thumbnail, fname, curl_handle); if (found) videos[i].tfile = fname; ret |= found; } } + curl_easy_cleanup(curl_handle); return ret; } diff --git a/src/system/ytparser.h b/src/system/ytparser.h index 8978f7ef0..efb1fa491 100644 --- a/src/system/ytparser.h +++ b/src/system/ytparser.h @@ -93,8 +93,8 @@ class cYTFeedParser static void splitString(std::string &str, std::string delim, std::vector &strlist, int start = 0); static void splitString(std::string &str, std::string delim, std::map &strmap, int start = 0); static bool saveToFile(const char * name, std::string str); - bool getUrl(std::string &url, std::string &answer); - bool DownloadUrl(std::string &url, std::string &file); + bool getUrl(std::string &url, std::string &answer, CURL *curl_handle = NULL); + bool DownloadUrl(std::string &url, std::string &file, CURL *curl_handle = NULL); bool parseFeedXml(std::string &answer); bool decodeVideoInfo(std::string &answer, cYTVideoInfo &vinfo); bool supportedFormat(int fmt);