]> git.sur5r.net Git - minitube/blob - src/ytfeedreader.cpp
Categories, region selection, refactoring
[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() == "entry") {
8             readEntry();
9         } else if (name() == "link"
10                    && attributes().value("rel").toString()
11                    == "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() == "entry") break;
31         if (isStartElement()) {
32
33             if (name() == "link"
34                     && attributes().value("rel").toString() == "alternate"
35                     && attributes().value("type").toString() == "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() == "author") {
41                 while(readNextStartElement())
42                     if (name() == "name") {
43                         QString author = readElementText();
44                         video->setAuthor(author);
45                     } else if (name() == "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() == "published") {
52                 video->setPublished(QDateTime::fromString(readElementText(), Qt::ISODate));
53             } else if (namespaceUri() == "http://gdata.youtube.com/schemas/2007"
54                        && name() == "statistics") {
55                 QString viewCount = attributes().value("viewCount").toString();
56                 video->setViewCount(viewCount.toInt());
57             }
58             else if (namespaceUri() == "http://search.yahoo.com/mrss/" && name() == "group") {
59
60                 // read media group
61                 while (!atEnd()) {
62                     readNext();
63                     if (isEndElement() && name() == "group") break;
64                     if (isStartElement()) {
65                         if (name() == "thumbnail") {
66                             // qDebug() << "Thumb: " << attributes().value("url").toString();
67                             QStringRef name = attributes().value("yt:name");
68                             if (name == "default")
69                                 video->setThumbnailUrl(
70                                             attributes().value("url").toString());
71                             else if (name == "hqdefault")
72                                 video->setMediumThumbnailUrl(
73                                             attributes().value("url").toString());
74                         }
75                         else if (name() == "title") {
76                             QString title = readElementText();
77                             // qDebug() << "Title: " << title;
78                             video->setTitle(title);
79                         }
80                         else if (name() == "description") {
81                             QString desc = readElementText();
82                             // qDebug() << "Description: " << desc;
83                             video->setDescription(desc);
84                         }
85                         else if (name() == "duration") {
86                             QString duration = attributes().value("seconds").toString();
87                             // qDebug() << "Duration: " << duration;
88                             video->setDuration(duration.toInt());
89                         }
90                     }
91                 }
92             }
93         }
94     }
95
96     videos.append(video);
97
98 }
99
100 const QList<Video *> &YTFeedReader::getVideos() {
101     return videos;
102 }
103
104 const QStringList & YTFeedReader::getSuggestions() const {
105     return suggestions;
106 }