linker.cpp 2.37 KB
Newer Older
1
#include "linker.h"
2
#include "owl.h"
3 4 5 6
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QEventLoop>
Camilo Higuita's avatar
Camilo Higuita committed
7 8 9 10 11

Linker::Linker(QObject *parent) : QObject(parent)
{

}
12 13 14 15 16 17 18 19 20 21 22 23 24 25
QByteArray Linker::getUrl(const QString &url)
{
    QUrl mURL(url);
    QNetworkAccessManager manager;
    QNetworkRequest request (mURL);

    QNetworkReply *reply =  manager.get(request);
    QEventLoop loop;
    connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);

    connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &loop,
            SLOT(quit()));

    loop.exec();
Camilo Higuita's avatar
Camilo Higuita committed
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    if(reply->error())
    {
        qDebug() << reply->error();
        return QByteArray();
    }

    if(reply->bytesAvailable())
    {
        auto data = reply->readAll();
        reply->deleteLater();

        return data;
    }

    return QByteArray();
}
Camilo Higuita's avatar
Camilo Higuita committed
43 44 45
/* extract needs to extract from a url the title, the body and a preview image*/
void Linker::extract(const QString &url)
{
46
    auto data = getUrl(url);
47

48
    QString title = url;
49 50
    auto titles = query(data, HtmlTag::TITLE);

51 52 53 54 55 56 57 58
    if(!titles.isEmpty())
        title = titles[0];

    title = title.isEmpty() ? url : title;

    QStringList imgs;

    //    auto tags = query(data, HtmlTag::META);
59

60 61 62 63
    for(auto img : query(data, HtmlTag::IMG, "src"))
    {
        if(imgs.contains(img) || img.isEmpty()) continue;

64 65 66 67 68 69
        qDebug()<< "IMGAE URL" << img;
        if((img.startsWith("http") || img.startsWith("https"))
                && (img.endsWith(".png", Qt::CaseInsensitive) ||
                    img.endsWith(".jpg", Qt::CaseInsensitive) ||
                    img.endsWith(".gif", Qt::CaseInsensitive) ||
                    img.endsWith(".jpeg", Qt::CaseInsensitive)))
70

71 72
            imgs << img;
        else continue;
73
    }
74 75

    LINK link_data {{OWL::KEYMAP[OWL::KEY::TITLE], title.trimmed()},
76 77 78
                    {OWL::KEYMAP[OWL::KEY::BODY], data},
                    {OWL::KEYMAP[OWL::KEY::IMAGE], imgs}};
    emit previewReady(link_data);
Camilo Higuita's avatar
Camilo Higuita committed
79 80 81 82
}



83
QStringList Linker::query(const QByteArray &array, const HtmlTag &tag, const QString &attribute)
Camilo Higuita's avatar
Camilo Higuita committed
84
{
85
    QStringList res;
86 87
    auto doc = QGumboDocument::parse(array);
    auto root = doc.rootNode();
Camilo Higuita's avatar
Camilo Higuita committed
88

89 90 91 92 93 94 95 96 97 98
    auto node = root.getElementsByTagName(tag);

    for(const auto &i : node)
    {
        if(attribute.isEmpty())
            res << i.innerText();
        else res << i.getAttribute(attribute);
    }

    return res;
camilo higuita's avatar
camilo higuita committed
99 100
}