Day 13/100 100 Days of Code

Info Hunter

Today, I created 2 event functions. The first one is the StartOperations() function which is executed when the user presses the start button.

// Start Scraping button
void MainFrame::StartOperation(wxMouseEvent &event)
{
    CSV_Handler handler;
    Scraper::isCanceled = false;

    handler.ReadSettings();

//    Separate urls and keywords
    for (const std::string &line : handler.csvLines)
    {
        int index = (int)line.find(",");

        getSettingsUrl.push_back(line.substr(0, index));
        getSettingsKeywords.push_back(line.substr(index + 1));
    }

    std::vector<std::string> getUrls;
    std::vector<int> urlCounterHolder;
    int counter = 0;

//    count how many keywords each url has
    for (const auto &url : getSettingsUrl)
    {
        if (urlCounterHolder.empty())
        {
            int count = (int)std::count(getSettingsUrl.begin(), getSettingsUrl.end(),
                                        url);
            urlCounterHolder.push_back((count));
            getUrls.push_back(url);
        }

        if (std::find(getUrls.begin(), getUrls.end(), url) != std::end(getUrls))
        {
            counter++;
            continue;
        }
        else
        {
            urlCounterHolder.push_back(counter);
            counter = 0;
            getUrls.push_back(url);
            counter++;
        }
    }

    getSettingsUrl.clear();
    counter = 0;

    for (int amount : urlCounterHolder)
    {

//      It is not possible to pass by reference when using threads
//      More information here:
//      https://www.reddit.com/r/cpp_questions/comments/kurtkw/error_attempt_to_use_a_deleted_function/
        std::thread t(StartScraping,amount, counter, getSettingsKeywords, getUrls);

        if (t.joinable())
        {
            t.detach();
        }

        counter++;
    }
}

The other function named StopOperation is executed when the user presses the stop button to cancel the scraping operation.

// Cancel Scraping button
void MainFrame::StopOperation(wxMouseEvent &event)
{
    Scraper::isCanceled = true;
}

The good news is that it works! My tasks for tomorrow are to add static text underneath the buttons, inform the user of the current state of the application, store the scraped information in a file, and add a conditional statement to stop another operation form starting when the user presses the start button again.