forked from isaevi/PodcastLoader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
82 lines (70 loc) · 2.24 KB
/
parser.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
#include <QtCore>
#include <QXmlStreamReader>
#include <QXmlResultItems>
#include <QDomDocument>
#include <QXmlQuery>
#include <QVector>
#include "recordinfo.h"
#include "parser.h"
Parser::Parser()
{
}
QVector<RecordInfo*> Parser::parseXml(QByteArray& data)
{
QString strQuery = R"*(//rss/channel/item/
<record
title="{title/text()}"
description="{description/text}"
date="{pubDate/text()}"
url="{enclosure/@url}"
length="{enclosure/@length}"
type="{enclosure/@type}"
guid="{guid/text()}"/>)*";
QXmlQuery xQuery;
xQuery.setFocus(data);
xQuery.setQuery(strQuery);
QXmlResultItems out;
xQuery.evaluateTo(&out);
QString url, title, date, guid, description, type, length;
int tmp;
QVector<RecordInfo*> records;
QXmlItem item(out.next());
while (!item.isNull())
{
if(item.isNode())
{
xQuery.setFocus(item);
xQuery.setQuery("./@title/string()");
xQuery.evaluateTo(&title);
xQuery.setQuery("./@description/string()");
xQuery.evaluateTo(&description);
xQuery.setQuery("./@date/string()");
xQuery.evaluateTo(&date);
xQuery.setQuery("./@url/string()");
xQuery.evaluateTo(&url);
xQuery.setQuery("./@length/string()");
xQuery.evaluateTo(&length);
tmp = length.toInt(nullptr);
xQuery.setQuery("./@type/string()");
xQuery.evaluateTo(&type);
xQuery.setQuery("./@guid/string()");
xQuery.evaluateTo(&guid);
//ignore records without mp3
if(type.trimmed().compare("audio/mpeg", Qt::CaseInsensitive) != 0)
{
item = out.next();
continue;
}
RecordInfo* rec = new RecordInfo();
rec->setTitle(title.trimmed());
rec->setUrl(url.trimmed());
rec->setDate(date.trimmed());
rec->setGuid(guid.trimmed());
rec->setLength(tmp);
rec->setDescription(description.trimmed());
records.push_back(rec);
}
item = out.next();
}
return records;
}