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
179
180
|
#include "atomitem.h"
#include <QSqlError>
#include <QSqlQuery>
#include <QXmlStreamReader>
#include "atomchannel.h"
#include "constants.h"
#include "macros.h"
#include "rsshit_db.h"
AtomItem::AtomItem(QXmlStreamReader *xmlReader)
{
Q_ASSERT(xmlReader != nullptr);
const QString titleTag{"title"};
const QString linkTag{"link"};
const QString categoryTag{"category"};
const QString guidTag{"guid"};
const QString pubDateTag{"pubDate"};
const QString descriptionTag{"description"};
const QString encodedTag{"encoded"};
while (!xmlReader->atEnd() && !xmlReader->hasError()) {
const auto itemNext = xmlReader->readNext();
switch (itemNext) {
case QXmlStreamReader::TokenType::StartElement: {
const auto name = xmlReader->name();
const auto elementText = xmlReader->readElementText();
if (name == titleTag)
title = elementText;
else if (name == linkTag)
link = elementText;
else if (name == categoryTag)
categories.append(elementText);
else if (name == guidTag)
guid = AtomGuid{elementText};
else if (name == pubDateTag)
pubDate = QDateTime::fromString(elementText, Qt::DateFormat::RFC2822Date);
else if (name == descriptionTag)
description = elementText;
else if (name == encodedTag)
encoded = elementText;
break;
}
case QXmlStreamReader::TokenType::EndElement:
// qDebug() << "EndElement: " << xmlReader->name();
return;
case QXmlStreamReader::TokenType::Characters:
const auto characters = xmlReader->text().toString().simplified();
if (characters.isEmpty())
break;
qDebug() << "item: characters: " << characters;
break;
}
}
}
int AtomItem::getDbId()
{
if (id != rsshit::db::IdNotFound)
return id;
const auto db = rsshit::db::open();
if (!db)
return rsshit::db::IdNotFound;
QSqlQuery selectQ{"select id from items where link=?"};
selectQ.addBindValue(link);
if (!selectQ.exec()) {
qWarning() << "cannot exec query" << selectQ.lastQuery() << ":"
<< selectQ.lastError().text();
return rsshit::db::IdNotFound;
}
if (!selectQ.next())
return rsshit::db::IdNotFound;
const auto idVariant = selectQ.value(rsshit::db::idTag);
if (!idVariant.isValid() || !idVariant.canConvert<int>())
return rsshit::db::IdNotFound;
bool ok{false};
const auto result = idVariant.toInt(&ok);
if (!ok) {
qWarning() << "got invalid id from db:" << idVariant;
return rsshit::db::IdNotFound;
}
return result;
}
int AtomItem::createInDb(const int feedId)
{
if (id != rsshit::db::IdNotFound)
return id;
const auto db = rsshit::db::open();
if (!db)
return rsshit::db::IdNotFound;
QSqlQuery insertQ{
"insert into items(feed_fk, pub_datetime_unix, title, link, author, description)"
"values(?, ?, ?, ?, ?, ?)"};
insertQ.addBindValue(feedId);
insertQ.addBindValue(pubDate.toSecsSinceEpoch());
insertQ.addBindValue(title);
insertQ.addBindValue(link);
insertQ.addBindValue(author);
insertQ.addBindValue(description);
if (!insertQ.exec()) {
qWarning() << "cannot exec query" << insertQ.lastQuery() << ":"
<< insertQ.lastError().text();
return rsshit::db::IdNotFound;
}
return insertQ.lastInsertId().toInt();
}
int AtomItem::getOrInsertDbId(const int feedId)
{
const auto id = getDbId();
if (id != rsshit::db::IdNotFound)
return id;
return createInDb(feedId);
}
QDebug operator<<(QDebug debug, const AtomItem &item)
{
QDebugStateSaver saver{debug};
debug.nospace() << typeid(AtomItem).name() << " {" << Qt::endl;
PRINT_ATOM_FIELD(item, title);
PRINT_ATOM_FIELD(item, link);
PRINT_ATOM_FIELD(item, categories);
PRINT_ATOM_FIELD(item, guid);
PRINT_ATOM_FIELD(item, pubDate);
// PRINT_ATOM_ITEM_FIELD(description);
// PRINT_ATOM_ITEM_FIELD(encoded);
auto halfSize = item.description.size() / 2;
constexpr decltype(halfSize) maxLeft{70};
constexpr decltype(halfSize) maxRight{20};
auto left = std::min(maxLeft, halfSize);
auto right = std::min(maxRight, halfSize);
debug.nospace().noquote() << "\tdescription: \"" << item.description.left(left) << "..."
<< item.description.right(right) << '"' << Qt::endl;
halfSize = item.encoded.size() / 2;
left = std::min(maxLeft, halfSize);
right = std::min(maxRight, halfSize);
debug.nospace().noquote() << "\tencoded: \"" << item.encoded.left(left) << "..."
<< item.encoded.right(right) << '"' << Qt::endl;
debug.nospace() << "}";
return debug;
}
|