]> git.sur5r.net Git - minitube/blob - src/ytfeedreader.cpp
Imported Upstream version 2.0
[minitube] / src / ytfeedreader.cpp
1 #include "ytfeedreader.h"
2 #include "video.h"
3
4 YTFeedReader::YTFeedReader(const QByteArray &bytes) : QXmlStreamReader(bytes) {
5     while (!atEnd()) {
6         readNext();
7         if (isStartElement() && name() == QLatin1String("entry")) {
8             readEntry();
9         } else if (name() == QLatin1String("link")
10                    && attributes().value("rel").toString()
11                    == QLatin1String("http://schemas.google.com/g/2006#spellcorrection")) {
12             suggestions << attributes().value("title").toString();
13         }
14     }
15 }
16
17 void YTFeedReader::readEntry() {
18     Video* video = new Video();
19
20     while (!atEnd()) {
21         readNext();
22
23         /*
24         qDebug() << name();
25         QXmlStreamAttribute attribute;
26         foreach (attribute, attributes())
27             qDebug() << attribute.name() << ":" << attribute.value();
28         */
29
30         if (isEndElement() && name() == QLatin1String("entry")) break;
31         if (isStartElement()) {
32
33             if (name() == QLatin1String("link")
34                     && attributes().value("rel").toString() == QLatin1String("alternate")
35                     && attributes().value("type").toString() == QLatin1String("text/html")
36                     ) {
37                 QString webpage = attributes().value("href").toString();
38                 webpage.remove("&feature=youtube_gdata");
39                 video->setWebpage(QUrl(webpage));
40             } else if (name() == QLatin1String("author")) {
41                 while(readNextStartElement())
42                     if (name() == QLatin1String("name")) {
43                         QString author = readElementText();
44                         video->setAuthor(author);
45                     } else if (name() == QLatin1String("uri")) {
46                         QString uri = readElementText();
47                         int i = uri.lastIndexOf('/');
48                         if (i != -1) uri = uri.mid(i+1);
49                         video->setAuthorUri(uri);
50                     } else skipCurrentElement();
51             } else if (name() == QLatin1String("published")) {
52                 video->setPublished(QDateTime::fromString(readElementText(), Qt::ISODate));
53             } else if (namespaceUri() == QLatin1String("http://gdata.youtube.com/schemas/2007")
54                        && name() == QLatin1String("statistics")) {
55                 QString viewCount = attributes().value("viewCount").toString();
56                 video->setViewCount(viewCount.toInt());
57             }
58             else if (namespaceUri() == QLatin1String("http://search.yahoo.com/mrss/")
59                      && name() == QLatin1String("group")) {
60
61                 // read media group
62                 while (!atEnd()) {
63                     readNext();
64                     if (isEndElement() && name() == QLatin1String("group")) break;
65                     if (isStartElement()) {
66                         if (name() == QLatin1String("thumbnail")) {
67                             // qDebug() << "Thumb: " << attributes().value("url").toString();
68                             QStringRef name = attributes().value("yt:name");
69                             if (name == QLatin1String("mqdefault"))
70                                 video->setThumbnailUrl(
71                                             attributes().value("url").toString());
72                             else if (name == QLatin1String("hqdefault"))
73                                 video->setMediumThumbnailUrl(
74                                             attributes().value("url").toString());
75                         }
76                         else if (name() == QLatin1String("title")) {
77                             QString title = readElementText();
78                             // qDebug() << "Title: " << title;
79                             video->setTitle(title);
80                         }
81                         else if (name() == QLatin1String("description")) {
82                             QString desc = readElementText();
83                             // qDebug() << "Description: " << desc;
84                             video->setDescription(desc);
85                         }
86                         else if (name() == QLatin1String("duration")) {
87                             QString duration = attributes().value("seconds").toString();
88                             // qDebug() << "Duration: " << duration;
89                             video->setDuration(duration.toInt());
90                         }
91                     }
92                 }
93             }
94         }
95     }
96
97     videos.append(video);
98
99 }
100
101 const QList<Video *> &YTFeedReader::getVideos() {
102     return videos;
103 }
104
105 const QStringList & YTFeedReader::getSuggestions() const {
106     return suggestions;
107 }