void JavaScriptEnclosureRequest::onRequestError(const QString &errorString) {
    Logger::log("JavaScriptEnclosureRequest::onRequestError(): " + errorString);
    setErrorString(errorString);
    setResult(Enclosure());
    setStatus(Error);
    emit finished(this);
}
void JavaScriptEnclosureRequest::onRequestFinished(const QVariantMap &result) {
    Logger::log("JavaScriptEnclosureRequest::onRequestFinished()", Logger::MediumVerbosity);
    const QString fileName = result.value("fileName").toString();
    const QVariantMap request = result.value("request").toMap();
    const QUrl url = request.value("url").toString();

    if ((!fileName.isEmpty()) && (!url.isEmpty())) {
        QNetworkRequest req(url);
        
        if (request.contains("headers")) {
            QMapIterator<QString, QVariant> iterator(request.value("headers").toMap());
            
            while (iterator.hasNext()) {
                iterator.next();
                req.setRawHeader(iterator.key().toUtf8(), iterator.value().toByteArray());
            }
        }

        m_result.fileName = fileName;
        m_result.request = req;
        m_result.operation = result.value("operation", "GET").toByteArray();
        m_result.data = result.value("data").toByteArray();

        setErrorString(QString());
        setStatus(Ready);
    }
    else {
        setErrorString(tr("Filename or URL is empty"));
        setResult(Enclosure());
        setStatus(Error);
    }
    
    emit finished(this);
}
bool JavaScriptEnclosureRequest::getEnclosure(const QString &url, const QVariantMap &settings) {
    if (status() == Active) {
        return false;
    }
    
    initEngine();
    QScriptValue func = m_engine->globalObject().property("getEnclosure");

    if (func.isFunction()) {
        const QScriptValue result = func.call(QScriptValue(), QScriptValueList() << url
                                                                                 << m_engine->toScriptValue(settings));

        if (result.isError()) {
            const QString errorString = result.toString();
            Logger::log("JavaScriptEnclosureRequest::getEnclosure(). Error calling getEnclosure(): " + errorString);
            setErrorString(errorString);
            setResult(Enclosure());
            setStatus(Error);
            emit finished(this);
            return false;
        }

        if (result.toBool()) {
            setErrorString(QString());
            setStatus(Active);
            return true;
        }
    }
    else {
        Logger::log("JavaScriptEnclosureRequest::getEnclosure(). getEnclosure() function not defined");
        setErrorString(tr("getEnclosure() function not defined"));
        setResult(Enclosure());
        setStatus(Error);
        emit finished(this);
    }

    return false;
}
Пример #4
0
QList<Enclosure> Item::enclosures() const
{
    QList<QDomElement> encs = elementsByTagNameNS(QString(),
            QLatin1String("enclosure"));

    QList<Enclosure> enclosures;

    QList<QDomElement>::ConstIterator it = encs.constBegin();
    for ( ; it != encs.constEnd(); ++it)
    {
        enclosures.append(Enclosure(*it));
    }
    return enclosures;
}
Пример #5
0
Enclosure Enclosure::fromXML(const QDomElement& e)
{
    QString url, type;
    int length = -1;

    if (e.hasAttribute(QString::fromLatin1("url")))
        url = e.attribute(QString::fromLatin1("url"));
    
    if (e.hasAttribute(QString::fromLatin1("length")))
    {
        bool ok;
        int c = e.attribute(QString::fromLatin1("length")).toInt(&ok);
        length = ok ? c : -1;
    }
    if (e.hasAttribute(QString::fromLatin1("type")))
        type = e.attribute(QString::fromLatin1("type"));

    return Enclosure(url, length, type);
}
Пример #6
0
bool YouTubeEnclosureRequest::getEnclosure(const QString &url, const QVariantMap &settings) {
    if (status() == Active) {
        return false;
    }

    const QString videoId = url.section(QRegExp("v=|list=|/"), -1).section(QRegExp("&|\\?"), 0, 0);
    
    
    if (videoId.isEmpty()) {
        setErrorString(tr("Cannot extract video ID from URL"));
        setResult(Enclosure());
        setStatus(Error);
        emit finished(this);
        return false;
    }

    m_settings = settings;
    m_settings["videoId"] = videoId;
    setStatus(Active);
    getStreams();
    return true;
}
Пример #7
0
void YouTubeEnclosureRequest::checkStreams() {
    if (m_streamsRequest->status() == QYouTube::StreamsRequest::Ready) {
        const QVariantList items = m_streamsRequest->result().toList();
        const int start = qMax(0, VIDEO_FORMATS.indexOf(m_settings.value("videoFormat", "22").toString()));
        
        for (int i = start; i < VIDEO_FORMATS.size(); i++) {
            foreach (const QVariant &item, items) {
                const QVariantMap stream = item.toMap();

                if (stream.value("id") == VIDEO_FORMATS.at(i)) {
                    m_result.request = QNetworkRequest(stream.value("url").toString());
                    getVideo();
                    return;
                }
            }
        }

        setErrorString(tr("No video streams found"));
        setResult(Enclosure());
        setStatus(Error);
        emit finished(this);
    }
    else if (m_streamsRequest->status() == QYouTube::StreamsRequest::Failed) {
Пример #8
0
QList<Message> ParsingFactory::parseAsATOM10(const QString &data) {
  QList<Message> messages;
  QDomDocument xml_file;
  QDateTime current_time = QDateTime::currentDateTime();

  xml_file.setContent(data, true);

  // Pull out all messages.
  QDomNodeList messages_in_xml = xml_file.elementsByTagName(QSL("entry"));

  for (int i = 0; i < messages_in_xml.size(); i++) {
    QDomNode message_item = messages_in_xml.item(i);
    Message new_message;

    // Deal with titles & descriptions.
    QString elem_title = message_item.namedItem(QSL("title")).toElement().text().simplified();
    QString elem_summary = message_item.namedItem(QSL("summary")).toElement().text();

    if (elem_summary.isEmpty()) {
      elem_summary = message_item.namedItem(QSL("content")).toElement().text();
    }

    // Now we obtained maximum of information for title & description.
    if (elem_title.isEmpty()) {
      if (elem_summary.isEmpty()) {
        // BOTH title and description are empty, skip this message.
        continue;
      }
      else {
        // Title is empty but description is not.
        new_message.m_title = WebFactory::instance()->stripTags(elem_summary.simplified());
        new_message.m_contents = elem_summary;
      }
    }
    else {
      // Title is not empty, description does not matter.
      new_message.m_title = WebFactory::instance()->stripTags(elem_title);
      new_message.m_contents = elem_summary;
    }

    // Deal with link.
    QDomNodeList elem_links = message_item.toElement().elementsByTagName(QSL("link"));

    for (int i = 0; i < elem_links.size(); i++) {
      QDomElement link = elem_links.at(i).toElement();

      if (link.attribute(QSL("rel")) == QSL("enclosure")) {
        new_message.m_enclosures.append(Enclosure(link.attribute(QSL("href")), link.attribute(QSL("type"))));

        qDebug("Adding enclosure '%s' for the message.", qPrintable(new_message.m_enclosures.last().m_url));
      }
      else {
        new_message.m_url = link.attribute(QSL("href"));
      }
    }

    if (new_message.m_url.isEmpty() && !new_message.m_enclosures.isEmpty()) {
      new_message.m_url = new_message.m_enclosures.first().m_url;
    }

    // Deal with authors.
    new_message.m_author = WebFactory::instance()->escapeHtml(message_item.namedItem(QSL("author")).namedItem(QSL("name")).toElement().text());

    // Deal with creation date.
    new_message.m_created = TextFactory::parseDateTime(message_item.namedItem(QSL("updated")).toElement().text());
    new_message.m_createdFromFeed = !new_message.m_created.isNull();

    if (!new_message.m_createdFromFeed) {
      // Date was NOT obtained from the feed, set current date as creation date for the message.
      new_message.m_created = current_time;
    }

    // WARNING: There is a difference between "" and QString() in terms of nullptr SQL values!
    // This is because of difference in QString::isNull() and QString::isEmpty(), the "" is not null
    // while QString() is.
    if (new_message.m_author.isNull()) {
      new_message.m_author = "";
    }

    if (new_message.m_url.isNull()) {
      new_message.m_url = "";
    }

    messages.append(new_message);
  }

  return messages;
}
Пример #9
0
QList<Message> ParsingFactory::parseAsRSS20(const QString &data) {
  QList<Message> messages;
  QDomDocument xml_file;
  QDateTime current_time = QDateTime::currentDateTime();

  xml_file.setContent(data, true);

  // Pull out all messages.
  QDomNodeList messages_in_xml = xml_file.elementsByTagName(QSL("item"));

  for (int i = 0; i < messages_in_xml.size(); i++) {
    QDomNode message_item = messages_in_xml.item(i);
    Message new_message;

    // Deal with titles & descriptions.
    QString elem_title = message_item.namedItem(QSL("title")).toElement().text().simplified();
    QString elem_description = message_item.namedItem(QSL("encoded")).toElement().text();
    QString elem_enclosure = message_item.namedItem(QSL("enclosure")).toElement().attribute(QSL("url"));
    QString elem_enclosure_type = message_item.namedItem(QSL("enclosure")).toElement().attribute(QSL("type"));

    if (elem_description.isEmpty()) {
      elem_description = message_item.namedItem(QSL("description")).toElement().text();
    }

    // Now we obtained maximum of information for title & description.
    if (elem_title.isEmpty()) {
      if (elem_description.isEmpty()) {
        // BOTH title and description are empty, skip this message.
        continue;
      }
      else {
        // Title is empty but description is not.
        new_message.m_title = WebFactory::instance()->stripTags(elem_description.simplified());
        new_message.m_contents = elem_description;
      }
    }
    else {
      // Title is really not empty, description does not matter.
      new_message.m_title = WebFactory::instance()->stripTags(elem_title);
      new_message.m_contents = elem_description;
    }

    if (!elem_enclosure.isEmpty()) {
      new_message.m_enclosures.append(Enclosure(elem_enclosure, elem_enclosure_type));

      qDebug("Adding enclosure '%s' for the message.", qPrintable(elem_enclosure));
    }

    // Deal with link and author.
    new_message.m_url = message_item.namedItem(QSL("link")).toElement().text();

    if (new_message.m_url.isEmpty() && !new_message.m_enclosures.isEmpty()) {
      new_message.m_url = new_message.m_enclosures.first().m_url;
    }

    if (new_message.m_url.isEmpty()) {
      // Try to get "href" attribute.
      new_message.m_url = message_item.namedItem(QSL("link")).toElement().attribute(QSL("href"));
    }

    new_message.m_author = message_item.namedItem(QSL("author")).toElement().text();

    if (new_message.m_author.isEmpty()) {
      new_message.m_author = message_item.namedItem(QSL("creator")).toElement().text();
    }

    // Deal with creation date.
    new_message.m_created = TextFactory::parseDateTime(message_item.namedItem(QSL("pubDate")).toElement().text());

    if (new_message.m_created.isNull()) {
      new_message.m_created = TextFactory::parseDateTime(message_item.namedItem(QSL("date")).toElement().text());
    }

    if (!(new_message.m_createdFromFeed = !new_message.m_created.isNull())) {
      // Date was NOT obtained from the feed,
      // set current date as creation date for the message.
      new_message.m_created = current_time;
    }

    if (new_message.m_author.isNull()) {
      new_message.m_author = "";
    }

    if (new_message.m_url.isNull()) {
      new_message.m_url = "";
    }

    messages.append(new_message);
  }

  return messages;
}