diff --git a/src/fetch/kinofetcher.cpp b/src/fetch/kinofetcher.cpp index 080b5313..63de4236 100644 --- a/src/fetch/kinofetcher.cpp +++ b/src/fetch/kinofetcher.cpp @@ -1,376 +1,381 @@ /*************************************************************************** Copyright (C) 2017 Robby Stephenson ***************************************************************************/ /*************************************************************************** * * * This program is free software; you can redistribute it and/or * * modify it under the terms of the GNU General Public License as * * published by the Free Software Foundation; either version 2 of * * the License or (at your option) version 3 or any later version * * accepted by the membership of KDE e.V. (or its successor approved * * by the membership of KDE e.V.), which shall act as a proxy * * defined in Section 14 of version 3 of the license. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program. If not, see . * * * ***************************************************************************/ #include "kinofetcher.h" #include "../utils/guiproxy.h" #include "../utils/string_utils.h" #include "../collections/bookcollection.h" #include "../collections/videocollection.h" #include "../entry.h" #include "../fieldformat.h" #include "../core/filehandler.h" #include "../images/imagefactory.h" #include "../tellico_debug.h" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace { static const char* KINO_BASE_URL = "https://www.kino.de/se/"; } using namespace Tellico; using Tellico::Fetch::KinoFetcher; KinoFetcher::KinoFetcher(QObject* parent_) : Fetcher(parent_), m_started(false) { } KinoFetcher::~KinoFetcher() { } QString KinoFetcher::source() const { return m_name.isEmpty() ? defaultName() : m_name; } bool KinoFetcher::canFetch(int type) const { return type == Data::Collection::Video; } void KinoFetcher::readConfigHook(const KConfigGroup& config_) { Q_UNUSED(config_); } void KinoFetcher::search() { m_started = true; m_matches.clear(); QUrl u(QString::fromLatin1(KINO_BASE_URL)); QUrlQuery q; q.addQueryItem(QStringLiteral("sp_search_filter"), QStringLiteral("movie")); switch(request().key) { case Title: q.addQueryItem(QStringLiteral("searchterm"), request().value); break; default: myWarning() << "key not recognized: " << request().key; stop(); return; } u.setQuery(q); // myDebug() << "url:" << u; m_job = KIO::storedGet(u, KIO::NoReload, KIO::HideProgressInfo); KJobWidgets::setWindow(m_job, GUI::Proxy::widget()); connect(m_job, SIGNAL(result(KJob*)), SLOT(slotComplete(KJob*))); } void KinoFetcher::stop() { if(!m_started) { return; } if(m_job) { m_job->kill(); m_job = nullptr; } m_started = false; emit signalDone(this); } void KinoFetcher::slotComplete(KJob*) { if(m_job->error()) { m_job->uiDelegate()->showErrorMessage(); stop(); return; } const QByteArray data = m_job->data(); if(data.isEmpty()) { myDebug() << "no data"; stop(); return; } // since the fetch is done, don't worry about holding the job pointer m_job = nullptr; QString s = Tellico::decodeHTML(data); #if 0 myWarning() << "Remove debug from kinofetcher.cpp"; QFile f(QStringLiteral("/tmp/test.html")); if(f.open(QIODevice::WriteOnly)) { QTextStream t(&f); t.setCodec("UTF-8"); t << s; } f.close(); #endif QRegularExpression linkRx(QStringLiteral("Film.+?(.+?)(.+?)addEntries(entry); entry->setField(QStringLiteral("title"), match.captured(2)); QString y; QRegularExpressionMatch dateMatch = dateSpanRx.match(s, match.capturedEnd()); if(dateMatch.hasMatch()) { y = dateRx.match(dateMatch.captured(1)).captured(1); } else { // see if year is embedded in url y = yearEndRx.match(u).captured(1); } entry->setField(QStringLiteral("year"), y); FetchResult* r = new FetchResult(Fetcher::Ptr(this), entry); QUrl url = QUrl(QString::fromLatin1(KINO_BASE_URL)).resolved(QUrl(u)); m_matches.insert(r->uid, url); m_entries.insert(r->uid, entry); // don't emit signal until after putting url in matches hash emit signalResultFound(r); } stop(); } Tellico::Data::EntryPtr KinoFetcher::fetchEntryHook(uint uid_) { if(!m_entries.contains(uid_)) { myWarning() << "no entry in hash"; return Data::EntryPtr(); } Data::EntryPtr entry = m_entries[uid_]; // if the url is not in the hash, the entry has already been fully populated if(!m_matches.contains(uid_)) { return entry; } QString results = Tellico::decodeHTML(FileHandler::readTextFile(m_matches[uid_], true, true)); if(results.isEmpty()) { myDebug() << "no text results from" << m_matches[uid_]; return entry; } #if 0 myWarning() << "Remove debug from kinofetcher.cpp"; QFile f(QStringLiteral("/tmp/test2.html")); if(f.open(QIODevice::WriteOnly)) { QTextStream t(&f); t.setCodec("UTF-8"); t << results; } f.close(); #endif parseEntry(entry, results); // remove url to signal the entry is fully populated m_matches.remove(uid_); return entry; } void KinoFetcher::parseEntry(Data::EntryPtr entry, const QString& str_) { QRegularExpression jsonRx(QStringLiteral("