[go: up one dir, main page]

File: html.cpp

package info (click to toggle)
syndication-domination 1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 408 kB
  • sloc: cpp: 1,101; makefile: 42; sh: 10
file content (243 lines) | stat: -rw-r--r-- 7,616 bytes parent folder | download
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "html.hpp"

void Html::configure_tidy_doc(TidyDoc &doc) {
    bool conf_success =
        tidyOptSetBool(doc, TidyXmlOut, yes) &&
        tidyOptSetBool(doc, TidyForceOutput, yes) &&
        tidyOptSetBool(doc, TidyWarnPropAttrs, no) &&
        tidyOptSetBool(doc, TidyShowWarnings, no) &&
        tidyOptSetInt(doc, TidyShowErrors, 0);
    if (!conf_success) {
        throw std::runtime_error("Error with tidy configuration");
    }
}

TidyDoc Html::tidy_doc_from_file(std::string path) {
    TidyDoc doc = tidyCreate();
    configure_tidy_doc(doc);
    int res = tidyParseFile(doc, path.c_str());
    if (res < 0) throw std::runtime_error("Error parsing HTML");
    return doc;
}

std::string Html::convert_to_xml(TidyDoc doc) {
    TidyBuffer out_buf = {0};
    int res = -1;
    // res: 0=ok; 1=warnings; 2=errors
    res = tidyCleanAndRepair(doc);
    if (res >= 0) res = tidySaveBuffer(doc, &out_buf);
    if (res < 0) throw std::runtime_error("Error parsing HTML");

    std::string xml = (char*) out_buf.bp;
    tidyBufFree(&out_buf);
    tidyRelease(doc);
    return xml;
}

void Html::remove_useless_children(xml_node &root) {
    for (auto name: USELESS_CHILDREN) {
        xpath_node_set nodes = root.select_nodes(("//"+name).c_str());
        for (xpath_node node: nodes) {
            node.node().parent().remove_child(node.node());
        }
    }
}

Html::Html(TidyDoc &tdoc) {
    std::string xml = convert_to_xml(tdoc);
    xml_parse_result res = doc.load_string(xml.c_str());
    if (!res) {
        throw std::runtime_error("Error parsing XML file");
    }
    head = doc.document_element().child("head");
}

xml_node Html::get_body_node() {
    xml_node body_node = doc.document_element().child("body");
    remove_useless_children(body_node);
    return body_node;
}

Html::Html(std::string path) {
    std::string xml = convert_to_xml(tidy_doc_from_file(path));
    xml_parse_result res = doc.load_string(xml.c_str());
    if (!res) {
        throw std::runtime_error("Error parsing XML file: "+path);
    }
    head = doc.document_element().child("head");
}


Html Html::from_string(std::string s) {
    TidyDoc doc = tidyCreate();
    configure_tidy_doc(doc);
    int res = tidyParseString(doc, s.c_str());
    if (res < 0) throw std::runtime_error("Error parsing HTML");
    return Html(doc);
}

std::string Html::get_title() {
    if (title.empty()) {
        title = head.child("title").text().as_string();
    }
    return title;
}

std::string Html::get_icon_url() {
    if (icon_url.empty()) {
        std::string rel, size_s;
        int size = -1;
        for (
                xml_node link = head.child("link");
                link;
                link = link.next_sibling("link")
        ) {
            rel = link.attribute("rel").value();
            if (
                    rel == "apple-touch-icon" ||
                    rel == "apple-touch-icon-precomposed"
            ) {
                size_s = link.attribute("size").value();
                if (size_s.empty())
                    size_s = link.attribute("sizes").value();
                if (!size_s.empty()) {
                    int n_size = std::stoi(
                        SynDomUtils::split(size_s, 'x')[0]
                    );
                    if (n_size <= size) continue;
                    size = n_size;
                }
                icon_url = link.attribute("href").value();
                if (!icon_url.empty() && (size <= 0 || size >= 180)) break;
            }
        }
        if (!icon_url.empty()) return icon_url;
        for (
                xml_node link = head.child("link");
                link;
                link = link.next_sibling("link")
        ) {
            rel = link.attribute("rel").value();
            if (rel == "icon" || rel == "shortcut icon") {
                icon_url = link.attribute("href").value();
                if (!icon_url.empty()) break;
            }
        }
    }
    return icon_url;
}
std::string Html::get_img_url() {
    if (img_url.empty()) {
        std::string property, name;
        for (
                xml_node meta = head.child("meta");
                meta;
                meta = meta.next_sibling("meta")
        ) {
            property = meta.attribute("property").value();
            name = meta.attribute("name").value();
            if (
                    property == "og:image" || property == "image" ||
                    property == "twitter:image" ||
                    name == "og:image" || name == "image" ||
                    name == "twitter:image"
            ) {
                img_url = meta.attribute("content").value();
                if (!img_url.empty()) break;
            }
        }
    }
    return img_url;
}
std::string Html::get_rss_url() {
    if (rss_url.empty()) {
        std::string rel, type;
        for (
                xml_node link = head.child("link");
                link;
                link = link.next_sibling("link")
        ) {
            rel = link.attribute("rel").value();
            type = link.attribute("type").value();
            if (rel == "alternate" && (
                    type == "application/rss+xml" ||
                    type == "application/atom+xml"
            )) {
                rss_url = link.attribute("href").value();
                if (!rss_url.empty()) break;
            }
        }
    }
    return rss_url;
}
std::string Html::get_body() {
    if (body.empty()) {
        xml_node body_node = get_body_node();
        SynDomUtils::xml_string_writer writer;
        body_node.print(writer, "");
        body = writer.result;
    }
    return body;
}
std::string Html::get_article() {
    if (article.empty()) {
        xml_node body_node = doc.document_element().child("body");
        xml_node article_node = body_node.find_child([](xml_node n){
            return std::strcmp(n.name(), "article") == 0;
        });
        SynDomUtils::xml_string_writer writer;
        if (article_node) {
            remove_useless_children(article_node);
            article_node.print(writer, "");
            article = writer.result;
        }
        else {
            xml_node clean_body_node = get_body_node();
            clean_body_node.set_name("article");
            SynDomUtils::xml_string_writer writer;
            clean_body_node.print(writer, "");
            article = writer.result;
        }
    }
    return article;
}
std::string Html::get_description() {
    if (description.empty()) {
        std::string property;
        for (
                xml_node meta = head.child("meta");
                meta;
                meta = meta.next_sibling("meta")
        ) {
            property = meta.attribute("property").value();
            if (
                    property == "og:description" ||
                    property == "description"
            ) {
                description = meta.attribute("content").value();
                if (!description.empty()) break;
            }
        }
    }
    return description;
}
std::string Html::to_json(bool metadata_only) {
    std::string res = "{\n"
        "\t\"title\": \"" + get_title() + "\",\n"
        "\t\"icon_url\": \"" + get_icon_url() + "\",\n"
        "\t\"img_url\": \"" + get_img_url() + "\",\n"
        "\t\"rss_url\": \"" + get_rss_url() + "\",\n"
        "\t\"description\": \"" + get_description() + "\"";

    if (!metadata_only) {
        res += ",\n"
            "\t\"body\": \"" + get_body() + "\",\n"
            "\t\"article\": \"" + get_article() + "\"\n"
        "}\n";
    }
    else {
        res += "\n}\n";
    }

    return res;
}