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