TheTVDB V2

This commit is contained in:
2017-05-29 21:30:29 +02:00
parent 1490102829
commit dd434fa89a
7 changed files with 234 additions and 280 deletions
+4 -7
View File
@@ -1,7 +1,6 @@
#include "cmainwindow.h"
#include "ui_cmainwindow.h"
#include "cthetvdb.h"
#include "cthetvdbv2.h"
#include "cseasondelegate.h"
#include "csearch.h"
@@ -653,9 +652,11 @@ void cMainWindow::onActionAdd()
if(id == -1)
return;
cTheTVDB tvDB;
cTheTVDBV2 tvDB2;
lpSerie = tvDB.load(id, "de");
lpSerie = tvDB2.load(id, "de");
if(!lpSerie)
lpSerie = tvDB2.load(id, "en");
delete lpDialog;
@@ -668,11 +669,7 @@ void cMainWindow::onActionAdd()
lpDialog->setMessage("Updating");
lpDialog->show();
// lpSerie->setDownload(szDownload);
// lpSerie->updateState();
lpSerie->save(m_db);
}
else
{
+4 -5
View File
@@ -41,24 +41,23 @@ void cSearch::on_m_lpSearchButton_clicked()
cTheTVDB theTVDB;
cTheTVDBV2 theTVDBV2;
QList<cSerie*> serieList = theTVDB.search(ui->m_lpSearch->text());
QList<cSerie*> serieList2 = theTVDBV2.search(ui->m_lpSearch->text());
ui->m_lpResults->clear();
for(int z = 0;z < serieList.count();z++)
for(int z = 0;z < serieList2.count();z++)
{
cSerie* lpSerie = serieList.at(z);
cSerie* lpSerie = serieList2.at(z);
QTreeWidgetItem* lpNew = new QTreeWidgetItem(ui->m_lpResults);
lpNew->setText(0, lpSerie->seriesName());
lpNew->setText(1, lpSerie->language());
lpNew->setText(2, QString("%1").arg(lpSerie->firstAired().year()));
lpNew->setData(0, Qt::UserRole, QVariant::fromValue(lpSerie->id()));
ui->m_lpResults->addTopLevelItem(lpNew);
}
ui->m_lpResults->resizeColumnToContents(0);
ui->m_lpResults->resizeColumnToContents(1);
ui->m_lpResults->resizeColumnToContents(2);
ui->m_lpResults->sortItems(0, Qt::AscendingOrder);
delete lpDialog;
}
-230
View File
@@ -1,230 +0,0 @@
#include "cthetvdb.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QEventLoop>
#include <QUrlQuery>
#include <QBuffer>
cTheTVDB::cTheTVDB()
{
}
// BC0893B659680049
// Mirrors: http://thetvdb.com/api/BC0893B659680049/mirrors.xml
// Server time: http://thetvdb.com/api/Updates.php?type=none
// Search: http://thetvdb.com/api/GetSeries.php?seriesname=outer limits&language=all
// Episodes: http://thetvdb.com/api/BC0893B659680049/series/72224/all/de.xml
// Banner: http://thetvdb.com/banners/graphical/77092-g3.jpg // 758 x 140
// Banner: http://thetvdb.com/banners/graphical/77544-g2.jpg
// Banner: http://thetvdb.com/banners/graphical/210841-g10.jpg
QList<cSerie*> cTheTVDB::search(const QString& szSerie, const QString& szLanguage)
{
QList<cSerie*> serieList;
QNetworkAccessManager networkManager;
cSerie* lpNew;
QNetworkRequest request(QUrl(QString("http://thetvdb.com/api/GetSeries.php?seriesname=%1&language=%2").arg(szSerie).arg(szLanguage)));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/xml");
QNetworkReply* reply = networkManager.get(request);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QByteArray szData = reply->readAll();
delete reply;
QDomDocument doc;
QString errorStr;
int errorLine;
int errorColumn;
if(!doc.setContent(szData, false, &errorStr, &errorLine, &errorColumn))
return(serieList);
QDomElement root = doc.documentElement();
if(root.tagName().compare("Data", Qt::CaseInsensitive))
return(serieList);
QDomNode child = root.firstChild();
while(!child.isNull())
{
if(!child.toElement().tagName().compare("Series", Qt::CaseInsensitive))
{
lpNew = parseSeries(child.toElement());
if(lpNew)
serieList.append(lpNew);
}
child = child.nextSibling();
}
return(serieList);
}
cSerie* cTheTVDB::parseSeries(const QDomElement& element)
{
cSerie* lpNew = new cSerie;
QDomNode child = element.firstChild();
while(!child.isNull())
{
if(!child.toElement().tagName().compare("SeriesName", Qt::CaseInsensitive))
lpNew->setSeriesName(child.toElement().text());
else if(!child.toElement().tagName().compare("seriesid", Qt::CaseInsensitive))
lpNew->setSeriesID(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("language", Qt::CaseInsensitive))
lpNew->setLanguage(child.toElement().text());
else if(!child.toElement().tagName().compare("banner", Qt::CaseInsensitive))
lpNew->setBanner(child.toElement().text());
else if(!child.toElement().tagName().compare("Overview", Qt::CaseInsensitive))
lpNew->setOverview(child.toElement().text());
else if(!child.toElement().tagName().compare("FirstAired", Qt::CaseInsensitive))
lpNew->setFirstAired(child.toElement().text());
else if(!child.toElement().tagName().compare("Network", Qt::CaseInsensitive))
lpNew->setNetwork(child.toElement().text());
else if(!child.toElement().tagName().compare("IMDB_ID", Qt::CaseInsensitive))
lpNew->setIMDBID(child.toElement().text());
else if(!child.toElement().tagName().compare("id", Qt::CaseInsensitive))
lpNew->setID(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("Actors", Qt::CaseInsensitive))
lpNew->setActors(child.toElement().text());
else if(!child.toElement().tagName().compare("ContentRating", Qt::CaseInsensitive))
lpNew->setContentRating(child.toElement().text());
else if(!child.toElement().tagName().compare("Genre", Qt::CaseInsensitive))
lpNew->setGenre(child.toElement().text());
else if(!child.toElement().tagName().compare("Rating", Qt::CaseInsensitive))
lpNew->setRating(child.toElement().text().toDouble());
else if(!child.toElement().tagName().compare("RatingCount", Qt::CaseInsensitive))
lpNew->setRatingCount(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("Runtime", Qt::CaseInsensitive))
lpNew->setRuntime(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("Status", Qt::CaseInsensitive))
lpNew->setStatus(child.toElement().text());
child = child.nextSibling();
}
if(lpNew->isValid())
return(lpNew);
delete lpNew;
return(0);
}
cSerie* cTheTVDB::load(const qint32 &iID, const QString &szLanguage)
{
cSerie* lpSerie = 0;
QNetworkAccessManager networkManager;
QNetworkRequest request(QUrl(QString("http://thetvdb.com/api/BC0893B659680049/series/%1/all/%2.xml").arg(iID).arg(szLanguage)));
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/xml");
QNetworkReply* reply = networkManager.get(request);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
QByteArray szData = reply->readAll();
delete reply;
QDomDocument doc;
QString errorStr;
int errorLine;
int errorColumn;
if(!doc.setContent(szData, false, &errorStr, &errorLine, &errorColumn))
return(lpSerie);
QDomElement root = doc.documentElement();
if(root.tagName().compare("Data", Qt::CaseInsensitive))
return(lpSerie);
QDomNode child = root.firstChild();
while(!child.isNull())
{
if(!child.toElement().tagName().compare("Series", Qt::CaseInsensitive))
{
lpSerie = parseSeries(child.toElement());
if(!lpSerie)
return(lpSerie);
}
else if(!child.toElement().tagName().compare("Episode", Qt::CaseInsensitive))
parseEpisode(lpSerie, child.toElement());
child = child.nextSibling();
}
return(lpSerie);
}
void cTheTVDB::parseEpisode(cSerie* lpSerie, const QDomElement& element)
{
cSeason* lpSeason = 0;
cEpisode* lpEpisode = new cEpisode;
QDomNode child = element.firstChild();
while(!child.isNull())
{
if(!child.toElement().tagName().compare("id", Qt::CaseInsensitive))
lpEpisode->setID(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("Director", Qt::CaseInsensitive))
lpEpisode->setDirector(child.toElement().text());
else if(!child.toElement().tagName().compare("EpisodeName", Qt::CaseInsensitive))
lpEpisode->setEpisodeName(child.toElement().text());
else if(!child.toElement().tagName().compare("EpisodeNumber", Qt::CaseInsensitive))
lpEpisode->setEpisodeNumber(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("FirstAired", Qt::CaseInsensitive))
lpEpisode->setFirstAired(child.toElement().text());
else if(!child.toElement().tagName().compare("GuestStars", Qt::CaseInsensitive))
lpEpisode->setGuestStars(child.toElement().text());
else if(!child.toElement().tagName().compare("IMDB_ID", Qt::CaseInsensitive))
lpEpisode->setIMDBID(child.toElement().text());
else if(!child.toElement().tagName().compare("Language", Qt::CaseInsensitive))
lpEpisode->setLanguage(child.toElement().text());
else if(!child.toElement().tagName().compare("Overview", Qt::CaseInsensitive))
lpEpisode->setOverview(child.toElement().text());
else if(!child.toElement().tagName().compare("ProductionCode", Qt::CaseInsensitive))
lpEpisode->setProductionCode(child.toElement().text());
else if(!child.toElement().tagName().compare("Rating", Qt::CaseInsensitive))
lpEpisode->setRating(child.toElement().text().toDouble());
else if(!child.toElement().tagName().compare("RatingCount", Qt::CaseInsensitive))
lpEpisode->setRatingCount(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("SeasonNumber", Qt::CaseInsensitive))
lpEpisode->setSeasonNumber(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("Writer", Qt::CaseInsensitive))
lpEpisode->setWriter(child.toElement().text());
else if(!child.toElement().tagName().compare("seasonid", Qt::CaseInsensitive))
lpEpisode->setSeasonID(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("seriesid", Qt::CaseInsensitive))
lpEpisode->setSeriesID(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("filename", Qt::CaseInsensitive))
lpEpisode->setFileName(child.toElement().text());
else if(!child.toElement().tagName().compare("thumb_height", Qt::CaseInsensitive))
lpEpisode->setThumbHeight(child.toElement().text().toInt());
else if(!child.toElement().tagName().compare("thumb_width", Qt::CaseInsensitive))
lpEpisode->setThumbWidth(child.toElement().text().toInt());
child = child.nextSibling();
}
if(!lpEpisode->isValid())
{
delete lpEpisode;
return;
}
lpSeason = lpSerie->findSeason(lpEpisode->seasonNumber());
if(!lpSeason)
lpSeason = lpSerie->addSeason(lpEpisode->seasonNumber());
if(!lpSeason)
{
delete lpEpisode;
return;
}
lpSeason->addEpisode(lpEpisode);
}
-24
View File
@@ -1,24 +0,0 @@
#ifndef CTHETVDB_H
#define CTHETVDB_H
#include "cserie.h"
#include <QList>
#include <QStringList>
#include <QDomDocument>
class cTheTVDB
{
public:
cTheTVDB();
QList<cSerie*> search(const QString& szSerie, const QString& szLanguage = "all");
cSerie* load(const qint32 &iID, const QString& szLanguage);
private:
cSerie* parseSeries(const QDomElement& element);
void parseEpisode(cSerie* lpSerie, const QDomElement& element);
};
#endif // CTHETVDB_H
+222 -9
View File
@@ -46,15 +46,15 @@ cTheTVDBV2::cTheTVDBV2()
QList<cSerie*> cTheTVDBV2::search(const QString& szSerie, const QString& szLanguage)
{
QList<cSerie*> 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());
else
request.setRawHeader("Accept-Language", "en");
QNetworkReply* reply = networkManager.get(request);
QEventLoop loop;
@@ -88,13 +88,226 @@ QList<cSerie*> cTheTVDBV2::search(const QString& szSerie, const QString& szLangu
return(serieList);
}
/*
cSerie* cTheTVDBV2::parseSeries(const QDomElement& element)
{
}
*/
/*
cSerie* cTheTVDBV2::load(const qint32 &iID, const QString &szLanguage)
{
cSerie* lpSerie = 0;
QNetworkAccessManager networkManager;
QNetworkRequest request(QUrl(QString("https://api.thetvdb.com/series/%1").arg(iID)));
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());
else
request.setRawHeader("Accept-Language", "en");
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();
QJsonObject serieDetails = jsonObj["data"].toObject();
QJsonArray genreArray = serieDetails["genre"].toArray();
QStringList genreList;
delete reply;
lpSerie = new cSerie;
lpSerie->setSeriesName(serieDetails["seriesName"].toString());
lpSerie->setSeriesID(serieDetails["seriesId"].toInt());
lpSerie->setLanguage(szLanguage);
lpSerie->setBanner(serieDetails["banner"].toString());
lpSerie->setOverview(serieDetails["overview"].toString());
lpSerie->setFirstAired(serieDetails["firstAired"].toString());
lpSerie->setNetwork(serieDetails["network"].toString());
lpSerie->setIMDBID(serieDetails["imdbId"].toString());
lpSerie->setID(serieDetails["id"].toInt());
lpSerie->setContentRating(serieDetails["rating"].toString());
for(int z = 0;z < genreArray.count();z++)
genreList.append(genreArray[z].toString());
lpSerie->setGenre(genreList);
lpSerie->setRating(serieDetails["siteRating"].toDouble());
lpSerie->setRatingCount(serieDetails["siteRatingCount"].toInt());
lpSerie->setRuntime(serieDetails["runtime"].toString().toInt());
lpSerie->setStatus(serieDetails["status"].toString());
lpSerie->setActors(getActors(iID));
getEpisodes(lpSerie, szLanguage);
}
else
{
qDebug() << reply->errorString();
delete reply;
}
return(lpSerie);
}
QStringList cTheTVDBV2::getActors(const qint32& iID)
{
QStringList actorsList;
QNetworkAccessManager networkManager;
QNetworkRequest request(QUrl(QString("https://api.thetvdb.com/series/%1/actors").arg(iID)));
request.setRawHeader("Content-Type", "application/json");
request.setRawHeader("Authorization", QString("Bearer %1").arg(m_szToken).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 actorsArray = jsonObj["data"].toArray();
for(int z = 0;z < actorsArray.count();z++)
{
QJsonObject actor = actorsArray.at(z).toObject();
actorsList.append(actor["name"].toString());
}
delete reply;
}
else
{
qDebug() << reply->errorString();
delete reply;
}
return(actorsList);
}
void cTheTVDBV2::getEpisodes(cSerie* lpSerie, const QString& szLanguage)
{
QNetworkAccessManager networkManager;
QNetworkRequest request(QUrl(QString("https://api.thetvdb.com/series/%1/episodes").arg(lpSerie->id())));
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());
else
request.setRawHeader("Accept-Language", "en");
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 episodesArray = jsonObj["data"].toArray();
for(int z = 0;z < episodesArray.count();z++)
{
QJsonObject episode = episodesArray.at(z).toObject();
cEpisode* lpEpisode = getEpisode(episode["id"].toInt(), szLanguage);
cSeason* lpSeason = 0;
if(lpEpisode)
{
lpSeason = lpSerie->findSeason(lpEpisode->seasonNumber());
if(!lpSeason)
lpSeason = lpSerie->addSeason(lpEpisode->seasonNumber());
if(!lpSeason)
delete lpEpisode;
else
lpSeason->addEpisode(lpEpisode);
}
}
delete reply;
}
else
{
qDebug() << reply->errorString();
delete reply;
}
}
cEpisode* cTheTVDBV2::getEpisode(const qint32& iID, const QString& szLanguage)
{
cEpisode* lpEpisode = 0;
QNetworkAccessManager networkManager;
QNetworkRequest request(QUrl(QString("https://api.thetvdb.com/episodes/%1").arg(iID)));
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());
else
request.setRawHeader("Accept-Language", "en");
QNetworkReply* reply = networkManager.get(request);
QEventLoop loop;
QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();
if (reply->error() == QNetworkReply::NoError)
{
lpEpisode = new cEpisode;
QString strReply = (QString)reply->readAll();
QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
QJsonObject episodeObj = jsonObj["data"].toObject();
QJsonArray writerArray = episodeObj["writers"].toArray();
QStringList writerList;
QJsonArray guestArray = episodeObj["guestStars"].toArray();
QStringList guestList;
lpEpisode->setID(episodeObj["id"].toInt());
lpEpisode->setDirector(episodeObj["director"].toString());
lpEpisode->setEpisodeName(episodeObj["episodeName"].toString());
lpEpisode->setEpisodeNumber(episodeObj["airedEpisodeNumber"].toInt());
lpEpisode->setFirstAired(episodeObj["firstAired"].toString());
for(int z = 0;z < guestArray.count();z++)
guestList.append(guestArray[z].toString());
lpEpisode->setGuestStars(guestList);
lpEpisode->setIMDBID(episodeObj["imdbId"].toString()); // IMDBID
lpEpisode->setLanguage(szLanguage);
lpEpisode->setOverview(episodeObj["overview"].toString());
lpEpisode->setProductionCode(episodeObj["productionCode"].toString()); // PRORUCTION CODE
lpEpisode->setRating(episodeObj["siteRating"].toDouble()); // RATING
lpEpisode->setRatingCount(episodeObj["siteRatingCount"].toInt()); // RATING COUNT
lpEpisode->setSeasonNumber(episodeObj["airedSeason"].toInt());
for(int z = 0;z < writerArray.count();z++)
writerList.append(writerArray[z].toString());
lpEpisode->setWriter(writerList);
lpEpisode->setSeasonID(episodeObj["airedSeasonID"].toInt()); // SEASON ID
lpEpisode->setSeriesID(episodeObj["seriesId"].toInt()); // SERIES ID
lpEpisode->setFileName(episodeObj["filename"].toString()); // FILENAME
lpEpisode->setThumbHeight(episodeObj["thumbHeight"].toString().toInt());
lpEpisode->setThumbWidth(episodeObj["thumbWidth"].toString().toInt());
if(!lpEpisode->isValid())
{
delete lpEpisode;
return(0);
}
return(lpEpisode);
}
else
{
qDebug() << reply->errorString();
delete reply;
return(0);
}
}
*/
+4 -3
View File
@@ -14,11 +14,12 @@ public:
cTheTVDBV2();
QList<cSerie*> search(const QString& szSerie, const QString& szLanguage = "all");
// cSerie* load(const qint32 &iID, const QString& szLanguage);
cSerie* load(const qint32 &iID, const QString& szLanguage);
private:
QString m_szToken;
// cSerie* parseSeries(const QDomElement& element);
// void parseEpisode(cSerie* lpSerie, const QDomElement& element);
QStringList getActors(const qint32& iID);
void getEpisodes(cSerie* lpSerie, const QString& szLanguage);
cEpisode* getEpisode(const qint32& iID, const QString& szLanguage);
};
#endif // CTHETVDBV2_H
-2
View File
@@ -14,7 +14,6 @@ TEMPLATE = app
SOURCES += main.cpp\
cmainwindow.cpp \
cthetvdb.cpp \
cserie.cpp \
cseasondelegate.cpp \
cseason.cpp \
@@ -32,7 +31,6 @@ SOURCES += main.cpp\
cthetvdbv2.cpp
HEADERS += cmainwindow.h \
cthetvdb.h \
cserie.h \
cseasondelegate.h \
cseason.h \