UBOEmbedParser.cpp 8.79 KB
Newer Older
1
/*
2
 * Copyright (C) 2015-2018 Département de l'Instruction Publique (DIP-SEM)
Craig Watson's avatar
Craig Watson committed
3
 *
Claudio Valerio's avatar
Claudio Valerio committed
4
 * Copyright (C) 2013 Open Education Foundation
5
 *
Claudio Valerio's avatar
Claudio Valerio committed
6 7
 * Copyright (C) 2010-2013 Groupement d'Intérêt Public pour
 * l'Education Numérique en Afrique (GIP ENA)
8
 *
Claudio Valerio's avatar
Claudio Valerio committed
9 10 11
 * This file is part of OpenBoard.
 *
 * OpenBoard is free software: you can redistribute it and/or modify
Claudio Valerio's avatar
Claudio Valerio committed
12 13
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License,
14 15 16 17
 * with a specific linking exception for the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the
 * same license as the "OpenSSL" library).
 *
Claudio Valerio's avatar
Claudio Valerio committed
18
 * OpenBoard is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Claudio Valerio's avatar
Claudio Valerio committed
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Claudio Valerio's avatar
Claudio Valerio committed
21
 * GNU General Public License for more details.
22
 *
Claudio Valerio's avatar
Claudio Valerio committed
23
 * You should have received a copy of the GNU General Public License
Claudio Valerio's avatar
Claudio Valerio committed
24
 * along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
25 26
 */

27

Claudio Valerio's avatar
Claudio Valerio committed
28

Claudio Valerio's avatar
Claudio Valerio committed
29

30 31 32 33 34 35 36 37 38 39 40
#include <QRegExp>
#include <QStringList>
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>
#include <QScriptValue>
#include <QScriptEngine>
#include <QDebug>

#include "UBOEmbedParser.h"

41 42
#include "core/memcheck.h"

43 44
UBOEmbedParser::UBOEmbedParser(QObject *parent, const char* name)
{
Claudio Valerio's avatar
Claudio Valerio committed
45
    Q_UNUSED(parent);
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
    setObjectName(name);
    mParsedTitles.clear();
    connect(this, SIGNAL(parseContent(QString)), this, SLOT(onParseContent(QString)));
}

UBOEmbedParser::~UBOEmbedParser()
{

}

void UBOEmbedParser::setNetworkAccessManager(QNetworkAccessManager *nam)
{
    mpNam = nam;
    connect(mpNam, SIGNAL(finished(QNetworkReply*)), this, SLOT(onFinished(QNetworkReply*)));
}

void UBOEmbedParser::parse(const QString& html)
{
    mContents.clear();
    QString query = "<link([^>]*)>";
    QRegExp exp(query);
    QStringList results;
    int count = 0;
    int pos = 0;
    while ((pos = exp.indexIn(html, pos)) != -1) {
        ++count;
        pos += exp.matchedLength();
        QStringList res = exp.capturedTexts();
        if("" != res.at(1)){
            results << res.at(1);
        }
    }

    QVector<QString> oembedUrls;

    if(2 <= results.size()){
        for(int i=1; i<results.size(); i++){
            if("" != results.at(i)){
                QString qsNode = QString("<link%0>").arg(results.at(i));
                QDomDocument domDoc;
                domDoc.setContent(qsNode);
                QDomNode linkNode = domDoc.documentElement();

                //  At this point, we have a node that is the <link> element. Now we have to parse its attributes
                //  in order to check if it is a oEmbed node or not
                QDomAttr typeAttribute = linkNode.toElement().attributeNode("type");
                if(typeAttribute.value().contains("oembed")){
                    // The node is an oembed one! We have to get the url and the type of oembed encoding
                    QDomAttr hrefAttribute = linkNode.toElement().attributeNode("href");
                    QString url = hrefAttribute.value();
                    oembedUrls.append(url);
                }
            }
        }
    }

    mPending = oembedUrls.size();

    if(0 == mPending){
        emit oembedParsed(mContents);
    }else{
        // Here we start the parsing (finally...)!
        for(int i=0; i<oembedUrls.size(); i++){
            emit parseContent(oembedUrls.at(i));
        }
    }
}

/**
  /brief Extract the oembed infos from the JSON
  @param jsonUrl as the url of the JSON file
  */
