| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "network-web/articleparse.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "exceptions/applicationexception.h" |
| 7 | #include "miscellaneous/application.h" |
| 8 | |
| 9 | #include <QDir> |
| 10 | |
| 11 | #define EXTRACTOR_PACKAGE "@extractus/article-extractor" |
| 12 | #define EXTRACTOR_VERSION "8.0.7" |
| 13 | |
| 14 | #define FETCH_PACKAGE "fetch-charset-detection" |
| 15 | #define FETCH_VERSION "1.0.1" |
| 16 | |
| 17 | ArticleParse::ArticleParse(QObject* parent) : QObject{parent}, m_modulesInstalling(false), m_modulesInstalled(false) { |
| 18 | connect(qApp->nodejs(), &NodeJs::packageInstalledUpdated, this, &ArticleParse::onPackageReady); |
| 19 | connect(qApp->nodejs(), &NodeJs::packageError, this, &ArticleParse::onPackageError); |
| 20 | } |
| 21 | |
| 22 | void ArticleParse::onPackageReady(const QObject* sndr, |
| 23 | const QList<NodeJs::PackageMetadata>& pkgs, |
| 24 | bool already_up_to_date) { |
| 25 | Q_UNUSED(already_up_to_date) |
| 26 | |
| 27 | bool concerns_extractor = boolinq::from(pkgs).any([](const NodeJs::PackageMetadata& pkg) { |
| 28 | return pkg.m_name == QSL(EXTRACTOR_PACKAGE); |
| 29 | }); |
| 30 | |
| 31 | if (!concerns_extractor) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | m_modulesInstalled = true; |
| 36 | m_modulesInstalling = false; |
| 37 | |
| 38 | qApp->showGuiMessage(Notification::Event::NodePackageUpdated, |
| 39 | {tr("Packages for article-extractor are installed"), |
| 40 | tr("Reload your website or article and you can test it then!"), |
| 41 | QSystemTrayIcon::MessageIcon::Information}, |
| 42 | {true, true, false}); |
| 43 | |
| 44 | // Emit this just to allow the action again for user. |
| 45 | emit errorOnArticleParsing(sndr, tr("Packages for article-extractor are installed. You can now use this feature!")); |
| 46 | } |
| 47 | |
| 48 | void ArticleParse::onPackageError(const QObject* sndr, |
| 49 | const QList<NodeJs::PackageMetadata>& pkgs, |
| 50 | const QString& error) { |
| 51 | bool concerns_extractor = boolinq::from(pkgs).any([](const NodeJs::PackageMetadata& pkg) { |
| 52 | return pkg.m_name == QSL(EXTRACTOR_PACKAGE); |
| 53 | }); |
| 54 | |
| 55 | if (!concerns_extractor) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | m_modulesInstalled = m_modulesInstalling = false; |
| 60 | |
| 61 | qApp->showGuiMessage(Notification::Event::NodePackageUpdated, |
| 62 | {tr("Packages for article-extractor are NOT installed"), |
| 63 | tr("There is error: %1").arg(error), |
| 64 | QSystemTrayIcon::MessageIcon::Critical}, |
| 65 | {true, true, false}); |
| 66 | |
| 67 | // Emit this just to allow readability again for user. |
| 68 | emit errorOnArticleParsing(sndr, |
| 69 | tr("Packages for article-extractor are NOT installed. There is error: %1").arg(error)); |
| 70 | } |
| 71 | |
| 72 | void ArticleParse::parseArticle(QObject* sndr, const QString& url) { |
| 73 | if (!m_modulesInstalled) { |
| 74 | // NOTE: Here we use MJS file directly placed in its NODE package folder |
| 75 | // because NODE_PATH is not supported for MJS files. |
| 76 | m_scriptFilename = qApp->nodejs()->processedPackageFolder() + QDir::separator() + QSL("extract-article.mjs"); |
| 77 | |
| 78 | if (!IOFactory::copyFile(QSL(":/scripts/article-extractor/extract-article.mjs"), m_scriptFilename)) { |
| 79 | qCriticalNN << LOGSEC_ADBLOCK << "Failed to copy article-extractor script to TEMP."; |
| 80 | } |
| 81 | |
| 82 | try { |
| 83 | NodeJs::PackageStatus st_extractor = |
| 84 | qApp->nodejs()->packageStatus({QSL(EXTRACTOR_PACKAGE), QSL(EXTRACTOR_VERSION)}); |
| 85 | NodeJs::PackageStatus st_fetch = qApp->nodejs()->packageStatus({QSL(FETCH_PACKAGE), QSL(FETCH_VERSION)}); |
| 86 | |
| 87 | if (st_extractor != NodeJs::PackageStatus::UpToDate || st_fetch != NodeJs::PackageStatus::UpToDate) { |
| 88 | if (!m_modulesInstalling) { |
| 89 | // We make sure to update modules. |
| 90 | m_modulesInstalling = true; |
| 91 | qApp->nodejs()->installUpdatePackages(sndr, |
| 92 | {{QSL(EXTRACTOR_PACKAGE), QSL(EXTRACTOR_VERSION)}, |
| 93 | {QSL(FETCH_PACKAGE), QSL(FETCH_VERSION)}}); |
| 94 | } |
| 95 | |
| 96 | return; |
| 97 | } |
| 98 | else { |
| 99 | m_modulesInstalled = true; |
| 100 | } |
| 101 | } |
| 102 | catch (const ApplicationException& ex) { |
| 103 | qApp->showGuiMessage(Notification::Event::NodePackageUpdated, |
| 104 | {tr("Node.js libraries not installed"), |
| 105 | tr("Node.js is not configured properly. Go to \"Settings\" -> \"Node.js\" and check " |
| 106 | "if your Node.js is properly configured."), |
| 107 | QSystemTrayIcon::MessageIcon::Critical}, |
| 108 | {true, true, false}); |
| 109 | |
| 110 | qCriticalNN << LOGSEC_CORE << "Failed to check for Node.js package status:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 111 | |
| 112 | // Emit this just to allow readability again for user. |
| 113 | emit articleParsed(sndr, |
| 114 | url, |
| 115 | tr("Node.js is not configured properly. Go to \"Settings\" -> \"Node.js\" and check " |
| 116 | "if your Node.js is properly configured.")); |
| 117 | return; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | QProcess* proc = new QProcess(this); |
| 122 | |
| 123 | connect(proc, |
| 124 | QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), |
| 125 | this, |
| 126 | [=](int exit_code, QProcess::ExitStatus exit_status) { |
| 127 | onParsingFinished(sndr, url, exit_code, exit_status); |
| 128 | }); |
| 129 | |
| 130 | qApp->nodejs()->runScript(proc, m_scriptFilename, {url}); |
| 131 | } |
| 132 | |
| 133 | void ArticleParse::onParsingFinished(const QObject *sndr, |
| 134 | const QString& url, |
| 135 | int exit_code, |
| 136 | QProcess::ExitStatus exit_status) { |
| 137 | QProcess* proc = qobject_cast<QProcess*>(sender()); |
| 138 | |
| 139 | if (exit_status == QProcess::ExitStatus::NormalExit && exit_code == EXIT_SUCCESS) { |
| 140 | emit articleParsed(sndr, url, QString::fromUtf8(proc->readAllStandardOutput())); |
| 141 | } |
| 142 | else { |
| 143 | QString err = QString::fromUtf8(proc->readAllStandardError()); |
| 144 | emit errorOnArticleParsing(sndr, err); |
| 145 | } |
| 146 | |
| 147 | proc->deleteLater(); |
| 148 | } |