forked from teroaij/tvkaistagui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channelfeedparser.cpp
92 lines (76 loc) · 1.84 KB
/
channelfeedparser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <QDebug>
#include "channelfeedparser.h"
bool ChannelFeedParser::parse(QIODevice *device)
{
m_reader.setDevice(device);
m_channels.clear();
if (!m_reader.readNextStartElement()) {
m_error = "Invalid channel feed";
return false;
}
if (m_reader.name() != "rss") {
m_error = "Channel feed does not contain rss element";
return false;
}
while (m_reader.readNextStartElement()) {
if (m_reader.name() == "channel") {
parseChannelElement();
}
else {
m_reader.skipCurrentElement();
}
}
return true;
}
QString ChannelFeedParser::lastError() const
{
return m_error;
}
QList<Channel> ChannelFeedParser::channels() const
{
return m_channels;
}
void ChannelFeedParser::parseChannelElement()
{
while (m_reader.readNextStartElement()) {
if (m_reader.name() == "item") {
parseItemElement();
}
else {
m_reader.skipCurrentElement();
}
}
}
void ChannelFeedParser::parseItemElement()
{
int channelId = -1;
QString name;
while (m_reader.readNextStartElement()) {
if (m_reader.name() == "title") {
name = m_reader.readElementText();
}
else if (m_reader.name() == "link") {
channelId = parseChannelId(m_reader.readElementText());
}
else {
m_reader.skipCurrentElement();
}
}
if (channelId >= 0) {
m_channels.append(Channel(channelId, name));
}
}
int ChannelFeedParser::parseChannelId(const QString &s)
{
/* "http://www.tvkaista.com/feed/channels/1004" -> 1004 */
int pos = s.lastIndexOf('/');
if (pos < 0) {
return -1;
}
bool ok;
int channelId = s.mid(pos + 1).toInt(&ok);
if (!ok) {
return -1;
}
return channelId;
}