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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* vdr-vodcatcher - A plugin for the Linux Video Disk Recorder
* Copyright (c) 2007 - 2009 Tobias Grimm <vdr@e-tobi.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "RssFeedParser.h"
#include "HtmlToText.h"
#include "Feed.h"
#include "IDownloadCache.h"
#include "tinyxml.h"
#include "Item.h"
using namespace std;
RssFeedParser::RssFeedParser(const IDownloadCache& downloadCache) :
_downloadCache(downloadCache)
{
}
bool RssFeedParser::Parse(Feed& feed) const
{
RefPtr<istream> source(_downloadCache.CreateStreamByUrl(feed.GetUrl()));
if (source.IsAssigned())
{
RefPtr<TiXmlDocument> rssDoc = CreateRssDocument(*source);
TiXmlHandle docHandle(rssDoc.get());
const TiXmlNode* channelNode = docHandle.FirstChild("rss").FirstChild("channel").Node();
if (channelNode)
{
feed.SetTitle(GetChildValue(*channelNode, "title"));
feed.SetTimeToLive(StringToIntDefault(GetChildValue(*channelNode, "ttl"), feed.GetTimeToLive()));
for (const TiXmlNode* itemNode = channelNode->FirstChild("item"); itemNode; itemNode
= itemNode->NextSibling("item"))
{
Item item = ParseItem(*itemNode);
feed.AddItem(item);
}
return true;
}
}
return false;
}
RefPtr<TiXmlDocument> RssFeedParser::CreateRssDocument(istream& rssStream) const
{
RefPtr<TiXmlDocument> rssDoc(new TiXmlDocument());
rssStream >> *rssDoc;
if (!rssDoc->Error())
{
return rssDoc;
}
return RefPtr<TiXmlDocument>();
}
string RssFeedParser::GetValue(const TiXmlNode& node) const
{
const TiXmlNode* textNode = node.FirstChild();
if (textNode)
{
return textNode->Value();
}
return "";
}
const string RssFeedParser::GetAttribute(const TiXmlElement& element, string name) const
{
const string* value = element.Attribute(name);
if (value != NULL)
{
return *value;
}
else
{
return "";
}
}
string RssFeedParser::GetChildValue(const TiXmlNode& root, const string& childName) const
{
const TiXmlNode* childNode = root.FirstChild(childName);
if (childNode)
{
return GetValue(*childNode);
}
return "";
}
Item RssFeedParser::ParseItem(const TiXmlNode& itemNode) const
{
string title = GetChildValue(itemNode, "title");
string description = GetChildValue(itemNode, "description");
string date = GetChildValue(itemNode, "pubDate");
Item item(title, Rfc822DateTime(date), HtmlToText::Convert(description));
const TiXmlElement* subFeedElement = itemNode.FirstChildElement("videocast:subfeed");
if (subFeedElement)
{
item.SetSubFeedUrl(GetAttribute(*subFeedElement, "url"));
}
const TiXmlElement* enclosureElement = itemNode.FirstChildElement("enclosure");
if (enclosureElement)
{
string type = GetAttribute(*enclosureElement, "type");
if (type.find("video/") == 0)
{
item.SetVideoCastStream(Low, GetAttribute(*enclosureElement, "url"));
}
}
const TiXmlElement* streamElement = itemNode.FirstChildElement("videocast:stream");
while (streamElement)
{
ParseStreamElement(*streamElement, item);
streamElement = streamElement->NextSiblingElement("videocast:stream");
}
const TiXmlElement* streamInfoElement = itemNode.FirstChildElement("videocast:streaminfo");
if (streamInfoElement)
{
item.SetStreamLength(GetAttribute(*streamInfoElement, "length"));
}
return item;
}
void RssFeedParser::ParseStreamElement(const TiXmlElement& streamElement, Item& item) const
{
string quality = GetAttribute(streamElement, "quality");
string url = GetAttribute(streamElement, "url");
if (quality == "high")
{
item.SetVideoCastStream(High, url);
}
else if (quality == "medium")
{
item.SetVideoCastStream(Medium, url);
}
else if (quality == "low")
{
item.SetVideoCastStream(Low, url);
}
}
int RssFeedParser::StringToIntDefault(const string& str, const int defaultValue) const
{
istringstream tempStream(str);
int value;
if (!(tempStream >> value))
{
value = defaultValue;
}
return value;
}
|