diff --git a/qmoviedb.cpp b/qmoviedb.cpp index 44d019b..c88a5d2 100644 --- a/qmoviedb.cpp +++ b/qmoviedb.cpp @@ -1,6 +1,9 @@ #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)) { @@ -23,7 +26,30 @@ void QMovieDB::searchMovieTitle(const QString title) netMan->get(req); } -void QMovieDB::receiveReply(QNetworkReply *reply) -{ - qInfo() << reply->readAll(); +void QMovieDB::receiveReply(QNetworkReply *reply) { + int responseStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + + if (responseStatus == 200) { + parseResults(QJsonDocument::fromJson(reply->readAll()).object()); + } else { + qInfo() << "Invalid Response. Status: " << responseStatus; + } +} + +void QMovieDB::parseResults(QJsonObject json) { + qInfo() << "Total Results: " << json.value("total_results").toInt(); + + QJsonValue results = json.value("results"); + QString releaseYear; + QString titleYear; + + if (results.isArray()) { + releaseYear = results.toArray().first().toObject().value("release_date").toString(); + releaseYear = "(" + releaseYear.left(releaseYear.indexOf("-")) + ")"; + titleYear = results.toArray().first().toObject().value("title").toString() + " " + releaseYear; + } else { + //If single result + } + + qInfo() << titleYear; } diff --git a/qmoviedb.h b/qmoviedb.h index c84e060..9d3691a 100644 --- a/qmoviedb.h +++ b/qmoviedb.h @@ -3,6 +3,7 @@ #include #include +#include class QMovieDB : public QObject { @@ -13,11 +14,13 @@ public: QNetworkAccessManager *netMan; void searchMovieTitle(const QString title); + private: QString movieDbApiKey; private slots: void receiveReply(QNetworkReply *reply); + void parseResults(QJsonObject json); signals: };