#include "web.h" #include #include #include namespace web { static size_t write_cb(char* ptr, size_t size, size_t nmemb, void* ud) { auto* s = (std::string*)ud; s->append(ptr, size * nmemb); if (s->size() > 600000) return 0; return size * nmemb; } std::string http_get(const std::string& url) { CURL* c = curl_easy_init(); if (!c) return ""; std::string out; curl_easy_setopt(c, CURLOPT_URL, url.c_str()); curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(c, CURLOPT_TIMEOUT, 15L); curl_easy_setopt(c, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"); curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_cb); curl_easy_setopt(c, CURLOPT_WRITEDATA, &out); curl_easy_setopt(c, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(c, CURLOPT_ACCEPT_ENCODING, ""); struct curl_slist* hdr = nullptr; hdr = curl_slist_append(hdr, "Accept-Language: de-DE,de;q=0.9,en;q=0.8"); curl_easy_setopt(c, CURLOPT_HTTPHEADER, hdr); curl_easy_perform(c); curl_slist_free_all(hdr); curl_easy_cleanup(c); return out; } static std::string url_encode(const std::string& s) { static const char* hex = "0123456789ABCDEF"; std::string o; for (unsigned char c : s) { if (std::isalnum(c) || c=='-'||c=='_'||c=='.'||c=='~') o += (char)c; else { o += '%'; o += hex[c>>4]; o += hex[c&0xF]; } } return o; } static std::string decode_entities(const std::string& s) { std::string o; o.reserve(s.size()); for (size_t i = 0; i < s.size(); ) { if (s[i] == '&') { size_t semi = s.find(';', i); if (semi != std::string::npos && semi - i < 8) { std::string e = s.substr(i+1, semi-i-1); if (e == "amp") { o += '&'; i = semi+1; continue; } if (e == "lt") { o += '<'; i = semi+1; continue; } if (e == "gt") { o += '>'; i = semi+1; continue; } if (e == "quot") { o += '"'; i = semi+1; continue; } if (e == "#39" || e == "apos") { o += '\''; i = semi+1; continue; } if (e == "nbsp") { o += ' '; i = semi+1; continue; } if (!e.empty() && e[0] == '#') { int code = atoi(e.c_str()+1); if (code > 0 && code < 128) { o += (char)code; i = semi+1; continue; } if (code >= 0x80 && code < 0x800) { o += (char)(0xC0 | (code >> 6)); o += (char)(0x80 | (code & 0x3F)); i = semi+1; continue; } } } } o += s[i++]; } return o; } std::string strip_html(const std::string& html) { std::string h = html; auto remove_block = [&](const std::string& open, const std::string& close) { size_t p = 0; while ((p = h.find(open, p)) != std::string::npos) { size_t e = h.find(close, p); if (e == std::string::npos) { h.erase(p); break; } h.erase(p, e + close.size() - p); } }; remove_block(""); remove_block(""); remove_block(""); std::string o; o.reserve(h.size()); bool in_tag = false, last_space = true; for (char c : h) { if (c == '<') { in_tag = true; if (!last_space) { o += ' '; last_space = true; } continue; } if (c == '>') { in_tag = false; continue; } if (in_tag) continue; if (c == '\r' || c == '\n' || c == '\t') c = ' '; if (c == ' ') { if (!last_space) { o += ' '; last_space = true; } } else { o += c; last_space = false; } } return decode_entities(o); } std::vector search(const std::string& query, size_t max_results) { std::vector out; std::string url = "https://html.duckduckgo.com/html/?q=" + url_encode(query) + "&kl=de-de"; std::string body = http_get(url); if (body.empty()) return out; size_t pos = 0; while (out.size() < max_results) { size_t a = body.find("result__a", pos); if (a == std::string::npos) break; size_t href = body.find("href=\"", a); if (href == std::string::npos) break; href += 6; size_t href_end = body.find("\"", href); if (href_end == std::string::npos) break; std::string link = body.substr(href, href_end - href); size_t uddg = link.find("uddg="); if (uddg != std::string::npos) { std::string enc = link.substr(uddg + 5); size_t amp = enc.find('&'); if (amp != std::string::npos) enc = enc.substr(0, amp); std::string dec; for (size_t i = 0; i < enc.size(); ++i) { if (enc[i] == '%' && i+2 < enc.size()) { dec += (char)std::stoi(enc.substr(i+1, 2), nullptr, 16); i += 2; } else if (enc[i] == '+') dec += ' '; else dec += enc[i]; } link = dec; } if (link.rfind("//", 0) == 0) link = "https:" + link; size_t titEnd = body.find("", href_end); std::string title; if (titEnd != std::string::npos) { title = strip_html(body.substr(href_end + 1, titEnd - href_end - 1)); while (!title.empty() && std::isspace((unsigned char)title.front())) title.erase(title.begin()); while (!title.empty() && std::isspace((unsigned char)title.back())) title.pop_back(); } size_t snip = body.find("result__snippet", titEnd == std::string::npos ? href_end : titEnd); std::string snippet; if (snip != std::string::npos) { size_t gt = body.find(">", snip); size_t end_a = body.find("", gt); if (gt != std::string::npos && end_a != std::string::npos) { snippet = strip_html(body.substr(gt+1, end_a - gt - 1)); while (!snippet.empty() && std::isspace((unsigned char)snippet.front())) snippet.erase(snippet.begin()); while (!snippet.empty() && std::isspace((unsigned char)snippet.back())) snippet.pop_back(); } pos = end_a == std::string::npos ? snip + 1 : end_a; } else { pos = titEnd == std::string::npos ? href_end : titEnd; } // Werbeanzeigen rausfiltern (DuckDuckGo Ads haben y.js?ad_domain=...) bool is_ad = link.find("duckduckgo.com/y.js") != std::string::npos || link.find("ad_domain=") != std::string::npos || link.find("bing.com/aclick") != std::string::npos; if (!link.empty() && link.find("http") == 0 && !is_ad) out.push_back({title, link, snippet}); } return out; } Research research(const std::string& topic, size_t max_results) { Research r; auto hits = search(topic, max_results); for (auto& h : hits) { std::string body = http_get(h.url); std::string text = strip_html(body); if (text.size() > 3000) text.resize(3000); r.text += "### " + h.title + "\n"; r.text += "URL: " + h.url + "\n"; if (!h.snippet.empty()) r.text += "Kurz: " + h.snippet + "\n"; r.text += "Inhalt: " + text + "\n\n"; r.sources.push_back(h.title + " - " + h.url); } return r; } } // namespace web