From 1490102829314e01e9b983384ea5e4d25c7dd697 Mon Sep 17 00:00:00 2001 From: birkeh Date: Mon, 29 May 2017 16:17:59 +0200 Subject: [PATCH] thetvdb v2 --- cmainwindow.cpp | 1 + csearch.cpp | 4 ++ cthetvdbv2.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++++ cthetvdbv2.h | 24 ++++++++++++ qtSeries.pro | 6 ++- 5 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 cthetvdbv2.cpp create mode 100644 cthetvdbv2.h diff --git a/cmainwindow.cpp b/cmainwindow.cpp index c31eaed..a86c22e 100644 --- a/cmainwindow.cpp +++ b/cmainwindow.cpp @@ -2,6 +2,7 @@ #include "ui_cmainwindow.h" #include "cthetvdb.h" +#include "cthetvdbv2.h" #include "cseasondelegate.h" #include "csearch.h" #include "cmessageanimatedialog.h" diff --git a/csearch.cpp b/csearch.cpp index 6242575..12657bf 100644 --- a/csearch.cpp +++ b/csearch.cpp @@ -2,6 +2,8 @@ #include "ui_csearch.h" #include "cthetvdb.h" +#include "cthetvdbv2.h" + #include "cserie.h" #include "cmessageanimatedialog.h" @@ -38,7 +40,9 @@ void cSearch::on_m_lpSearchButton_clicked() lpDialog->show(); cTheTVDB theTVDB; + cTheTVDBV2 theTVDBV2; QList serieList = theTVDB.search(ui->m_lpSearch->text()); + QList serieList2 = theTVDBV2.search(ui->m_lpSearch->text()); ui->m_lpResults->clear(); diff --git a/cthetvdbv2.cpp b/cthetvdbv2.cpp new file mode 100644 index 0000000..7feb35f --- /dev/null +++ b/cthetvdbv2.cpp @@ -0,0 +1,100 @@ +#include "cthetvdbv2.h" + +#include +#include +#include +#include +#include +#include +#include +#include + + +cTheTVDBV2::cTheTVDBV2() +{ + QByteArray jsonRequest = "{\"apikey\":\"BC0893B659680049\"}"; + QByteArray postDataSize = QByteArray::number(jsonRequest.size()); + QNetworkAccessManager networkManager; + + QNetworkRequest request(QUrl(QString("https://api.thetvdb.com/login"))); + + request.setRawHeader("Content-Type", "application/json"); + request.setRawHeader("Content-Length", postDataSize); + + QNetworkReply* reply = networkManager.post(request, jsonRequest); + QEventLoop loop; + + QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + + if (reply->error() == QNetworkReply::NoError) + { + QString strReply = (QString)reply->readAll(); + QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); + QJsonObject jsonObj = jsonResponse.object(); + m_szToken = jsonObj["token"].toString(); + + delete reply; + } + else + { + qDebug() << reply->errorString(); + delete reply; + } +} + +QList cTheTVDBV2::search(const QString& szSerie, const QString& szLanguage) +{ + QList serieList; + + QNetworkAccessManager networkManager; + + QNetworkRequest request(QUrl(QString("https://api.thetvdb.com/search/series?name=%1").arg(szSerie))); + + request.setRawHeader("Content-Type", "application/json"); + request.setRawHeader("Authorization", QString("Bearer %1").arg(m_szToken).toUtf8()); + if(!szLanguage.contains("all")) + request.setRawHeader("Accept-Language", szLanguage.toUtf8()); + + QNetworkReply* reply = networkManager.get(request); + QEventLoop loop; + + QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + + if (reply->error() == QNetworkReply::NoError) + { + QString strReply = (QString)reply->readAll(); + QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); + QJsonObject jsonObj = jsonResponse.object(); + QJsonArray jsonArray = jsonObj["data"].toArray(); + + for(int z = 0;z < jsonArray.count();z++) + { + QJsonObject serie = jsonArray[z].toObject(); + cSerie* lpSerie = new cSerie; + lpSerie->setID(serie["id"].toInt()); + lpSerie->setSeriesName(serie["seriesName"].toString()); + lpSerie->setFirstAired(serie["firstAired"].toString()); + serieList.append(lpSerie); + } + delete reply; + } + else + { + qDebug() << reply->errorString(); + delete reply; + } + return(serieList); +} + +/* +cSerie* cTheTVDBV2::parseSeries(const QDomElement& element) +{ +} +*/ +/* +cSerie* cTheTVDBV2::load(const qint32 &iID, const QString &szLanguage) +{ +} +*/ diff --git a/cthetvdbv2.h b/cthetvdbv2.h new file mode 100644 index 0000000..56cfa8d --- /dev/null +++ b/cthetvdbv2.h @@ -0,0 +1,24 @@ +#ifndef CTHETVDBV2_H +#define CTHETVDBV2_H + + +#include "cserie.h" + +#include +#include + + +class cTheTVDBV2 +{ +public: + cTheTVDBV2(); + + QList search(const QString& szSerie, const QString& szLanguage = "all"); +// cSerie* load(const qint32 &iID, const QString& szLanguage); +private: + QString m_szToken; +// cSerie* parseSeries(const QDomElement& element); +// void parseEpisode(cSerie* lpSerie, const QDomElement& element); +}; + +#endif // CTHETVDBV2_H diff --git a/qtSeries.pro b/qtSeries.pro index bcbacb9..9fb28cc 100644 --- a/qtSeries.pro +++ b/qtSeries.pro @@ -28,7 +28,8 @@ SOURCES += main.cpp\ cepisodedetails.cpp \ cmessagedialog.cpp \ cupdatethread.cpp \ - cpicturesthread.cpp + cpicturesthread.cpp \ + cthetvdbv2.cpp HEADERS += cmainwindow.h \ cthetvdb.h \ @@ -45,7 +46,8 @@ HEADERS += cmainwindow.h \ cepisodedetails.h \ cmessagedialog.h \ cupdatethread.h \ - cpicturesthread.h + cpicturesthread.h \ + cthetvdbv2.h FORMS += cmainwindow.ui \ csearch.ui \