-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractmoviedetailservice.cpp
224 lines (181 loc) · 7.72 KB
/
abstractmoviedetailservice.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "abstractmoviedetailservice.h"
#include <QDebug>
#include <QJsonObject>
#include <QtSql/QSqlError>
#include <QtSql/QSqlQuery>
#include "torrentsqltablemodel.h"
#include "types/moviedetail.h"
#include "utils/fs.h"
AbstractMovieDetailService::AbstractMovieDetailService(TorrentSqlTableModel *const model,
QObject *const parent)
: QObject(parent)
, m_model(model)
{}
SearchMovieResult
AbstractMovieDetailService::getSearchMovieDetail(const QSqlRecord &torrent,
const bool skipCache) const
{
const auto torrentId = torrent.value("id").toULongLong();
// Try to fetch from DB
if (!skipCache) {
auto movieDetail = selectSearchedMovieDetailByTorrentId(torrentId);
if (!movieDetail.isEmpty())
return movieDetail;
}
auto movieDetail = searchMovieDetail(torrent);
// Save movie detail to the cache
m_searchMovieCache.insert(torrentId, movieDetail);
// Also save to obtained movie detail cache make sense
m_movieDetailsCache.insert(
movieDetail["detail"]["id"].toInt(),
QJsonDocument(movieDetail["detail"].toObject()));
// Save to DB
saveSearchedMovieDetailToDb(torrent, movieDetail);
return movieDetail;
}
MovieDetailResult AbstractMovieDetailService::getMovieDetail(quint64 filmId) const
{
// Return from cache
if (m_movieDetailsCache.contains(filmId)) {
qDebug() << "Obtained movie detail obtained from cache, film :" << filmId;
return m_movieDetailsCache.value(filmId);
}
auto movieDetail = obtainMovieDetail(filmId);
// Save movie detail to cache
m_movieDetailsCache.insert(movieDetail["id"].toInt(), movieDetail);
return movieDetail;
}
// TODO run operation like this in thread, QtConcurent::run() is ideal silverqx
bool AbstractMovieDetailService::updateSearchMovieDetailInDb(
const QSqlRecord &torrent, const MovieDetail &movieDetail,
const MovieSearchResults &movieSearchResult, const int movieDetailComboBoxIndex) const
{
const auto torrentId = torrent.value("id").toULongLong();
// Prepare movie detail json as QString
auto movieDetailNew = SearchMovieResult({
{"detail", movieDetail},
{"search", movieSearchResult}
});
const auto movieDetailDocument = QJsonDocument(movieDetailNew);
const auto movieDetailString = QString::fromStdString(
movieDetailDocument.toJson(QJsonDocument::Compact)
.toStdString());
// Save to cache
m_searchMovieCache.insert(torrentId, movieDetailDocument);
const auto torrentRow = m_model->getTorrentRowByInfoHash(torrent.value("hash").toString());
m_model->setData(m_model->index(torrentRow,
TorrentSqlTableModel::TR_MOVIE_DETAIL_INDEX),
movieDetailComboBoxIndex);
m_model->setData(m_model->index(torrentRow,
TorrentSqlTableModel::TR_CSFD_MOVIE_DETAIL),
movieDetailString);
if (!m_model->submit()) {
qDebug("Update of a movie detail for the torrent(ID%llu) failed : %s",
torrentId, qUtf8Printable(m_model->lastError().text()));
return false;
}
qDebug() << "Searched movie detail updated in db, torrent :"
<< torrentId;
return true;
}
MovieDetailResult
AbstractMovieDetailService::parseMovieDetail(const QByteArray &movieDetailRaw) const
{
auto movieDetail = QJsonDocument::fromJson(movieDetailRaw);
if (movieDetail.isEmpty() || movieDetail.isNull() || !movieDetail.isObject())
return {};
return movieDetail;
}
SearchMovieResult
AbstractMovieDetailService::parseSearchedMovieDetail(const QByteArray &movieDetailRaw) const
{
auto movieDetail = QJsonDocument::fromJson(movieDetailRaw);
if (movieDetail.isEmpty() || movieDetail.isNull() || !movieDetail.isObject() ||
!movieDetail.object().contains("search") ||
!movieDetail.object().contains("detail")
)
return {};
return movieDetail;
}
QString AbstractMovieDetailService::getMovieScrapperPath() const
{
#ifdef QMEDIA_DEBUG
return Utils::Fs::toNativePath("E:/c/qMedia/qMedia/movies_scrapper/src/index.js");
#else
return Utils::Fs::toNativePath(QCoreApplication::applicationDirPath() +
"/movies_scrapper/src/index.js");
#endif
}
QString AbstractMovieDetailService::prepareSearchQueryString(const QSqlRecord &torrent) const
{
// TODO tune search query string regexp silverqx
return torrent.value("name").toString()
.replace(QChar('.'), QChar(' '))
.replace(QRegularExpression(" {2,}"), QLatin1String(" "));
}
SearchMovieResult
AbstractMovieDetailService::selectSearchedMovieDetailByTorrentId(const quint64 id) const
{
// Return from the cache
if (m_searchMovieCache.contains(id)) {
qDebug() << "Searched movie detail obtained from cache, torrent :" << id;
return m_searchMovieCache.value(id);
}
QSqlQuery query;
query.setForwardOnly(true);
query.prepare(QStringLiteral("SELECT CAST(%1 AS CHAR) as detail FROM torrents WHERE id = ?")
.arg(getMovieDetailColumnName()));
query.addBindValue(id);
if (!query.exec()) {
qDebug("Select a movie detail from the database for the torrent(ID%llu) failed : %s",
id, qUtf8Printable(query.lastError().text()));
return {};
}
// Check return query size
if (const auto querySize = query.size(); querySize != 1) {
// TODO decide how to handle this type of situations, asserts vs exceptions, ... silverqx
qWarning().noquote()
<< QStringLiteral(
"Select a movie detail from the databasefor the torrent(ID%1) "
"doesn't have size 1, current size is : %2")
.arg(id, querySize);
return {};
}
// Get movie detail
query.first();
const auto movieDetailRaw = query.record().value("detail");
if (!movieDetailRaw.isValid() || movieDetailRaw.isNull())
return {};
// In db is saved searched movie detail
auto movieDetail = parseSearchedMovieDetail(movieDetailRaw.toByteArray());
// Save movie detail to cache
m_searchMovieCache.insert(id, movieDetail);
// Also save to obtained movie detail cache make sense
m_movieDetailsCache.insert(
movieDetail["detail"]["id"].toInt(),
MovieDetailResult(movieDetail["detail"].toObject()));
qDebug() << "Searched movie detail obtained from db, torrent :" << id;
return movieDetail;
}
void AbstractMovieDetailService::saveSearchedMovieDetailToDb(
const QSqlRecord &torrent, const SearchMovieResult &movieDetail) const
{
const auto movieDetailString = QString::fromStdString(
movieDetail.toJson(QJsonDocument::Compact)
.toStdString());
const auto torrentRow =
m_model->getTorrentRowByInfoHash(torrent.value("hash").toString());
m_model->setData(m_model->index(torrentRow,
TorrentSqlTableModel::TR_MOVIE_DETAIL_INDEX),
0);
m_model->setData(m_model->index(torrentRow,
TorrentSqlTableModel::TR_CSFD_MOVIE_DETAIL),
movieDetailString);
const auto torrentId = torrent.value("id").toULongLong();
if (!m_model->submit()) {
qDebug("Update of a movie detail for the torrent(ID%llu) failed : %s",
torrentId, qUtf8Printable(m_model->lastError().text()));
return;
}
qDebug() << "Searched movie detail saved to db, torrent :" << torrentId;
}