gethttp: now completely removed the need of files
This commit is contained in:
parent
36cfe14015
commit
e569cbb436
7 changed files with 21 additions and 73 deletions
|
@ -1,8 +1,8 @@
|
|||
===============================================================================
|
||||
Language Files Lines Code Comments Blanks
|
||||
===============================================================================
|
||||
C++ 4 368 293 26 49
|
||||
C++ Header 4 71 56 6 9
|
||||
C++ 4 320 253 20 47
|
||||
C++ Header 4 67 54 5 8
|
||||
===============================================================================
|
||||
Total 8 439 349 32 58
|
||||
Total 8 387 307 25 55
|
||||
===============================================================================
|
||||
|
|
|
@ -8,23 +8,14 @@
|
|||
// regex and stuff (collectionid)
|
||||
void Regex(cmd *inputCmd) {
|
||||
if (!inputCmd->collectionid.empty()) {
|
||||
// Input and output file paths
|
||||
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);
|
||||
}
|
||||
std::istringstream inputStream(inputCmd->source);
|
||||
|
||||
// Regex to search for the desired pattern
|
||||
std::regex grepRegex(R"(<div class="workshopItemPreviewHolder )");
|
||||
std::string line;
|
||||
|
||||
// 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)
|
||||
if (std::regex_search(line, grepRegex)) {
|
||||
// sed 's/"><div class=.*//'
|
||||
|
@ -49,15 +40,6 @@ void Regex(cmd *inputCmd) {
|
|||
|
||||
// Step 6: Write "+quit" at the end of the output buffer
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include <curl/curl.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) {
|
||||
// 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;
|
||||
}
|
||||
|
||||
void writeHtmlFile(std::string *woof, std::string *outputfile) {
|
||||
// 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) {
|
||||
void getHttp(cmd *inputCmd, std::string url) {
|
||||
// initialize curl object and curlcode
|
||||
CURL *meow = curl_easy_init();
|
||||
CURLcode res;
|
||||
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
|
||||
curl_easy_setopt(meow, CURLOPT_URL, url.c_str());
|
||||
curl_easy_setopt(meow, CURLOPT_WRITEFUNCTION, appendCurlOutputToString);
|
||||
curl_easy_setopt(meow, CURLOPT_WRITEDATA, &woof);
|
||||
|
||||
// perform the request
|
||||
res = curl_easy_perform(meow);
|
||||
if (res != CURLE_OK) {
|
||||
if (curl_easy_perform(meow) != CURLE_OK) {
|
||||
throw("failed to perform the request\n");
|
||||
}
|
||||
writeHtmlFile(&woof, outputfile);
|
||||
|
||||
inputCmd->source = woof;
|
||||
curl_easy_cleanup(meow);
|
||||
}
|
||||
|
|
|
@ -4,14 +4,9 @@
|
|||
|
||||
// all the var's
|
||||
struct cmd {
|
||||
// legacy
|
||||
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
|
||||
// buffers
|
||||
std::string ids;
|
||||
std::string source;
|
||||
|
||||
// what the args need
|
||||
std::string collectionid;
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#ifndef GETHTTP_H
|
||||
#define GETHTTP_H
|
||||
#include <string>
|
||||
void getHttp(std::string url, std::string *outputfile);
|
||||
#include "Regex.hpp"
|
||||
void getHttp(cmd *inputCmd, std::string url);
|
||||
#endif
|
||||
|
|
25
src/main.cpp
25
src/main.cpp
|
@ -1,38 +1,20 @@
|
|||
#include <cctype>
|
||||
#include <cpptoml.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "includes/getHttp.hpp"
|
||||
#include "includes/Strings.hpp"
|
||||
#include "includes/Regex.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) {
|
||||
// struct cmd
|
||||
cmd inputCmd;
|
||||
|
||||
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};
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
|
@ -78,7 +60,7 @@ int main(int argc, char **argv, char **envp) {
|
|||
|
||||
inputCmd.ab = 0;
|
||||
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) {
|
||||
std::cout << meow;
|
||||
return 1;
|
||||
|
@ -98,7 +80,7 @@ int main(int argc, char **argv, char **envp) {
|
|||
|
||||
inputCmd.ab = 0;
|
||||
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) {
|
||||
std::cout << meow;
|
||||
return 1;
|
||||
|
@ -158,6 +140,7 @@ int main(int argc, char **argv, char **envp) {
|
|||
}
|
||||
}
|
||||
|
||||
// main functions
|
||||
Regex(&inputCmd);
|
||||
slashing(&inputCmd);
|
||||
maincommand(&inputCmd);
|
||||
|
|
|
@ -70,7 +70,7 @@ void maincommand(cmd *inputCmd) {
|
|||
running = false;
|
||||
cursorThread.join();
|
||||
} catch (const std::runtime_error& e) {
|
||||
std::cerr << "\nError: " << e.what() << std::endl;
|
||||
std::cerr << "\nError: " << e.what() << "\n";
|
||||
}
|
||||
|
||||
// mod or collection?
|
||||
|
|
Loading…
Add table
Reference in a new issue