cleaning
This commit is contained in:
parent
5e3638b7d6
commit
553373180f
|
@ -1,122 +0,0 @@
|
|||
#include "Downloader.h"
|
||||
|
||||
#if !defined(WIN32) && !defined(_WIN32) && !defined(__WIN32__) && !defined(__NT__)
|
||||
error_t fopen_s(FILE** f, const char* name, const char* mode)
|
||||
{ // https://stackoverflow.com/questions/1513209/is-there-a-way-to-use-fopen-s-with-gcc-or-at-least-create-a-define-about-it
|
||||
error_t ret = 0;
|
||||
assert(f);
|
||||
*f = fopen(name, mode);
|
||||
if (!*f)
|
||||
{
|
||||
ret = errno;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
using namespace std;
|
||||
|
||||
Downloader::Downloader()
|
||||
{
|
||||
m_fp = nullptr;
|
||||
m_downloadCallback = nullptr;
|
||||
curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
m_curl = curl_easy_init();
|
||||
curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYPEER, 0L);
|
||||
curl_easy_setopt(m_curl, CURLOPT_SSL_VERIFYHOST, 0L);
|
||||
curl_easy_setopt(m_curl, CURLOPT_COOKIEFILE, "");
|
||||
curl_easy_setopt(m_curl, CURLOPT_COOKIEJAR, "");
|
||||
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, this);
|
||||
}
|
||||
|
||||
Downloader::Downloader(std::string uqUrl, std::string outputFile) : Downloader()
|
||||
{
|
||||
setUqUrl(uqUrl);
|
||||
setOutputFile(outputFile);
|
||||
}
|
||||
|
||||
void Downloader::setUqUrl(std::string uqUrl)
|
||||
{
|
||||
m_uqUrl = uqUrl;
|
||||
}
|
||||
|
||||
void Downloader::setOutputFile(std::string outputFile)
|
||||
{
|
||||
m_outputFile = outputFile;
|
||||
}
|
||||
|
||||
void Downloader::setDownloadCallback(void* callback)
|
||||
{
|
||||
if (callback != nullptr)
|
||||
{
|
||||
curl_easy_setopt(m_curl, CURLOPT_NOPROGRESS, false);
|
||||
m_downloadCallback = callback;
|
||||
}
|
||||
}
|
||||
|
||||
Downloader::~Downloader()
|
||||
{
|
||||
if (m_curl)
|
||||
{
|
||||
curl_easy_cleanup(m_curl);
|
||||
curl_global_cleanup();
|
||||
}
|
||||
if (m_fp != nullptr)
|
||||
{
|
||||
fclose(m_fp);
|
||||
}
|
||||
}
|
||||
|
||||
size_t Downloader::writefunc(void* ptr, size_t size, size_t nmemb, std::string* s)
|
||||
{
|
||||
s->append(static_cast<char*>(ptr), size * nmemb);
|
||||
return size * nmemb;
|
||||
}
|
||||
|
||||
void Downloader::download()
|
||||
{
|
||||
if (m_uqUrl == "" || m_outputFile == "")
|
||||
{
|
||||
throw string("Error: please specify URL and output file");
|
||||
}
|
||||
if (!m_curl)
|
||||
{
|
||||
throw string("Error: curl init");
|
||||
}
|
||||
if (uqdCheckAccess(m_outputFile.c_str(), 0) == 0)
|
||||
{
|
||||
throw string("Error: " + m_outputFile + " already exists");
|
||||
}
|
||||
fopen_s(&m_fp, m_outputFile.c_str(), "wb");
|
||||
string strHTML;
|
||||
curl_easy_setopt(m_curl, CURLOPT_URL, m_uqUrl.c_str());
|
||||
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, &Downloader::writefunc);
|
||||
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, &strHTML);
|
||||
m_res = curl_easy_perform(m_curl);
|
||||
if (m_res != CURLE_OK)
|
||||
{
|
||||
throw string("Error: curl_easy_perform() failed: " + string(curl_easy_strerror(m_res)));
|
||||
}
|
||||
size_t posBegVid = strHTML.find("sources: [\"https://");
|
||||
size_t posEndVid = strHTML.find("/v.mp4\"]");
|
||||
if (posBegVid == std::string::npos || posEndVid == std::string::npos)
|
||||
{
|
||||
throw string("Video URL not found");
|
||||
}
|
||||
string videoURL = strHTML.substr(posBegVid + 11, posEndVid - posBegVid - 5);
|
||||
|
||||
if (m_downloadCallback != nullptr)
|
||||
{
|
||||
curl_easy_setopt(m_curl, CURLOPT_XFERINFOFUNCTION, m_downloadCallback);
|
||||
}
|
||||
|
||||
curl_easy_setopt(m_curl, CURLOPT_URL, videoURL.c_str());
|
||||
curl_easy_setopt(m_curl, CURLOPT_REFERER, m_uqUrl.c_str());
|
||||
curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, NULL);
|
||||
curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, m_fp);
|
||||
m_res = curl_easy_perform(m_curl);
|
||||
if (m_res != CURLE_OK)
|
||||
{
|
||||
throw string("Error: curl_easy_perform() failed: " + string(curl_easy_strerror(m_res)));
|
||||
}
|
||||
}
|
|
@ -1,34 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
||||
#include <io.h>
|
||||
#define uqdCheckAccess(pathname, mode) _access(pathname, mode)
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <cassert>
|
||||
#define uqdCheckAccess(pathname, mode) access(pathname, mode)
|
||||
error_t fopen_s(FILE** f, const char* name, const char* mode);
|
||||
#endif
|
||||
#include <curl/curl.h>
|
||||
|
||||
class Downloader
|
||||
{
|
||||
public:
|
||||
Downloader();
|
||||
Downloader(std::string uqUrl, std::string outputFile);
|
||||
~Downloader();
|
||||
void setUqUrl(std::string uqUrl);
|
||||
void setOutputFile(std::string outputFile);
|
||||
void setDownloadCallback(void* callback);
|
||||
void download();
|
||||
|
||||
private:
|
||||
static size_t writefunc(void *curl, size_t size, std::size_t nmemb, std::string *s);
|
||||
std::string m_uqUrl, m_outputFile;
|
||||
CURL* m_curl;
|
||||
CURLcode m_res;
|
||||
FILE* m_fp;
|
||||
void* m_downloadCallback;
|
||||
};
|
|
@ -1,43 +0,0 @@
|
|||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include "Downloader.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int downloadCallback(void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow);
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 3)
|
||||
{
|
||||
cerr << "Usage: " << argv[0] << "https://uqload.com/embed-xxxxxxx.html movie.mp4" << endl;
|
||||
return -1;
|
||||
}
|
||||
string pageInit = string(argv[1]), outputFile=string(argv[2]);
|
||||
Downloader *uqDownloader=new Downloader(pageInit, outputFile);
|
||||
uqDownloader->setDownloadCallback((void*)downloadCallback);
|
||||
try
|
||||
{
|
||||
uqDownloader->download();
|
||||
}
|
||||
catch (const string& e)
|
||||
{
|
||||
cerr << e << endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int downloadCallback(void* p, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
|
||||
{
|
||||
if (dltotal != 0.0)
|
||||
{
|
||||
cout << setprecision(3) << ((double)dlnow / (double)dltotal) * 100 << " %\r" << flush;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
https://uqload.com/embed-h63yfu9dkw1r.html
|
||||
test.mp4
|
||||
*/
|
Loading…
Reference in New Issue