Day 1/100 100 Days of Code

Info Hunter

Info Hunter is a scraping computer application. The user sets up the application by adding the keywords in the URLs they want to search on. When the search is complete, then the results will be saved on a CSV file which can be accessed by pressing the database option.

A screenshot of info hunter's instruction page.

Today, I worked on saving the search settings. This was achieved by saving the inserted data to a CSV file. It also saves the amount of URLs that exist in the settings.

When the confirm button is pressed, the CSV file that contains the settings gets erased and new data is inserted. The same happens in the file where the number of URLs is stored.

First, the settings file is cleared before adding the new data. This is needed because the settings file must be opened in append mode to save everything properly.

// Clearing a file quite easy, just open it without setting
// an input or output mode and clsoe it
// Source: https://stackoverflow.com/questions/25201131/writing-csv-files-from-c
void CSV_Handler::ClearPreviousOptions()
{
    std::ofstream csvFileToClear;
    csvFileToClear.open("../settings/settings.csv");

    if (!csvFileToClear.is_open())
    {
        std::cout << "Failed to save settings." << std::endl;
        std::cout << std::strerror(errno) << std::endl;
        exit(errno);
    }

    csvFileToClear.close();
}

Then, we save the new data:

void CSV_Handler::WriteSavedSearchOptions(std::string keyword, std::string& url) {
    std::ofstream csvfile;
    csvfile.open("../settings/settings.csv",
                 std::ios::app);

    if (!csvfile.is_open())
    {
        std::cout << "Failed to open settings file." << std::endl;
        std::cout << std::strerror(errno) << std::endl;
        exit(errno);
    }

    std::string csvData = url;
    csvData.append(", ");
    csvData.append(keyword);
    csvfile << csvData << std::endl;

    csvfile.close();
}

The following function stores the amount of URLs that have been inserted.

void CSV_Handler::WriteNumberOfUrls(int num)
{
    std::ofstream urlAmount;
    urlAmount.open("../settings/url-amount.txt");

    if (!urlAmount.is_open())
    {
        std::cout << "Failed to save settings" << std::endl;
        std::cout << std::strerror(errno) << std::endl;
        exit(errno);
    }

    urlAmount << num << std::endl;
    urlAmount.close();
}

The save options function is executed in a for loop which is also used to count how many URLs are being saved.

    for (int i = 0; i < 5; i++)
    {
        std::string getUrlValue = std::string(urlInput[i]->GetValue());

        if (getUrlValue.empty())
        {
            break;
        }

        urlCounter++;

        handler.WriteSavedSearchOptions(std::string(keywords1[i]->GetValue()),
                                        getUrlValue);
        handler.WriteSavedSearchOptions(std::string(keywords2[i]->GetValue()),
                                        getUrlValue);
        handler.WriteSavedSearchOptions(std::string(keywords3[i]->GetValue()),
                                        getUrlValue);
        handler.WriteSavedSearchOptions(std::string(keywords4[i]->GetValue()),
                                        getUrlValue);
    }
    handler.WriteNumberOfUrls(urlCounter);

A screenshot of ino hunter's search settings