Developing from my previous question, wanted to generalize the behavior so that would download files within a specific data range.
So, modified the code to look like this
#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <ctime>
#include <iostream>
#include <iostream>
#include <stdio.h>
#include <curl/curl.h>
#include <string.h>
#include <windows.h>
size_t write_data(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
void downloadFile(const char* url, const char* fname) {
CURL *curl;
FILE *fp;
CURLcode res;
curl = curl_easy_init();
if (curl) {
fp = fopen(fname, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
}
}
struct mytm : std::tm { // inherit "tm" to extend it a little
mytm(int year, int month, int day) : tm{}, current{} {
tm_year = year - 1900;
tm_mon = month - 1;
tm_mday = day;
tm_hour = 12;
current = mktime(this);
}
void add_days(int days) {
current += days * 60 * 60 * 24;
std::memcpy(this, std::localtime(¤t), sizeof(std::tm));
}
std::string strftime(const char* format) {
std::string retval(128, ' '); // a suitable sized buffer
std::size_t len = std::strftime(&retval[0], retval.size(), format, this);
retval.resize(len);
return retval;
}
std::time_t current;
};
int main(void) {
mytm date(2007, 1, 1);
for (int i = 0; i < 10; ++i) {
Sleep(1000);
std::string url =
date.strftime("http://www.centrodeinformacao.ren.pt/_layouts/CI.GetExcel/SafeGetExcel.aspx?T=CRG&P=%d-%m-%Y&variation=PT");
std::string file_location =
date.strftime("C:/Users/molecoder/Desktop/file_%d_%m_%Y.xls");
// Pass std::string to a function that needs const char* - https://stackoverflow.com/a/347959/5675325
const char * url_final = url.c_str();
const char * file_location_final = file_location.c_str();
downloadFile(url_final, file_location_final);
}
}
While this downloads the a XLS file with the right name and data (so it's requesting correctly the URL)
it does only so for the initial date - the day, month and year are not changing.