#include <iostream>
#include <curl/curl.h>
#include "pugixml.hpp"
#include <regex>

using namespace std;

// Callback function to write data received from cURL request
size_t WriteCallback(void* contents, size_t size, size_t nmemb, string* output) {
    size_t totalSize = size * nmemb;
    output->append((char*)contents, totalSize);
    return totalSize;
}

// Function to fetch HTML content using cURL
string fetchHTML(const string& url) {
    CURL* curl;
    CURLcode res;
    string response;

    curl = curl_easy_init();
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
        curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);
    }
    return response;
}

// Function to extract pronunciation URLs using regex
void extractPronunciationLinks(const string& html) {
    regex audioRegex(R"((https://d...content-available-to-author-only...e.org/.*?\.mp3))");
    smatch matches;
    string::const_iterator searchStart(html.cbegin());

    while (regex_search(searchStart, html.cend(), matches, audioRegex)) {
        cout << "Pronunciation link: " << matches[1] << endl;
        searchStart = matches.suffix().first;
    }
}

int main() {
    string words[] = {"personal", "natural", "nationality", "atmosphere"};

    for (const string& word : words) {
        string url = "https://d...content-available-to-author-only...e.org/dictionary/english/" + word;
        cout << "Fetching pronunciation for: " << word << endl;
        string htmlContent = fetchHTML(url);
        extractPronunciationLinks(htmlContent);
        cout << "----------------------------------" << endl;
    }
    return 0;
}
