File size: 7,445 Bytes
85b4c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5d21f28
 
 
 
 
85b4c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include "web.h"
#include <curl/curl.h>
#include <cctype>
#include <cstring>

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("<script", "</script>");
    remove_block("<style", "</style>");
    remove_block("<noscript", "</noscript>");

    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<SearchHit> search(const std::string& query, size_t max_results) {
    std::vector<SearchHit> 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("</a>", 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("</a>", 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