Skip to content

Commit

Permalink
More Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
jchallenger1 committed Jul 3, 2017
1 parent 31a9fa4 commit 9582a00
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 56 deletions.
9 changes: 1 addition & 8 deletions Project3/4chanDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,7 @@ void ChanDownloader::getAllImages(string& url) {
appendJsonString(url);

string pure_chan_json;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &pure_chan_json);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
curl_slist* header_list = nullptr;
header_list = curl_slist_append(header_list, "User-Agent: 0"); //4chan will otherwise not allow access.
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

initCurlSetup(url, pure_chan_json);
auto response = curl_easy_perform(curl);
curl_easy_reset(curl);

Expand Down
42 changes: 21 additions & 21 deletions Project3/Download.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ Downloader::~Downloader() {
curl_global_cleanup();
}


void Downloader::initCurlSetup(const string & url, const string& buffer) noexcept {

curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);

curl_slist* header_list = nullptr;
header_list = curl_slist_append(header_list, "User-Agent: 0"); //4chan will otherwise not allow access.
header_list = curl_slist_append(header_list, options.imgur_auth.c_str());

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

}

inline void Downloader::createFolderPath(string& url) const {
int remove_chars = 0;
for (auto cbegin = url.crbegin(); cbegin != url.crend(); cbegin++) {
Expand Down Expand Up @@ -112,14 +128,7 @@ vector<string> Downloader::getPureImgUrl(vector<std::pair<string, string>>& mapp
std::reverse(query_string.begin(), query_string.end());
string request = "https://api.imgur.com/3/album/" + query_string;


curl_easy_setopt(curl, CURLOPT_URL, request.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json_data);
curl_slist* header_list = nullptr;
header_list = curl_slist_append(header_list, options.imgur_auth.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error);
initCurlSetup(request, json_data);
auto response = curl_easy_perform(curl);
curl_easy_reset(curl);
if (response == CURLE_OK) {
Expand Down Expand Up @@ -153,13 +162,7 @@ vector<string> Downloader::getPureImgUrl(vector<std::pair<string, string>>& mapp
}
string request = "https://api.imgur.com/3/image/" + query_string;

curl_easy_setopt(curl, CURLOPT_URL, request.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json_data);
curl_slist* header_list = nullptr;
header_list = curl_slist_append(header_list, options.imgur_auth.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error);
initCurlSetup(request, json_data);
auto response = curl_easy_perform(curl);
curl_easy_reset(curl);
if (response == CURLE_OK) {
Expand Down Expand Up @@ -187,10 +190,7 @@ vector<string> Downloader::getPureImgUrl(vector<std::pair<string, string>>& mapp
std::reverse(query.begin(), query.end());
string request("https://api.gfycat.com/v1test/gfycats/" + query);

curl_easy_setopt(curl, CURLOPT_URL, request.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json_data);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, &error);
initCurlSetup(request, json_data);
auto response = curl_easy_perform(curl);
curl_easy_reset(curl);
if (response == CURLE_OK) {
Expand Down Expand Up @@ -259,9 +259,9 @@ void Downloader::download() {
std::experimental::filesystem::path image_path(options.current_path + img_name);
bool img_exists = std::experimental::filesystem::exists(image_path);

if (!(options.duplicate_file == SKIP && img_exists)) {
if (!(options.duplicate_file == File::SKIP && img_exists)) {
string full_path;
if (options.duplicate_file == CREATENEW && img_exists) {
if (options.duplicate_file == File::CREATENEW && img_exists) {
// adds a number to indicate the nth copy; sdp.jpg(2).
int new_duplicate = 2;
auto dot = std::find(download_urls.second.cbegin(), download_urls.second.cend(), '.');
Expand Down
9 changes: 6 additions & 3 deletions Project3/Download.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

using std::string; using std::vector; using json = nlohmann::json;

enum DuplicateFile {
enum class File {
SKIP,OVERWRITE,CREATENEW };

struct Options {
Expand All @@ -17,7 +17,7 @@ struct Options {
bool all_gallery; //imgur often has a gallery that can consist of 200+ imgs of possibly the same thing, option to turn this off.
string imgur_auth;
string current_path;//path where the application is located
int duplicate_file; // if the file already exists, it tells us what we should do with it.
File duplicate_file; // if the file already exists, it tells us what we should do with it.
string folder_name; //optional for the user to provide a name for the folder
};

Expand All @@ -40,6 +40,7 @@ class Downloader {
bool all = false; // checks if the downloader should download all photos on imgur, or only one.

/* MEMBER FUNCTIONS */
void Downloader::initCurlSetup(const string & url, const string& buffer) noexcept;
void createFolderPath(string&) const; //creates the string where all photos,gifs,vids are inputted into a folder named by the function.
static bool removeNonSupported(const string&);//some posts will have images and post we cannot support, we must get rid of them.
virtual vector<std::pair<string, string>> mapUrls(vector<string>&);
Expand Down Expand Up @@ -91,7 +92,7 @@ class TumblrDownloader : public Downloader {
virtual void getAllImages(string&) override;
virtual void websiteOptions(Options&) override;
private:
string tumblr_auth = "0XpX1kLQeH3EobbeeMXgCcJ6ThcAZ1oDUeoiiC9GzUKEvOBpY6";
string tumblr_auth = "";
string pure_url; //the base url without any extensions, 'prefixes' or 'suffixes'.
int min_size = 0;
virtual void getUrlsFromJson(const string&, vector<string>&) override;
Expand Down Expand Up @@ -121,3 +122,5 @@ class ImgurDownloader : public Downloader {


#endif // !DOWNLOADER


5 changes: 4 additions & 1 deletion Project3/Functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ bool runMainOptions(Options& opt) {

cout << "These options is what happens when a duplicate file occurs,\nType 1 to skip it, 2 to overwrite it, 3 to create a new one. " << endl;
int dup = check<int>("Invalid input,(1) to skip,(2) to overwrite,(3) to create new", "Only input 1 2 or 3.", [](const int& i) {return i >= 1 && i <= 3; });
opt.duplicate_file = dup == 1 ? SKIP : dup == 2 ? OVERWRITE : CREATENEW;
opt.duplicate_file = dup == 1 ? File::SKIP : dup == 2 ? File::OVERWRITE : File::CREATENEW;

cout << "Enter (y) if you want to enter a path to a folder to download images, enter (n) if you want us to create a new folder for you." << endl;
char path = check<char>("Invalid input, (y) to enter path, (n) for us making a new folder", "Only enter (y)es or (n)o", yesOrNo);
Expand Down Expand Up @@ -75,6 +75,9 @@ bool runMainOptions(Options& opt) {
} // !if
else {
create_new = true;
cout << "Do you want to enter a name for the folder?(y/n)" << endl;
opt.folder_name = check<char>("Invalid input, only enter characters", "Yes or No.", yesOrNo);

}
return create_new;
}
Expand Down
9 changes: 1 addition & 8 deletions Project3/ImgurDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,7 @@ void ImgurDownloader::getAllImages(string& raw_url) {
if (validate(raw_url)) {
string url("https://api.imgur.com/3/album/" + this->query_string);
string json_buffer;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json_buffer);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);

curl_slist* header_list = nullptr;
header_list = curl_slist_append(header_list, options.imgur_auth.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
initCurlSetup(url, json_buffer);
auto response = curl_easy_perform(curl);
curl_easy_reset(curl);

Expand Down
7 changes: 3 additions & 4 deletions Project3/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ using std::string; using std::endl; using std::cout;

int main(int argc, char ** argv) {
#if defined(WIN32) || defined(_WIN32) ||defined(__WIN32) || defined(_WIN64)
//string imgur_authorization = "";
//Options dev_options{ 1,3000,"",false, imgur_authorization ,string("") ,CREATENEW,"New folder"};
string your_client_id("b56bd55f8d0bfd4");
string your_client_id("");
string imgur_authorization("Authorization: Client-ID " + your_client_id);
string current_dir(argv[0]);
Options options{ 0,0,"",false,imgur_authorization,"",SKIP,"Your Pics" };
Options options{ 0,0,"",false,imgur_authorization,"",File::SKIP,"Your Pics" };
cout << "Choose a downloader, (reddit),(imgur),(4chan),(tumblr)" << endl;

string type = check<string>("Invalid input, (reddit), (imgur), (4chan), or (tumblr)","", [](const string& s){
return s == "reddit" || s == "imgur" || s == "4chan" || s == "tumblr";
});
Expand Down
4 changes: 1 addition & 3 deletions Project3/RedditDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ void RedditDownloader::getAllImages(string& url) {
appendJsonString(url);
while (page_count > 0 && urls.size() < options.max_files) {
string all_reddit_json;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &all_reddit_json);
initCurlSetup(url, all_reddit_json);
auto response = curl_easy_perform(curl);
curl_easy_reset(curl);

Expand Down
10 changes: 2 additions & 8 deletions Project3/TumblrDownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ void TumblrDownloader::getAllImages(string& raw_url) {
string url = "https://api.tumblr.com/v2/blog/" + pure_url + "/posts/photo?limit=" +
std::to_string(limit) + "&offset=" + std::to_string(offset) + "&api_key=" + tumblr_auth;
string json_buffer;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json_buffer);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
initCurlSetup(url, json_buffer);
auto response = curl_easy_perform(curl);
curl_easy_reset(curl);

Expand Down Expand Up @@ -85,10 +82,7 @@ bool TumblrDownloader::validate(const string& url) {
//send a request to the server, it will reply if the hostname is apart of the tumblr server.
string json_buffer;
string test_url = "https://api.tumblr.com/v2/blog/" + pure_url + "/posts/photo?limit=1&api_key=" + tumblr_auth;
curl_easy_setopt(curl, CURLOPT_URL, test_url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeJsonData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &json_buffer);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, error);
initCurlSetup(test_url, json_buffer);
auto response = curl_easy_perform(curl);
curl_easy_reset(curl);
if (response == CURLE_OK) {
Expand Down

0 comments on commit 9582a00

Please sign in to comment.