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