]> git.sur5r.net Git - minitube/blob - src/youtubestreamreader.cpp
Imported Upstream version 1.9
[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                 // qDebug() << "Webpage: " << webpage;
61                 video->setWebpage(QUrl(webpage));
62
63             } else if (name() == "author") {
64                 readNext();
65                 if (name() == "name") {
66                     QString author = readElementText();
67                     // qDebug() << "Author: " << author;
68                     video->setAuthor(author);
69                 }
70             } else if (name() == "published") {
71                 video->setPublished(QDateTime::fromString(readElementText(), Qt::ISODate));
72             } else if (namespaceUri() == "http://gdata.youtube.com/schemas/2007" && name() == "statistics") {
73
74                 QString viewCount = attributes().value("viewCount").toString();
75                 // qDebug() << "viewCount: " << viewCount;
76                 video->setViewCount(viewCount.toInt());
77             }
78             else if (namespaceUri() == "http://search.yahoo.com/mrss/" && name() == "group") {
79
80                 // read media group
81                 while (!atEnd()) {
82                     readNext();
83                     if (isEndElement() && name() == "group") break;
84                     if (isStartElement()) {
85                         if (name() == "thumbnail") {
86                             // qDebug() << "Thumb: " << attributes().value("url").toString();
87                             // video->thumbnailUrls() << QUrl(attributes().value("url").toString());
88                             video->addThumbnailUrl(QUrl(attributes().value("url").toString()));
89                         }
90                         else if (name() == "title") {
91                             QString title = readElementText();
92                             // qDebug() << "Title: " << title;
93                             video->setTitle(title);
94                         }
95                         else if (name() == "description") {
96                             QString desc = readElementText();
97                             // qDebug() << "Description: " << desc;
98                             video->setDescription(desc);
99                         }
100                         else if (name() == "duration") {
101                             QString duration = attributes().value("seconds").toString();
102                             // qDebug() << "Duration: " << duration;
103                             video->setDuration(duration.toInt());
104                         }
105                     }
106                 }
107             }
108         }
109     }
110
111     videos.append(video);
112
113 }
114
115 QList<Video*> YouTubeStreamReader::getVideos() {
116     return videos;
117 }
118
119 const QStringList & YouTubeStreamReader::getSuggestions() const {
120     return suggestions;
121 }