sOEmbedContent UBOEmbedParser::getJSONInfos(const QString &json)
{
    sOEmbedContent content;

    QScriptValue scriptValue;
    QScriptEngine scriptEngine;
    scriptValue = scriptEngine.evaluate ("(" + json + ")");

    QString providerUrl = scriptValue.property("provider_url").toString();
    QString title = scriptValue.property("title").toString();
    QString html = scriptValue.property("html").toString();
    QString authorName = scriptValue.property("author_name").toString();
    int height = scriptValue.property("height").toInteger();
    int thumbnailWidth = scriptValue.property("thumbnail_width").toInteger();
    int width = scriptValue.property("width").toInteger();
    float version = scriptValue.property("version").toString().toFloat();
    QString authorUrl = scriptValue.property("author_url").toString();
    QString providerName = scriptValue.property("provider_name").toString();
    QString thumbnailUrl = scriptValue.property("thumbnail_url").toString();
    QString type = scriptValue.property("type").toString();
    int thumbnailHeight = scriptValue.property("thumbnail_height").toInteger();

    content.providerUrl = providerUrl;
    content.title = title;
    content.html = html;
    content.author = authorName;
    content.height = height;
    content.thumbWidth = thumbnailWidth;
    content.width = width;
    content.version = version;
    content.authorUrl = authorUrl;
    content.providerName = providerName;
    content.thumbUrl = thumbnailUrl;
    content.type = type;
    content.thumbHeight = thumbnailHeight;

    if("photo" == content.type){
        content.url = scriptValue.property("url").toString();
    }else if("video" == content.type){
        QStringList strl = content.html.split('\"');
        for(int i=0; i<strl.size(); i++){
            if(strl.at(i).endsWith("src=") && strl.size() > (i+1)){
                content.url = strl.at(i+1);
            }
        }
    }

    return content;
}

/**
  /brief Extract the oembed infos from the XML
  @param xmlUrl as the url of the XML file
  */
sOEmbedContent UBOEmbedParser::getXMLInfos(const QString &xml)
{
    sOEmbedContent content;

    QDomDocument domDoc;
    domDoc.setContent(xml);
    QDomNode oembed = domDoc.documentElement();

    QDomNodeList children = oembed.toElement().childNodes();

    for(int i=0; i<children.size(); i++){
        QDomNode node = children.at(i);
        QString tag = node.nodeName();
        QString value = node.toElement().text();
        if("provider_url" == tag){
            content.providerUrl = value;
        }else if("title" == tag){
            content.title = value;
        }else if("html" == tag){
            content.html = value;
        }else if("author_name" == tag){
            content.author = value;
        }else if("height" == tag){
            content.height = value.toInt();
        }else if("thumbnail_width" == tag){
            content.thumbWidth = value.toInt();
        }else if("width" == tag){
            content.width = value.toInt();
        }else if("version" == tag){
            content.version = value.toFloat();
        }else if("author_url" == tag){
            content.authorUrl = value;
        }else if("provider_name" == tag){
            content.providerName = value;
        }else if("thumbnail_url" == tag){
            content.thumbUrl = value;
        }else if("type" == tag){
            content.type = value;
        }else if("thumbnail_height" == tag){
            content.thumbHeight = value.toInt();
        }else if("url" == tag){
            content.url = value; // This case appears only for type = photo
        }
    }

    if("video" == content.type){
        QStringList strl = content.html.split('\"');
        for(int i=0; i<strl.size(); i++){
            if(strl.at(i).endsWith("src=") && strl.size() > (i+1)){
                content.url = strl.at(i+1);
            }
        }
    }

    return content;
}

void UBOEmbedParser::onParseContent(QString url)
{
231
    QUrl qurl = QUrl::fromEncoded(url.toLatin1());
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

    QNetworkRequest req;
    req.setUrl(qurl);
    if(NULL != mpNam){
        mpNam->get(req);
    }
}

void UBOEmbedParser::onFinished(QNetworkReply *reply)
{
    if(QNetworkReply::NoError == reply->error()){
        QString receivedDatas = reply->readAll().constData();
        sOEmbedContent crntContent;
        // The received datas can be in two different formats: JSON or XML
        if(receivedDatas.contains("<oembed>")){
            // XML !
            crntContent = getXMLInfos(receivedDatas);
        }else if(receivedDatas.contains("{\"provider_url")){
            // JSON !
            crntContent = getJSONInfos(receivedDatas);
        }

        //  As we don't want duplicates, we have to check if the content title has already
        //  been parsed.
        if("" != crntContent.title && !mParsedTitles.contains(crntContent.title)){
            mParsedTitles << crntContent.title;
            mContents << crntContent;
        }

    }else{
        //  We decided to not handle the error case here. If there is a problem with
        //  getting the oembed content information, we just don't handle it: the content
        //  will not be available for importation.
    }

    // Decrement the number of content to analyze
    mPending--;
    if(0 == mPending){
        //  All the oembed contents have been parsed. We notify it!
        emit oembedParsed(mContents);
    }
}