3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "ytfeedreader.h"
24 YTFeedReader::YTFeedReader(const QByteArray &bytes) : QXmlStreamReader(bytes) {
27 if (isStartElement() && name() == QLatin1String("entry")) {
29 } else if (name() == QLatin1String("link")
30 && attributes().value("rel").toString()
31 == QLatin1String("http://schemas.google.com/g/2006#spellcorrection")) {
32 suggestions << attributes().value("title").toString();
37 void YTFeedReader::readEntry() {
38 Video* video = new Video();
45 QXmlStreamAttribute attribute;
46 foreach (attribute, attributes())
47 qDebug() << attribute.name() << ":" << attribute.value();
50 if (isEndElement() && name() == QLatin1String("entry")) break;
51 if (isStartElement()) {
53 if (name() == QLatin1String("link")
54 && attributes().value("rel").toString() == QLatin1String("alternate")
55 && attributes().value("type").toString() == QLatin1String("text/html")
57 QString webpage = attributes().value("href").toString();
58 webpage.remove("&feature=youtube_gdata");
59 video->setWebpage(webpage);
60 } else if (name() == QLatin1String("author")) {
61 while(readNextStartElement())
62 if (name() == QLatin1String("name")) {
63 QString author = readElementText();
64 video->setChannelTitle(author);
65 } else if (name() == QLatin1String("userId")) {
66 QString userId = readElementText();
67 video->setChannelId(userId);
68 } else skipCurrentElement();
69 } else if (name() == QLatin1String("published")) {
70 video->setPublished(QDateTime::fromString(readElementText(), Qt::ISODate));
71 } else if (namespaceUri() == QLatin1String("http://gdata.youtube.com/schemas/2007")
72 && name() == QLatin1String("statistics")) {
73 QString viewCount = attributes().value("viewCount").toString();
74 video->setViewCount(viewCount.toInt());
76 else if (namespaceUri() == QLatin1String("http://search.yahoo.com/mrss/")
77 && name() == QLatin1String("group")) {
82 if (isEndElement() && name() == QLatin1String("group")) break;
83 if (isStartElement()) {
84 if (name() == QLatin1String("thumbnail")) {
85 // qDebug() << "Thumb: " << attributes().value("url").toString();
86 QStringRef name = attributes().value("yt:name");
87 if (name == QLatin1String("mqdefault"))
88 video->setThumbnailUrl(
89 attributes().value("url").toString());
90 else if (name == QLatin1String("hqdefault"))
91 video->setMediumThumbnailUrl(
92 attributes().value("url").toString());
94 else if (name() == QLatin1String("title")) {
95 QString title = readElementText();
96 // qDebug() << "Title: " << title;
97 video->setTitle(title);
99 else if (name() == QLatin1String("description")) {
100 QString desc = readElementText();
101 // qDebug() << "Description: " << desc;
102 video->setDescription(desc);
104 else if (name() == QLatin1String("duration")) {
105 QString duration = attributes().value("seconds").toString();
106 // qDebug() << "Duration: " << duration;
107 video->setDuration(duration.toInt());
109 else if (name() == QLatin1String("license")) {
110 QString license = readElementText();
111 // qDebug() << "License: " << license;
112 if (license == QLatin1String("cc"))
113 video->setLicense(Video::LicenseCC);
121 videos.append(video);
125 const QList<Video *> &YTFeedReader::getVideos() {
129 const QStringList & YTFeedReader::getSuggestions() const {