File indexing completed on 2024-05-12 05:09:30

0001 /***************************************************************************
0002     Copyright (C) 2010 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "dvdfrfetcher.h"
0026 #include "../translators/xslthandler.h"
0027 #include "../translators/tellicoimporter.h"
0028 #include "../utils/string_utils.h"
0029 #include "../tellico_debug.h"
0030 
0031 #include <KLocalizedString>
0032 #include <KConfigGroup>
0033 
0034 #include <QLabel>
0035 #include <QFile>
0036 #include <QTextStream>
0037 #include <QVBoxLayout>
0038 #include <QTextCodec>
0039 #include <QDomDocument>
0040 #include <QUrlQuery>
0041 
0042 namespace {
0043   static const int DVDFR_MAX_RETURNS_TOTAL = 20;
0044   static const char* DVDFR_SEARCH_API_URL = "http://www.dvdfr.com/api/search.php";
0045   static const char* DVDFR_DETAIL_API_URL = "http://www.dvdfr.com/api/dvd.php";
0046 }
0047 
0048 using namespace Tellico;
0049 using Tellico::Fetch::DVDFrFetcher;
0050 
0051 DVDFrFetcher::DVDFrFetcher(QObject* parent_)
0052     : XMLFetcher(parent_) {
0053   setLimit(DVDFR_MAX_RETURNS_TOTAL);
0054   setXSLTFilename(QStringLiteral("dvdfr2tellico.xsl"));
0055 }
0056 
0057 DVDFrFetcher::~DVDFrFetcher() {
0058 }
0059 
0060 QString DVDFrFetcher::source() const {
0061   return m_name.isEmpty() ? defaultName() : m_name;
0062 }
0063 
0064 bool DVDFrFetcher::canSearch(Fetch::FetchKey k) const {
0065   return k == Title || k == UPC;
0066 }
0067 
0068 bool DVDFrFetcher::canFetch(int type) const {
0069   return type == Data::Collection::Video;
0070 }
0071 
0072 QUrl DVDFrFetcher::searchUrl() {
0073   QUrl u(QString::fromLatin1(DVDFR_SEARCH_API_URL));
0074 
0075   QUrlQuery q;
0076   switch(request().key()) {
0077     case Title:
0078       // DVDfr requires the title string to be in iso-8859-15
0079       {
0080         QTextCodec* codec = QTextCodec::codecForName("iso-8859-15");
0081         Q_ASSERT(codec);
0082         q.addQueryItem(QStringLiteral("title"), QString::fromUtf8(codec->fromUnicode(request().value())));
0083       }
0084       break;
0085 
0086     case UPC:
0087       q.addQueryItem(QStringLiteral("gencode"), request().value());
0088       break;
0089 
0090     default:
0091       myWarning() << source() << "- key not recognized:" << request().key();
0092       return QUrl();
0093   }
0094   u.setQuery(q);
0095 //  myDebug() << "url: " << u.url();
0096   return u;
0097 }
0098 
0099 Tellico::Data::EntryPtr DVDFrFetcher::fetchEntryHookData(Data::EntryPtr entry_) {
0100   Q_ASSERT(entry_);
0101 
0102   const QString id = entry_->field(QStringLiteral("dvdfr-id"));
0103   if(id.isEmpty()) {
0104     myDebug() << "no dvdfr id found";
0105     return entry_;
0106   }
0107 
0108   QUrl u(QString::fromLatin1(DVDFR_DETAIL_API_URL));
0109   QUrlQuery q;
0110   q.addQueryItem(QStringLiteral("id"), id);
0111   u.setQuery(q);
0112 //  myDebug() << "url: " << u;
0113 
0114   // quiet
0115   const QString output = FileHandler::readXMLFile(u, true);
0116 
0117 #if 0
0118   myWarning() << "Remove output debug from dvdfrfetcher.cpp";
0119   QFile f(QLatin1String("/tmp/test-dvdfr-details.xml"));
0120   if(f.open(QIODevice::WriteOnly)) {
0121     QTextStream t(&f);
0122     t.setCodec("UTF-8");
0123     t << output;
0124   }
0125   f.close();
0126 #endif
0127 
0128   Import::TellicoImporter imp(xsltHandler()->applyStylesheet(output));
0129   // be quiet when loading images
0130   imp.setOptions(imp.options() ^ Import::ImportShowImageErrors);
0131   Data::CollPtr coll = imp.collection();
0132   if(!coll) {
0133     myWarning() << "no collection pointer";
0134     return entry_;
0135   }
0136   if(coll->entryCount() == 0) {
0137     myWarning() << "no entries";
0138     return entry_;
0139   }
0140 
0141   if(coll->entryCount() > 1) {
0142     myDebug() << "weird, more than one entry found";
0143   }
0144 
0145   // don't want to include id
0146   coll->removeField(QStringLiteral("dvdfr-id"));
0147   return coll->entries().front();
0148 }
0149 
0150 Tellico::Fetch::FetchRequest DVDFrFetcher::updateRequest(Data::EntryPtr entry_) {
0151   QString barcode = entry_->field(QStringLiteral("barcode"));
0152   if(barcode.isEmpty()) {
0153     barcode = entry_->field(QStringLiteral("upc"));
0154   }
0155   if(!barcode.isEmpty()) {
0156     return FetchRequest(UPC, barcode);
0157   }
0158 
0159   const QString title = entry_->field(QStringLiteral("title"));
0160   if(!title.isEmpty()) {
0161     return FetchRequest(Title, title);
0162   }
0163   return FetchRequest();
0164 }
0165 
0166 Tellico::Fetch::ConfigWidget* DVDFrFetcher::configWidget(QWidget* parent_) const {
0167   return new DVDFrFetcher::ConfigWidget(parent_, this);
0168 }
0169 
0170 QString DVDFrFetcher::defaultName() {
0171   return QStringLiteral("DVDFr.com");
0172 }
0173 
0174 QString DVDFrFetcher::defaultIcon() {
0175   return favIcon("https://www.dvdfr.com");
0176 }
0177 
0178 Tellico::StringHash DVDFrFetcher::allOptionalFields() {
0179   StringHash hash;
0180   hash[QStringLiteral("origtitle")] = i18n("Original Title");
0181   hash[QStringLiteral("dvdfr")]     = i18n("DVDFr Link");
0182   hash[QStringLiteral("alttitle")]  = i18n("Alternative Titles");
0183   hash[QStringLiteral("barcode")]   = i18n("Barcode");
0184   return hash;
0185 }
0186 
0187 DVDFrFetcher::ConfigWidget::ConfigWidget(QWidget* parent_, const DVDFrFetcher* fetcher_)
0188     : Fetch::ConfigWidget(parent_) {
0189   QVBoxLayout* l = new QVBoxLayout(optionsWidget());
0190   l->addWidget(new QLabel(i18n("This source has no options."), optionsWidget()));
0191   l->addStretch();
0192 
0193   // now add additional fields widget
0194   addFieldsWidget(DVDFrFetcher::allOptionalFields(), fetcher_ ? fetcher_->optionalFields() : QStringList());
0195 }
0196 
0197 void DVDFrFetcher::ConfigWidget::saveConfigHook(KConfigGroup&) {
0198 }
0199 
0200 QString DVDFrFetcher::ConfigWidget::preferredName() const {
0201   return DVDFrFetcher::defaultName();
0202 }