main.cpp 5 KB
Newer Older
Volker Krause's avatar
Volker Krause committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
    Copyright (C) 2019 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

Volker Krause's avatar
Volker Krause committed
18 19
#include <kitinerary_version.h>

Volker Krause's avatar
Volker Krause committed
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
#include <KItinerary/ExtractorEngine>
#include <KItinerary/ExtractorPostprocessor>
#include <KItinerary/HtmlDocument>
#include <KItinerary/JsonLdDocument>
#include <KItinerary/PdfDocument>

#include <KPkPass/Pass>

#include <KMime/Message>

#include <KCalCore/MemoryCalendar>
#include <KCalCore/ICalFormat>

#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QObject>

#include <iostream>

using namespace KItinerary;

static void printCapabilities()
{
    // TODO
}

int main(int argc, char** argv)
{
    QCoreApplication::setApplicationName(QStringLiteral("kitinerary-extractor"));
Volker Krause's avatar
Volker Krause committed
54
    QCoreApplication::setApplicationVersion(QStringLiteral(KITINERARY_VERSION_STRING));
Volker Krause's avatar
Volker Krause committed
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
    QCoreApplication::setOrganizationDomain(QStringLiteral("kde.org"));
    QCoreApplication::setOrganizationName(QStringLiteral("KDE"));
    QCoreApplication app(argc, argv);

    QCommandLineParser parser;
    parser.setApplicationDescription(QStringLiteral("Command line itinerary extractor."));
    parser.addHelpOption();
    parser.addVersionOption();
    QCommandLineOption capOpt({QStringLiteral("capabilities")}, QStringLiteral("Show available extraction capabilities."));
    parser.addOption(capOpt);
    QCommandLineOption typeOpt({QStringLiteral("t"), QStringLiteral("type")}, QStringLiteral("Type of the input data [mime, pdf, pkpass, ical, html]."), QStringLiteral("type"));
    parser.addOption(typeOpt);

    parser.addPositionalArgument(QStringLiteral("input"), QStringLiteral("File to extract data from, omit for using stdin."));
    parser.process(app);

    if (parser.isSet(capOpt)) {
        printCapabilities();
        return 0;
    }

    QFile f;
    if (parser.positionalArguments().size() == 1) {
        f.setFileName(parser.positionalArguments().at(0));
        if (!f.open(QFile::ReadOnly)) {
            std::cerr << qPrintable(f.errorString()) << std::endl;
            return 1;
        }
    } else {
        f.open(stdin, QFile::ReadOnly);
    }

    ExtractorEngine engine;

    std::unique_ptr<KPkPass::Pass> pass;
    std::unique_ptr<HtmlDocument> htmlDoc;
    std::unique_ptr<PdfDocument> pdfDoc;
    KCalCore::Calendar::Ptr calendar;
    std::unique_ptr<KMime::Message> mimeMsg;
    QJsonArray jsonResult;

    if (f.fileName().endsWith(QLatin1String(".pkpass")) || parser.value(typeOpt) == QLatin1String("pkpass")) {
        pass.reset(KPkPass::Pass::fromData(f.readAll()));
        engine.setPass(pass.get());
    } else if (f.fileName().endsWith(QLatin1String(".pdf")) || parser.value(typeOpt) == QLatin1String("pdf")) {
        pdfDoc.reset(PdfDocument::fromData(f.readAll()));
        engine.setPdfDocument(pdfDoc.get());
    } else if (f.fileName().endsWith(QLatin1String(".html")) || parser.value(typeOpt) == QLatin1String("html")) {
        htmlDoc.reset(HtmlDocument::fromData(f.readAll()));
        engine.setHtmlDocument(htmlDoc.get());
    } else if (f.fileName().endsWith(QLatin1String(".txt"))) {
        engine.setText(QString::fromUtf8(f.readAll()));
    } else if (f.fileName().endsWith(QLatin1String(".ics")) || parser.value(typeOpt) == QLatin1String("ical")) {
        calendar.reset(new KCalCore::MemoryCalendar(QTimeZone()));
        KCalCore::ICalFormat format;
Volker Krause's avatar
Volker Krause committed
110 111 112 113
        if (!format.fromRawString(calendar, f.readAll())) {
            std::cerr << "Failed to parse iCal file." << std::endl;
            return 1;
        }
Volker Krause's avatar
Volker Krause committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        calendar->setProductId(format.loadedProductId());
        engine.setCalendar(calendar);
    } else if (f.fileName().endsWith(QLatin1String(".eml")) || f.fileName().endsWith(QLatin1String(".mbox")) || parser.value(typeOpt) == QLatin1String("mime")) {
        mimeMsg.reset(new KMime::Message);
        mimeMsg->setContent(f.readAll());
        mimeMsg->parse();
        engine.setContent(mimeMsg.get());
    }

    jsonResult = engine.extract();
    const auto result = JsonLdDocument::fromJson(jsonResult);
    ExtractorPostprocessor postproc;
    // TODO extra option to set this, or retrieve from mime message
//     postproc.setContextDate(contextMsg.date()->dateTime());
    postproc.process(result);
    const auto postProcResult = JsonLdDocument::toJson(postproc.result());
    std::cout << QJsonDocument(postProcResult).toJson().constData() << std::endl;
}