ytparser: re-use exiting curl connection for bulk transfers

Origin commit data
------------------
Branch: ni/coolstream
Commit: 0e6848d7d9
Author: martii <m4rtii@gmx.de>
Date: 2013-06-09 (Sun, 09 Jun 2013)


------------------
No further description and justification available within origin commit message!

------------------
This commit was generated by Migit
This commit is contained in:
martii
2013-06-09 08:28:13 +02:00
committed by [CST] Focus
parent 8a92c919f8
commit b6a01d2000
2 changed files with 16 additions and 10 deletions

View File

@@ -96,9 +96,9 @@ size_t cYTFeedParser::CurlWriteToString(void *ptr, size_t size, size_t nmemb, vo
return size*nmemb; 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_URL, url.c_str());
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &cYTFeedParser::CurlWriteToString); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, &cYTFeedParser::CurlWriteToString);
curl_easy_setopt(curl_handle, CURLOPT_FILE, (void *)&answer); 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()); printf("try to get [%s] ...\n", url.c_str());
CURLcode httpres = curl_easy_perform(curl_handle); 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()); 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; 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"); FILE * fp = fopen(file.c_str(), "wb");
if (fp == NULL) { if (fp == NULL) {
perror(file.c_str()); perror(file.c_str());
return false; 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_URL, url.c_str());
curl_easy_setopt(curl_handle, CURLOPT_FILE, fp); curl_easy_setopt(curl_handle, CURLOPT_FILE, fp);
curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1); curl_easy_setopt(curl_handle, CURLOPT_FAILONERROR, 1);
@@ -145,7 +146,8 @@ bool cYTFeedParser::DownloadUrl(std::string &url, std::string &file)
double dsize; double dsize;
curl_easy_getinfo(curl_handle, CURLINFO_SIZE_DOWNLOAD, &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); fclose(fp);
printf("http: res %d size %f.\n", httpres, dsize); 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=vevo");
estr.push_back("&el=detailpage"); estr.push_back("&el=detailpage");
CURL *curl_handle = curl_easy_init();
for (unsigned i = 0; i < estr.size(); i++) { for (unsigned i = 0; i < estr.size(); i++) {
std::string vurl = "http://www.youtube.com/get_video_info?video_id="; std::string vurl = "http://www.youtube.com/get_video_info?video_id=";
vurl += vinfo.id; vurl += vinfo.id;
@@ -544,12 +547,13 @@ bool cYTFeedParser::ParseVideoInfo(cYTVideoInfo &vinfo)
vurl += "&ps=default&eurl=&gl=US&hl=en"; vurl += "&ps=default&eurl=&gl=US&hl=en";
printf("cYTFeedParser::ParseVideoInfo: get [%s]\n", vurl.c_str()); printf("cYTFeedParser::ParseVideoInfo: get [%s]\n", vurl.c_str());
std::string answer; std::string answer;
if (!getUrl(vurl, answer)) if (!getUrl(vurl, answer, curl_handle))
continue; continue;
ret = decodeVideoInfo(answer, vinfo); ret = decodeVideoInfo(answer, vinfo);
if (ret) if (ret)
break; break;
} }
curl_easy_cleanup(curl_handle);
return ret; return ret;
} }
@@ -560,6 +564,7 @@ bool cYTFeedParser::DownloadThumbnails()
perror(thumbnail_dir.c_str()); perror(thumbnail_dir.c_str());
//return ret; //return ret;
} }
CURL *curl_handle = curl_easy_init();
for (unsigned i = 0; i < videos.size(); i++) { for (unsigned i = 0; i < videos.size(); i++) {
if (!videos[i].thumbnail.empty()) { if (!videos[i].thumbnail.empty()) {
std::string fname = thumbnail_dir; std::string fname = thumbnail_dir;
@@ -568,12 +573,13 @@ bool cYTFeedParser::DownloadThumbnails()
fname += ".jpg"; fname += ".jpg";
bool found = !access(fname.c_str(), F_OK); bool found = !access(fname.c_str(), F_OK);
if (!found) if (!found)
found = DownloadUrl(videos[i].thumbnail, fname); found = DownloadUrl(videos[i].thumbnail, fname, curl_handle);
if (found) if (found)
videos[i].tfile = fname; videos[i].tfile = fname;
ret |= found; ret |= found;
} }
} }
curl_easy_cleanup(curl_handle);
return ret; return ret;
} }

View File

@@ -93,8 +93,8 @@ class cYTFeedParser
static void splitString(std::string &str, std::string delim, std::vector<std::string> &strlist, int start = 0); static void splitString(std::string &str, std::string delim, std::vector<std::string> &strlist, int start = 0);
static void splitString(std::string &str, std::string delim, std::map<std::string,std::string> &strmap, int start = 0); static void splitString(std::string &str, std::string delim, std::map<std::string,std::string> &strmap, int start = 0);
static bool saveToFile(const char * name, std::string str); static bool saveToFile(const char * name, std::string str);
bool getUrl(std::string &url, std::string &answer); bool getUrl(std::string &url, std::string &answer, CURL *curl_handle = NULL);
bool DownloadUrl(std::string &url, std::string &file); bool DownloadUrl(std::string &url, std::string &file, CURL *curl_handle = NULL);
bool parseFeedXml(std::string &answer); bool parseFeedXml(std::string &answer);
bool decodeVideoInfo(std::string &answer, cYTVideoInfo &vinfo); bool decodeVideoInfo(std::string &answer, cYTVideoInfo &vinfo);
bool supportedFormat(int fmt); bool supportedFormat(int fmt);