Exemple #1
0
void YT3ListParser::parseItem(const QScriptValue &item) {
    Video *video = new Video();

    QScriptValue id = item.property("id");
    if (id.isString()) video->setId(id.toString());
    else {
        QString videoId = id.property("videoId").toString();
        video->setId(videoId);
    }

    QScriptValue snippet = item.property("snippet");

    bool isLiveBroadcastContent = snippet.property("liveBroadcastContent").toString() != QLatin1String("none");
    if (isLiveBroadcastContent) {
        delete video;
        return;
    }

    QString publishedAt = snippet.property("publishedAt").toString();
    QDateTime publishedDateTime = QDateTime::fromString(publishedAt, Qt::ISODate);
    video->setPublished(publishedDateTime);

    video->setChannelId(snippet.property("channelId").toString());

    video->setTitle(snippet.property("title").toString());
    video->setDescription(snippet.property("description").toString());

    QScriptValue thumbnails = snippet.property("thumbnails");
    video->setThumbnailUrl(thumbnails.property("medium").property("url").toString());
    video->setMediumThumbnailUrl(thumbnails.property("high").property("url").toString());

    video->setChannelTitle(snippet.property("channelTitle").toString());

    // These are only for "videos" requests

    QScriptValue contentDetails = item.property("contentDetails");
    if (contentDetails.isObject()) {
        QString isoPeriod = contentDetails.property("duration").toString();
        int duration = DataUtils::parseIsoPeriod(isoPeriod);
        video->setDuration(duration);
    }

    QScriptValue statistics = item.property("statistics");
    if (statistics.isObject()) {
        uint viewCount = statistics.property("viewCount").toUInt32();
        video->setViewCount(viewCount);
    }

    videos.append(video);
}
Exemple #2
0
void YTFeedReader::readEntry() {
    Video* video = new Video();

    while (!atEnd()) {
        readNext();

        /*
        qDebug() << name();
        QXmlStreamAttribute attribute;
        foreach (attribute, attributes())
            qDebug() << attribute.name() << ":" << attribute.value();
        */

        if (isEndElement() && name() == QLatin1String("entry")) break;
        if (isStartElement()) {

            if (name() == QLatin1String("link")
                    && attributes().value("rel").toString() == QLatin1String("alternate")
                    && attributes().value("type").toString() == QLatin1String("text/html")
                    ) {
                QString webpage = attributes().value("href").toString();
                webpage.remove("&feature=youtube_gdata");
                video->setWebpage(webpage);
            } else if (name() == QLatin1String("author")) {
                while(readNextStartElement())
                    if (name() == QLatin1String("name")) {
                        QString author = readElementText();
                        video->setChannelTitle(author);
                    } else if (name() == QLatin1String("userId")) {
                        QString userId = readElementText();
                        video->setChannelId(userId);
                    } else skipCurrentElement();
            } else if (name() == QLatin1String("published")) {
                video->setPublished(QDateTime::fromString(readElementText(), Qt::ISODate));
            } else if (namespaceUri() == QLatin1String("http://gdata.youtube.com/schemas/2007")
                       && name() == QLatin1String("statistics")) {
                QString viewCount = attributes().value("viewCount").toString();
                video->setViewCount(viewCount.toInt());
            }
            else if (namespaceUri() == QLatin1String("http://search.yahoo.com/mrss/")
                     && name() == QLatin1String("group")) {

                // read media group
                while (!atEnd()) {
                    readNext();
                    if (isEndElement() && name() == QLatin1String("group")) break;
                    if (isStartElement()) {
                        if (name() == QLatin1String("thumbnail")) {
                            // qDebug() << "Thumb: " << attributes().value("url").toString();
                            QStringRef name = attributes().value("yt:name");
                            if (name == QLatin1String("mqdefault"))
                                video->setThumbnailUrl(
                                            attributes().value("url").toString());
                            else if (name == QLatin1String("hqdefault"))
                                video->setMediumThumbnailUrl(
                                            attributes().value("url").toString());
                        }
                        else if (name() == QLatin1String("title")) {
                            QString title = readElementText();
                            // qDebug() << "Title: " << title;
                            video->setTitle(title);
                        }
                        else if (name() == QLatin1String("description")) {
                            QString desc = readElementText();
                            // qDebug() << "Description: " << desc;
                            video->setDescription(desc);
                        }
                        else if (name() == QLatin1String("duration")) {
                            QString duration = attributes().value("seconds").toString();
                            // qDebug() << "Duration: " << duration;
                            video->setDuration(duration.toInt());
                        }
                        else if (name() == QLatin1String("license")) {
                            QString license = readElementText();
                            // qDebug() << "License: " << license;
                            if (license == QLatin1String("cc"))
                                video->setLicense(Video::LicenseCC);
                        }
                    }
                }
            }
        }
    }

    videos.append(video);

}