]> git.sur5r.net Git - minitube/blob - src/youtubestreamreader.cpp
Imported Upstream version 1.0
[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                     }
21                 }
22             }
23         }
24     }
25
26     return !error();
27 }
28
29 void YouTubeStreamReader::readMediaGroup() {
30
31 }
32
33 void YouTubeStreamReader::readEntry() {
34     Video* video = new Video();
35     // qDebug(" *** ENTRY ***");
36
37     while (!atEnd()) {
38         readNext();
39
40         /*
41         qDebug() << name();
42         QXmlStreamAttribute attribute;
43         foreach (attribute, attributes())
44             qDebug() << attribute.name() << ":" << attribute.value();
45         */
46
47         if (isEndElement() && name() == "entry") break;
48         if (isStartElement()) {
49
50             if (name() == "link"
51                 && attributes().value("rel").toString() == "alternate"
52                 && attributes().value("type").toString() == "text/html"
53                 ) {
54                 QString webpage = attributes().value("href").toString();
55                 // qDebug() << "Webpage: " << webpage;
56                 video->setWebpage(QUrl(webpage));
57             } else if (name() == "author") {
58                 readNext();
59                 if (name() == "name") {
60                     QString author = readElementText();
61                     // qDebug() << "Author: " << author;
62                     video->setAuthor(author);
63                 }
64             } else if (name() == "published") {
65                 video->setPublished(QDateTime::fromString(readElementText(), Qt::ISODate));
66             } else if (namespaceUri() == "http://gdata.youtube.com/schemas/2007" && name() == "statistics") {
67
68                 QString viewCount = attributes().value("viewCount").toString();
69                 // qDebug() << "viewCount: " << viewCount;
70                 video->setViewCount(viewCount.toInt());
71             }
72             else if (namespaceUri() == "http://search.yahoo.com/mrss/" && name() == "group") {
73
74                 // read media group
75                 while (!atEnd()) {
76                     readNext();
77                     if (isEndElement() && name() == "group") break;
78                     if (isStartElement()) {
79                         if (name() == "thumbnail") {
80                             // qDebug() << "Thumb: " << attributes().value("url").toString();
81                             // video->thumbnailUrls() << QUrl(attributes().value("url").toString());
82                             video->addThumbnailUrl(QUrl(attributes().value("url").toString()));
83                         }
84                         else if (name() == "title") {
85                             QString title = readElementText();
86                             // qDebug() << "Title: " << title;
87                             video->setTitle(title);
88                         }
89                         else if (name() == "description") {
90                             QString desc = readElementText();
91                             // qDebug() << "Description: " << desc;
92                             video->setDescription(desc);
93                         }
94                         else if (name() == "duration") {
95                             QString duration = attributes().value("seconds").toString();
96                             // qDebug() << "Duration: " << duration;
97                             video->setDuration(duration.toInt());
98                         }
99                     }
100                 }
101             }
102         }
103     }
104
105     videos.append(video);
106
107 }
108
109 QList<Video*> YouTubeStreamReader::getVideos() {
110     return videos;
111 }