]> git.sur5r.net Git - minitube/blob - src/ytfeedreader.cpp
Imported Upstream version 2.4
[minitube] / src / ytfeedreader.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "ytfeedreader.h"
22 #include "video.h"
23
24 YTFeedReader::YTFeedReader(const QByteArray &bytes) : QXmlStreamReader(bytes) {
25     while (!atEnd()) {
26         readNext();
27         if (isStartElement() && name() == QLatin1String("entry")) {
28             readEntry();
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();
33         }
34     }
35 }
36
37 void YTFeedReader::readEntry() {
38     Video* video = new Video();
39
40     while (!atEnd()) {
41         readNext();
42
43         /*
44         qDebug() << name();
45         QXmlStreamAttribute attribute;
46         foreach (attribute, attributes())
47             qDebug() << attribute.name() << ":" << attribute.value();
48         */
49
50         if (isEndElement() && name() == QLatin1String("entry")) break;
51         if (isStartElement()) {
52
53             if (name() == QLatin1String("link")
54                     && attributes().value("rel").toString() == QLatin1String("alternate")
55                     && attributes().value("type").toString() == QLatin1String("text/html")
56                     ) {
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());
75             }
76             else if (namespaceUri() == QLatin1String("http://search.yahoo.com/mrss/")
77                      && name() == QLatin1String("group")) {
78
79                 // read media group
80                 while (!atEnd()) {
81                     readNext();
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());
93                         }
94                         else if (name() == QLatin1String("title")) {
95                             QString title = readElementText();
96                             // qDebug() << "Title: " << title;
97                             video->setTitle(title);
98                         }
99                         else if (name() == QLatin1String("description")) {
100                             QString desc = readElementText();
101                             // qDebug() << "Description: " << desc;
102                             video->setDescription(desc);
103                         }
104                         else if (name() == QLatin1String("duration")) {
105                             QString duration = attributes().value("seconds").toString();
106                             // qDebug() << "Duration: " << duration;
107                             video->setDuration(duration.toInt());
108                         }
109                         else if (name() == QLatin1String("license")) {
110                             QString license = readElementText();
111                             // qDebug() << "License: " << license;
112                             if (license == QLatin1String("cc"))
113                                 video->setLicense(Video::LicenseCC);
114                         }
115                     }
116                 }
117             }
118         }
119     }
120
121     videos.append(video);
122
123 }
124
125 const QList<Video *> &YTFeedReader::getVideos() {
126     return videos;
127 }
128
129 const QStringList & YTFeedReader::getSuggestions() const {
130     return suggestions;
131 }