DoViGUI/qmoviedb.cpp
Adam Bissen 28ab770b3e Add handbrake encode progress to UI.
Add encode stop button.
Movie now searches when selected. First result in UI.
2024-01-13 10:56:24 +00:00

65 lines
2.2 KiB
C++

#include "qmoviedb.h"
#include "QNetworkReply"
#include "QDebug"
#include "QJsonDocument"
#include "QJsonObject"
#include "QJsonArray"
QMovieDB::QMovieDB(QString apiKey, QObject *parent)
: QObject{parent}, netMan(new QNetworkAccessManager(this)) {
movieDbApiKey = apiKey;
QObject::connect(netMan, &QNetworkAccessManager::finished, this, &QMovieDB::receiveReply);
}
void QMovieDB::searchMovieTitle(const QString title)
{
QNetworkRequest req;
req.setRawHeader("Accept", "application/json");
QString bearer = "Bearer " + movieDbApiKey;
req.setRawHeader("Authorization", bearer.toUtf8());
QByteArray URL = QUrl::toPercentEncoding(title);
URL = "https://api.themoviedb.org/3/search/movie?query=" + URL + "&include_adult=false&language=en-US&page=1";
req.setUrl(QUrl(URL));
netMan->get(req);
}
//Remove path and extension of filename before searching
void QMovieDB::searchMovieTitleFile(const QString file) {
uint8_t endURI = file.lastIndexOf('/');
uint8_t startExt = file.lastIndexOf('.');
searchMovieTitle(file.sliced(endURI + 1, startExt - endURI - 1));
}
void QMovieDB::receiveReply(QNetworkReply *reply) {
int responseStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (responseStatus == 200) {
parseResults(QJsonDocument::fromJson(reply->readAll()).object());
} else {
qWarning() << "Invalid Response. Status: " << responseStatus;
}
}
void QMovieDB::parseResults(QJsonObject json) {
qInfo() << "Total Results: " << json.value("total_results").toInt();
QJsonArray results = json.value("results").toArray(); //Even if single result, always returns array of result(s).
for (int i = 0; i < results.size(); i++) {
QString releaseYear = results.at(i).toObject().value("release_date").toString();
releaseYear = "(" + releaseYear.left(releaseYear.indexOf("-")) + ")";
QString titleYear = results.at(i).toObject().value("title").toString() + " " + releaseYear;
qInfo() << "Result " << (i + 1) << ": " << titleYear;
//For now only returning first result
if (i == 0) {
emit searchResultReturned(titleYear);
}
}
}