| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "src/parsers/rdfparser.h" |
| 4 | |
| 5 | #include "src/definitions.h" |
| 6 | #include "src/standardfeed.h" |
| 7 | |
| 8 | #include <librssguard/exceptions/applicationexception.h> |
| 9 | #include <librssguard/miscellaneous/settings.h> |
| 10 | #include <librssguard/miscellaneous/textfactory.h> |
| 11 | |
| 12 | #include <QDomDocument> |
| 13 | #include <QTextCodec> |
| 14 | |
| 15 | RdfParser::RdfParser(const QString& data) |
| 16 | : FeedParser(data), m_rdfNamespace(QSL("http://www.w3.org/1999/02/22-rdf-syntax-ns#")), |
| 17 | m_rssNamespace(QSL("http://purl.org/rss/1.0/")), m_rssCoNamespace(QSL("http://purl.org/rss/1.0/modules/content/")), |
| 18 | m_dcElNamespace(QSL("http://purl.org/dc/elements/1.1/")) {} |
| 19 | |
| 20 | RdfParser::~RdfParser() {} |
| 21 | |
| 22 | QList<StandardFeed*> RdfParser::discoverFeeds(ServiceRoot* root, const QUrl& url, bool greedy) const { |
| 23 | auto base_result = FeedParser::discoverFeeds(root, url, greedy); |
| 24 | |
| 25 | if (!base_result.isEmpty()) { |
| 26 | return base_result; |
| 27 | } |
| 28 | |
| 29 | QString my_url = url.toString(); |
| 30 | QList<StandardFeed*> feeds; |
| 31 | |
| 32 | // 1. Test direct URL for a feed. |
| 33 | // 2. Test embedded RDF feed links from HTML data. |
| 34 | // 3. Test "URL/feed" endpoint. |
| 35 | // 4. Test "URL/rdf" endpoint. |
| 36 | |
| 37 | // Download URL. |
| 38 | int timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt(); |
| 39 | QByteArray data; |
| 40 | auto res = NetworkFactory::performNetworkOperation(my_url, |
| 41 | timeout, |
| 42 | {}, |
| 43 | data, |
| 44 | QNetworkAccessManager::Operation::GetOperation, |
| 45 | {}, |
| 46 | {}, |
| 47 | {}, |
| 48 | {}, |
| 49 | root->networkProxy()); |
| 50 | |
| 51 | if (res.m_networkError == QNetworkReply::NetworkError::NoError) { |
| 52 | try { |
| 53 | // 1. |
| 54 | auto guessed_feed = guessFeed(data, res); |
| 55 | |
| 56 | return {guessed_feed.first}; |
| 57 | } |
| 58 | catch (...) { |
| 59 | qDebugNN << LOGSEC_STANDARD << QUOTE_W_SPACE(my_url) << "is not a direct feed file."; |
| 60 | } |
| 61 | |
| 62 | // 2. |
| 63 | static QRegularExpression rx(QSL(RSS_REGEX_MATCHER), QRegularExpression::PatternOption::CaseInsensitiveOption); |
| 64 | static QRegularExpression rx_href(QSL(RSS_HREF_REGEX_MATCHER), |
| 65 | QRegularExpression::PatternOption::CaseInsensitiveOption); |
| 66 | |
| 67 | rx_href.optimize(); |
| 68 | |
| 69 | QRegularExpressionMatchIterator it_rx = rx.globalMatch(QString::fromUtf8(data)); |
| 70 | |
| 71 | while (it_rx.hasNext()) { |
| 72 | QRegularExpressionMatch mat_tx = it_rx.next(); |
| 73 | QString link_tag = mat_tx.captured(); |
| 74 | QString feed_link = rx_href.match(link_tag).captured(1); |
| 75 | |
| 76 | if (feed_link.startsWith(QL1S("//"))) { |
| 77 | feed_link = QSL(URI_SCHEME_HTTP) + feed_link.mid(2); |
| 78 | } |
| 79 | else if (feed_link.startsWith(QL1C('/'))) { |
| 80 | feed_link = url.toString(QUrl::UrlFormattingOption::RemovePath | QUrl::UrlFormattingOption::RemoveQuery | |
| 81 | QUrl::UrlFormattingOption::StripTrailingSlash) + |
| 82 | feed_link; |
| 83 | } |
| 84 | |
| 85 | QByteArray data; |
| 86 | auto res = NetworkFactory::performNetworkOperation(feed_link, |
| 87 | timeout, |
| 88 | {}, |
| 89 | data, |
| 90 | QNetworkAccessManager::Operation::GetOperation, |
| 91 | {}, |
| 92 | {}, |
| 93 | {}, |
| 94 | {}, |
| 95 | root->networkProxy()); |
| 96 | |
| 97 | if (res.m_networkError == QNetworkReply::NetworkError::NoError) { |
| 98 | try { |
| 99 | auto guessed_feed = guessFeed(data, res); |
| 100 | |
| 101 | feeds.append(guessed_feed.first); |
| 102 | } |
| 103 | catch (const ApplicationException& ex) { |
| 104 | qDebugNN << LOGSEC_STANDARD << QUOTE_W_SPACE(feed_link) |
| 105 | << " should be direct link to feed file but was not recognized:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 106 | } |
| 107 | } |
| 108 | else { |
| 109 | logUnsuccessfulRequest(res); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | else { |
| 114 | logUnsuccessfulRequest(res); |
| 115 | } |
| 116 | |
| 117 | // 3. |
| 118 | my_url = url.toString(QUrl::UrlFormattingOption::StripTrailingSlash) + QSL("/feed"); |
| 119 | res = NetworkFactory::performNetworkOperation(my_url, |
| 120 | timeout, |
| 121 | {}, |
| 122 | data, |
| 123 | QNetworkAccessManager::Operation::GetOperation, |
| 124 | {}, |
| 125 | {}, |
| 126 | {}, |
| 127 | {}, |
| 128 | root->networkProxy()); |
| 129 | |
| 130 | if (res.m_networkError == QNetworkReply::NetworkError::NoError) { |
| 131 | try { |
| 132 | auto guessed_feed = guessFeed(data, res); |
| 133 | |
| 134 | feeds.append(guessed_feed.first); |
| 135 | } |
| 136 | catch (...) { |
| 137 | qDebugNN << LOGSEC_STANDARD << QUOTE_W_SPACE(my_url) << "is not a direct feed file."; |
| 138 | } |
| 139 | } |
| 140 | else { |
| 141 | logUnsuccessfulRequest(res); |
| 142 | } |
| 143 | |
| 144 | // 4. |
| 145 | my_url = url.toString(QUrl::UrlFormattingOption::StripTrailingSlash) + QSL("/rdf"); |
| 146 | res = NetworkFactory::performNetworkOperation(my_url, |
| 147 | timeout, |
| 148 | {}, |
| 149 | data, |
| 150 | QNetworkAccessManager::Operation::GetOperation, |
| 151 | {}, |
| 152 | {}, |
| 153 | {}, |
| 154 | {}, |
| 155 | root->networkProxy()); |
| 156 | |
| 157 | if (res.m_networkError == QNetworkReply::NetworkError::NoError) { |
| 158 | try { |
| 159 | auto guessed_feed = guessFeed(data, res); |
| 160 | |
| 161 | feeds.append(guessed_feed.first); |
| 162 | } |
| 163 | catch (...) { |
| 164 | qDebugNN << LOGSEC_STANDARD << QUOTE_W_SPACE(my_url) << "is not a direct feed file."; |
| 165 | } |
| 166 | } |
| 167 | else { |
| 168 | logUnsuccessfulRequest(res); |
| 169 | } |
| 170 | |
| 171 | return feeds; |
| 172 | } |
| 173 | |
| 174 | QPair<StandardFeed*, QList<IconLocation>> RdfParser::guessFeed(const QByteArray& content, |
| 175 | const NetworkResult& network_res) const { |
| 176 | QString xml_schema_encoding = QSL(DEFAULT_FEED_ENCODING); |
| 177 | QString xml_contents_encoded; |
| 178 | QString enc = |
| 179 | QRegularExpression(QSL("encoding=\"([A-Z0-9\\-]+)\""), QRegularExpression::PatternOption::CaseInsensitiveOption) |
| 180 | .match(content) |
| 181 | .captured(1); |
| 182 | |
| 183 | if (!enc.isEmpty()) { |
| 184 | // Some "encoding" attribute was found get the encoding |
| 185 | // out of it. |
| 186 | xml_schema_encoding = enc; |
| 187 | } |
| 188 | |
| 189 | QTextCodec* custom_codec = QTextCodec::codecForName(xml_schema_encoding.toLocal8Bit()); |
| 190 | |
| 191 | if (custom_codec != nullptr) { |
| 192 | xml_contents_encoded = custom_codec->toUnicode(content); |
| 193 | } |
| 194 | else { |
| 195 | xml_contents_encoded = QString::fromUtf8(content); |
| 196 | } |
| 197 | |
| 198 | // NOTE: Some XMLs have whitespace before XML declaration, erase it. |
| 199 | xml_contents_encoded = xml_contents_encoded.trimmed(); |
| 200 | |
| 201 | // Feed XML was obtained, guess it now. |
| 202 | QDomDocument xml_document; |
| 203 | QString error_msg; |
| 204 | int error_line, error_column; |
| 205 | |
| 206 | if (!xml_document.setContent(xml_contents_encoded, true, &error_msg, &error_line, &error_column)) { |
| 207 | throw ApplicationException(QObject::tr("XML is not well-formed, %1").arg(error_msg)); |
| 208 | } |
| 209 | |
| 210 | QDomElement root_element = xml_document.documentElement(); |
| 211 | |
| 212 | if (root_element.namespaceURI() != rdfNamespace()) { |
| 213 | throw ApplicationException(QObject::tr("not an RDF feed")); |
| 214 | } |
| 215 | |
| 216 | auto* feed = new StandardFeed(); |
| 217 | QList<IconLocation> icon_possible_locations; |
| 218 | |
| 219 | feed->setEncoding(xml_schema_encoding); |
| 220 | feed->setType(StandardFeed::Type::Rdf); |
| 221 | feed->setSource(network_res.m_url.toString()); |
| 222 | |
| 223 | QDomElement channel_element = root_element.elementsByTagNameNS(rssNamespace(), QSL("channel")).at(0).toElement(); |
| 224 | |
| 225 | feed->setTitle(channel_element.elementsByTagNameNS(rssNamespace(), QSL("title")).at(0).toElement().text()); |
| 226 | feed |
| 227 | ->setDescription(channel_element.elementsByTagNameNS(rssNamespace(), QSL("description")).at(0).toElement().text()); |
| 228 | |
| 229 | QString home_page = channel_element.elementsByTagNameNS(rssNamespace(), QSL("link")).at(0).toElement().text(); |
| 230 | |
| 231 | if (!home_page.isEmpty()) { |
| 232 | icon_possible_locations.prepend({home_page, false}); |
| 233 | } |
| 234 | |
| 235 | return {feed, icon_possible_locations}; |
| 236 | } |
| 237 | |
| 238 | QDomNodeList RdfParser::xmlMessageElements() { |
| 239 | return m_xml.elementsByTagNameNS(m_rssNamespace, QSL("item")); |
| 240 | } |
| 241 | |
| 242 | QString RdfParser::rssNamespace() const { |
| 243 | return m_rssNamespace; |
| 244 | } |
| 245 | |
| 246 | QString RdfParser::rdfNamespace() const { |
| 247 | return m_rdfNamespace; |
| 248 | } |
| 249 | |
| 250 | QString RdfParser::xmlMessageTitle(const QDomElement& msg_element) const { |
| 251 | return msg_element.elementsByTagNameNS(m_rssNamespace, QSL("title")).at(0).toElement().text(); |
| 252 | } |
| 253 | |
| 254 | QString RdfParser::xmlMessageDescription(const QDomElement& msg_element) const { |
| 255 | QString description = msg_element.elementsByTagNameNS(m_rssCoNamespace, QSL("encoded")).at(0).toElement().text(); |
| 256 | |
| 257 | if (description.simplified().isEmpty()) { |
| 258 | description = msg_element.elementsByTagNameNS(m_rssNamespace, QSL("description")).at(0).toElement().text(); |
| 259 | } |
| 260 | |
| 261 | return description; |
| 262 | } |
| 263 | |
| 264 | QString RdfParser::xmlMessageAuthor(const QDomElement& msg_element) const { |
| 265 | return msg_element.elementsByTagNameNS(m_dcElNamespace, QSL("creator")).at(0).toElement().text(); |
| 266 | } |
| 267 | |
| 268 | QDateTime RdfParser::xmlMessageDateCreated(const QDomElement& msg_element) { |
| 269 | return TextFactory::parseDateTime(msg_element.elementsByTagNameNS(m_dcElNamespace, QSL("date")) |
| 270 | .at(0) |
| 271 | .toElement() |
| 272 | .text(), |
| 273 | &m_dateTimeFormat); |
| 274 | } |
| 275 | |
| 276 | QString RdfParser::xmlMessageId(const QDomElement& msg_element) const { |
| 277 | return msg_element.elementsByTagNameNS(m_dcElNamespace, QSL("identifier")).at(0).toElement().text(); |
| 278 | } |
| 279 | |
| 280 | QString RdfParser::xmlMessageUrl(const QDomElement& msg_element) const { |
| 281 | return msg_element.elementsByTagNameNS(m_rssNamespace, QSL("link")).at(0).toElement().text(); |
| 282 | } |
| 283 | |
| 284 | QList<Enclosure> RdfParser::xmlMessageEnclosures(const QDomElement& msg_element) const { |
| 285 | return {}; |
| 286 | } |