consolidate all repos to one for archive
This commit is contained in:
148
projektna_naloga/compression/main.cpp
Normal file
148
projektna_naloga/compression/main.cpp
Normal file
@@ -0,0 +1,148 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <optional>
|
||||
#include <filesystem>
|
||||
#include <thread>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <cstdio>
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include "compressor.h"
|
||||
#include "stb.h"
|
||||
|
||||
size_t WriteCallback(void *contents, size_t size, size_t nmemb, std::string *buffer)
|
||||
{
|
||||
size_t total_size = size * nmemb;
|
||||
buffer->append((char *)contents, total_size);
|
||||
return total_size;
|
||||
}
|
||||
|
||||
unsigned char *getImg(const char *url, int *x, int *y, int *n, CURL *curl)
|
||||
{
|
||||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||||
|
||||
std::string response_data;
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_data);
|
||||
|
||||
CURLcode res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK)
|
||||
{
|
||||
std::printf("Error: %s, %s\n", curl_easy_strerror(res), url);
|
||||
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return stb_lfm(reinterpret_cast<const unsigned char *>(response_data.c_str()), response_data.size(), x, y, n, 1);
|
||||
}
|
||||
|
||||
bool compress(const char *url, const char *savepath, CURL *curl)
|
||||
{
|
||||
int x, y, n;
|
||||
unsigned char *data = getImg(url, &x, &y, &n, curl);
|
||||
if (data == NULL)
|
||||
{
|
||||
printf("Error loading image!\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::vector<unsigned char>> matrix(x, std::vector<unsigned char>(y));
|
||||
for (int i = 0; i < x; i++)
|
||||
{
|
||||
for (int j = 0; j < y; j++)
|
||||
{
|
||||
matrix[i][j] = data[i * y + j];
|
||||
}
|
||||
}
|
||||
delete[] data;
|
||||
Compressor com;
|
||||
com.compress(matrix);
|
||||
com.writer.buffer.save_to_file(savepath);
|
||||
return true;
|
||||
}
|
||||
|
||||
struct data
|
||||
{
|
||||
int location_id;
|
||||
int camera_id;
|
||||
std::string url;
|
||||
data(int i, int x, std::string u) : location_id(i), camera_id(x), url(u) {}
|
||||
};
|
||||
|
||||
std::vector<data> getData()
|
||||
{
|
||||
std::ifstream file("cameras.csv");
|
||||
std::string line;
|
||||
|
||||
std::vector<data> ret;
|
||||
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
std::stringstream ss(line);
|
||||
std::string s;
|
||||
std::getline(ss, s, ',');
|
||||
int location_id = std::stoi(s);
|
||||
|
||||
std::getline(ss, s, ',');
|
||||
int camera_id = std::stoi(s);
|
||||
std::getline(ss, s, '\n');
|
||||
|
||||
ret.emplace_back(data(location_id, camera_id, s));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
CURL *curl;
|
||||
CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
if (res != CURLE_OK)
|
||||
{
|
||||
std::cerr << "curl_global_init() failed: " << curl_easy_strerror(res) << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
curl = curl_easy_init();
|
||||
if (!curl)
|
||||
{
|
||||
std::cerr << "Failed to initialize cURL." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
char buffer[100] = {0};
|
||||
std::vector<data> vData = getData();
|
||||
|
||||
while (true)
|
||||
{
|
||||
auto curTime = std::chrono::system_clock::now();
|
||||
std::time_t curTime_t = std::chrono::system_clock::to_time_t(curTime);
|
||||
std::tm *cur_tm = std::localtime(&curTime_t);
|
||||
|
||||
std::sprintf(buffer, "%d", cur_tm->tm_year + 1900);
|
||||
std::filesystem::create_directory(buffer);
|
||||
std::sprintf(buffer, "%d/%02d", cur_tm->tm_year + 1900, cur_tm->tm_mon + 1);
|
||||
std::filesystem::create_directory(buffer);
|
||||
std::sprintf(buffer, "%d/%02d/%02d", cur_tm->tm_year + 1900, cur_tm->tm_mon + 1, cur_tm->tm_mday);
|
||||
std::filesystem::create_directory(buffer);
|
||||
std::sprintf(buffer, "%d/%02d/%02d/%02d", cur_tm->tm_year + 1900, cur_tm->tm_mon + 1, cur_tm->tm_mday, cur_tm->tm_hour);
|
||||
std::filesystem::create_directory(buffer);
|
||||
|
||||
for (size_t i = 0; i < vData.size(); i++)
|
||||
{
|
||||
std::sprintf(buffer, "%d/%02d/%02d/%02d/%03d_%d.bin", cur_tm->tm_year + 1900, cur_tm->tm_mon + 1, cur_tm->tm_mday, cur_tm->tm_hour, vData[i].location_id, vData[i].camera_id);
|
||||
std::printf("%d %d %s\n", vData[i].location_id, vData[i].camera_id, vData[i].url.c_str());
|
||||
// std::printf("Store: %s\n", buffer);
|
||||
compress(vData[i].url.c_str(), buffer, curl);
|
||||
}
|
||||
|
||||
std::printf("Done\n");
|
||||
std::this_thread::sleep_for(std::chrono::hours(1));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user