gethttp: now completely removed the need of files

This commit is contained in:
Kaley, Fischer 2024-10-22 02:07:34 +02:00
parent 36cfe14015
commit e569cbb436
7 changed files with 21 additions and 73 deletions

View file

@ -1,8 +1,8 @@
=============================================================================== ===============================================================================
Language Files Lines Code Comments Blanks Language Files Lines Code Comments Blanks
=============================================================================== ===============================================================================
C++ 4 368 293 26 49 C++ 4 320 253 20 47
C++ Header 4 71 56 6 9 C++ Header 4 67 54 5 8
=============================================================================== ===============================================================================
Total 8 439 349 32 58 Total 8 387 307 25 55
=============================================================================== ===============================================================================

View file

@ -8,23 +8,14 @@
// regex and stuff (collectionid) // regex and stuff (collectionid)
void Regex(cmd *inputCmd) { void Regex(cmd *inputCmd) {
if (!inputCmd->collectionid.empty()) { if (!inputCmd->collectionid.empty()) {
// Input and output file paths std::istringstream inputStream(inputCmd->source);
std::string inputFilePath = inputCmd->cachesc; // Path to the input file
// Open the input file
std::ifstream inputFile(inputFilePath);
// Check if the input file is open
if (!inputFile.is_open()) {
throw std::runtime_error("Unable to open file: " + inputFilePath);
}
// Regex to search for the desired pattern // Regex to search for the desired pattern
std::regex grepRegex(R"(<div class="workshopItemPreviewHolder )"); std::regex grepRegex(R"(<div class="workshopItemPreviewHolder )");
std::string line; std::string line;
// Process each line from the input file // Process each line from the input file
while (std::getline(inputFile, line)) { while (std::getline(inputStream, line)) {
// grep-like behavior (only process lines containing the pattern with two spaces) // grep-like behavior (only process lines containing the pattern with two spaces)
if (std::regex_search(line, grepRegex)) { if (std::regex_search(line, grepRegex)) {
// sed 's/"><div class=.*//' // sed 's/"><div class=.*//'
@ -49,15 +40,6 @@ void Regex(cmd *inputCmd) {
// Step 6: Write "+quit" at the end of the output buffer // Step 6: Write "+quit" at the end of the output buffer
inputCmd->ids += "+quit\n"; inputCmd->ids += "+quit\n";
// Store the output back in the cmd structure (or handle it as needed)
// inputCmd->ids = outputBuffer.str(); // Store output in cacheid
// Close the input file
inputFile.close();
// Optionally print the output for verification
// std::cout << inputCmd->ids; // Print output buffer content
} }
} }

View file

@ -1,6 +1,6 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <curl/easy.h> #include <curl/easy.h>
#include <fstream> #include "includes/Regex.hpp"
size_t appendCurlOutputToString(void *ptr, size_t size, size_t nmemb, std::string *woof) { size_t appendCurlOutputToString(void *ptr, size_t size, size_t nmemb, std::string *woof) {
// append the output from curl to a string // append the output from curl to a string
@ -8,35 +8,22 @@ size_t appendCurlOutputToString(void *ptr, size_t size, size_t nmemb, std::strin
return size * nmemb; return size * nmemb;
} }
void writeHtmlFile(std::string *woof, std::string *outputfile) { void getHttp(cmd *inputCmd, std::string url) {
// check for nullptrs
if (!woof || !outputfile) {
throw("string is null\n");
}
// open file and write to it
std::ofstream meow{*outputfile};
meow << *woof;
}
void getHttp(std::string url, std::string *outputfile) {
// initialize curl object and curlcode // initialize curl object and curlcode
CURL *meow = curl_easy_init(); CURL *meow = curl_easy_init();
CURLcode res; CURLcode res;
std::string woof; std::string woof;
// check for nullptr and if nullptr throw an exception
if (!outputfile) {
curl_easy_cleanup(meow);
throw("outputfile is null\n");
}
// set the options // set the options
curl_easy_setopt(meow, CURLOPT_URL, url.c_str()); curl_easy_setopt(meow, CURLOPT_URL, url.c_str());
curl_easy_setopt(meow, CURLOPT_WRITEFUNCTION, appendCurlOutputToString); curl_easy_setopt(meow, CURLOPT_WRITEFUNCTION, appendCurlOutputToString);
curl_easy_setopt(meow, CURLOPT_WRITEDATA, &woof); curl_easy_setopt(meow, CURLOPT_WRITEDATA, &woof);
// perform the request // perform the request
res = curl_easy_perform(meow); if (curl_easy_perform(meow) != CURLE_OK) {
if (res != CURLE_OK) {
throw("failed to perform the request\n"); throw("failed to perform the request\n");
} }
writeHtmlFile(&woof, outputfile);
inputCmd->source = woof;
curl_easy_cleanup(meow); curl_easy_cleanup(meow);
} }

View file

@ -4,14 +4,9 @@
// all the var's // all the var's
struct cmd { struct cmd {
// legacy // buffers
const char *userHome = getenv("HOME");
std::string usercache = std::string(userHome) + "/.cache/";
std::string cacheid = std::string(usercache) + "ids.txt";
std::string cachesc = std::string(usercache) + "sources.html";
// unsure
std::string ids; std::string ids;
std::string source;
// what the args need // what the args need
std::string collectionid; std::string collectionid;

View file

@ -1,5 +1,6 @@
#ifndef GETHTTP_H #ifndef GETHTTP_H
#define GETHTTP_H #define GETHTTP_H
#include <string> #include <string>
void getHttp(std::string url, std::string *outputfile); #include "Regex.hpp"
void getHttp(cmd *inputCmd, std::string url);
#endif #endif

View file

@ -1,38 +1,20 @@
#include <cctype> #include <cctype>
#include <cpptoml.h>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <filesystem> #include <filesystem>
#include <fstream>
#include <iostream> #include <iostream>
#include <ostream>
#include <string> #include <string>
#include <vector>
#include "includes/getHttp.hpp" #include "includes/getHttp.hpp"
#include "includes/Strings.hpp" #include "includes/Strings.hpp"
#include "includes/Regex.hpp" #include "includes/Regex.hpp"
#include "includes/maincommand.hpp" #include "includes/maincommand.hpp"
std::string woof(std::ifstream &meow) {
std::ostringstream nya;
nya << meow.rdbuf();
return nya.str();
}
int main(int argc, char **argv, char **envp) { int main(int argc, char **argv, char **envp) {
// struct cmd // struct cmd
cmd inputCmd; cmd inputCmd;
inputCmd.dir = std::filesystem::current_path(); inputCmd.dir = std::filesystem::current_path();
// Removes cache
if (std::filesystem::exists(inputCmd.cacheid) &&
std::filesystem::is_directory(inputCmd.cacheid)) {
std::filesystem::remove(inputCmd.cacheid);
std::filesystem::remove(inputCmd.cachesc);
} else {
std::filesystem::remove(inputCmd.cacheid);
std::filesystem::remove(inputCmd.cachesc);
}
std::vector<std::string> ARGS{argv, argv + argc}; std::vector<std::string> ARGS{argv, argv + argc};
for (int i = 0; i < argc; ++i) { for (int i = 0; i < argc; ++i) {
@ -78,7 +60,7 @@ int main(int argc, char **argv, char **envp) {
inputCmd.ab = 0; inputCmd.ab = 0;
try { try {
getHttp(std::string{"https://steamcommunity.com/sharedfiles/filedetails/?id=" + inputCmd.collectionid}, &inputCmd.cachesc); getHttp(&inputCmd, std::string{"https://steamcommunity.com/sharedfiles/filedetails/?id=" + inputCmd.collectionid});
} catch (std::string &meow) { } catch (std::string &meow) {
std::cout << meow; std::cout << meow;
return 1; return 1;
@ -98,7 +80,7 @@ int main(int argc, char **argv, char **envp) {
inputCmd.ab = 0; inputCmd.ab = 0;
try { try {
getHttp(std::string{"https://steamcommunity.com/sharedfiles/filedetails/?id=" + inputCmd.collectionid}, &inputCmd.cachesc); getHttp(&inputCmd, std::string{"https://steamcommunity.com/sharedfiles/filedetails/?id=" + inputCmd.collectionid});
} catch (std::string &meow) { } catch (std::string &meow) {
std::cout << meow; std::cout << meow;
return 1; return 1;
@ -158,6 +140,7 @@ int main(int argc, char **argv, char **envp) {
} }
} }
// main functions
Regex(&inputCmd); Regex(&inputCmd);
slashing(&inputCmd); slashing(&inputCmd);
maincommand(&inputCmd); maincommand(&inputCmd);

View file

@ -70,7 +70,7 @@ void maincommand(cmd *inputCmd) {
running = false; running = false;
cursorThread.join(); cursorThread.join();
} catch (const std::runtime_error& e) { } catch (const std::runtime_error& e) {
std::cerr << "\nError: " << e.what() << std::endl; std::cerr << "\nError: " << e.what() << "\n";
} }
// mod or collection? // mod or collection?