File indexing completed on 2024-05-05 16:49:44
0001 /* 0002 * Copyright (C) 2014 David Faure <faure@kde.org> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "themoviedbapi.h" 0021 #include "searchjob.h" 0022 #include "tvsearchjob.h" 0023 #include "creditsjob.h" 0024 #include "movieinfojob.h" 0025 #include "tvshowinfojob.h" 0026 #include "tvseasoninfojob.h" 0027 #include "configuration.h" 0028 #include "jobparams_p.h" 0029 #include <QNetworkAccessManager> 0030 #include <QNetworkRequest> 0031 #include <QNetworkReply> 0032 #include <QJsonDocument> 0033 #include <QJsonObject> 0034 #include <QUrl> 0035 #include <QUrlQuery> 0036 using namespace TmdbQt; 0037 0038 class TmdbQt::TheMovieDbApiPrivate 0039 { 0040 public: 0041 TheMovieDbApiPrivate(const QString &apiKey) 0042 : m_apiKey(apiKey) 0043 , m_jobParams(m_qnam, m_configuration) 0044 { 0045 } 0046 0047 QUrl baseUrl() const; 0048 void slotConfigurationReady(TheMovieDbApi *q); 0049 0050 QString m_apiKey; 0051 QNetworkAccessManager m_qnam; 0052 QNetworkReply *m_configurationReply; 0053 Configuration m_configuration; 0054 JobParams m_jobParams; 0055 }; 0056 0057 TheMovieDbApi::TheMovieDbApi(const QString &apiKey) 0058 : d(new TheMovieDbApiPrivate(apiKey)) 0059 { 0060 QUrl url = d->baseUrl(); 0061 d->m_jobParams.setBaseUrl(url); 0062 0063 // See https://developers.themoviedb.org/3/configuration/get-api-configuration 0064 url.setPath(url.path() + QLatin1String("/configuration")); 0065 QNetworkRequest request(url); 0066 d->m_configurationReply = d->m_qnam.get(request); 0067 connect(d->m_configurationReply, &QNetworkReply::finished, 0068 this, [this]() { d->slotConfigurationReady(this); }); 0069 } 0070 0071 TheMovieDbApi::~TheMovieDbApi() 0072 { 0073 delete d; 0074 } 0075 0076 bool TheMovieDbApi::isInitialized() const 0077 { 0078 return d->m_configurationReply == nullptr; 0079 } 0080 0081 SearchJob *TheMovieDbApi::searchMovie(const QString &movieName, int searchYear, const QString &language) 0082 { 0083 return new SearchJob(d->m_jobParams, movieName, searchYear, language); 0084 } 0085 0086 TvSearchJob* TheMovieDbApi::searchTvShow(const QString& tvShowName, int firstAiredYear, const QString& language) 0087 { 0088 return new TvSearchJob(d->m_jobParams, tvShowName, firstAiredYear, language); 0089 } 0090 0091 MovieInfoJob *TheMovieDbApi::getMovieInfo(int movieId) 0092 { 0093 return new MovieInfoJob(d->m_jobParams, movieId); 0094 } 0095 0096 TvShowInfoJob* TheMovieDbApi::getTvShowInfo(int tvshowId) 0097 { 0098 return new TvShowInfoJob(d->m_jobParams, tvshowId); 0099 } 0100 0101 TvSeasonInfoJob* TheMovieDbApi::getTvSeasonInfo(int tvshowId, int seasonNum) 0102 { 0103 return new TvSeasonInfoJob(d->m_jobParams, tvshowId, seasonNum); 0104 } 0105 0106 CreditsJob *TheMovieDbApi::getCredits(int movieId) 0107 { 0108 return new CreditsJob(d->m_jobParams, movieId); 0109 } 0110 0111 CreditsJob *TheMovieDbApi::getEpisodeCredits(int tvShowId, int seasonNum, int episodeNum) 0112 { 0113 return new CreditsJob(d->m_jobParams, tvShowId, seasonNum, episodeNum); 0114 } 0115 0116 Configuration &TheMovieDbApi::configuration() const 0117 { 0118 return d->m_configuration; 0119 } 0120 0121 void TheMovieDbApiPrivate::slotConfigurationReady(TheMovieDbApi *q) 0122 { 0123 if (m_configurationReply->error()) { 0124 qDebug() << "ERROR" << m_configurationReply->errorString(); 0125 } else { 0126 const QByteArray data = m_configurationReply->readAll(); 0127 QJsonDocument doc = QJsonDocument::fromJson(data); 0128 QJsonObject root = doc.object(); 0129 m_configuration.load(root); 0130 Q_EMIT q->initialized(); 0131 } 0132 m_configurationReply->deleteLater(); 0133 m_configurationReply = nullptr; 0134 } 0135 0136 QUrl TheMovieDbApiPrivate::baseUrl() const 0137 { 0138 static const char s_tmdbApiUrl[] = "http://api.themoviedb.org/3"; 0139 QUrl url(QString::fromLatin1(s_tmdbApiUrl)); 0140 QUrlQuery query(url); 0141 query.addQueryItem(QLatin1String("api_key"), m_apiKey); 0142 url.setQuery(query); 0143 return url; 0144 }