Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
bugwelle committed Apr 27, 2023
1 parent 3b21e8f commit 48c0db6
Show file tree
Hide file tree
Showing 63 changed files with 483 additions and 314 deletions.
4 changes: 2 additions & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ endif()
# ------------------------------------------------------------------------------
# Sources
if(Qt6_FOUND)
target_sources(libmediaelch PUBLIC ../ui_qt6.qrc)
target_sources(libmediaelch PUBLIC ../ui_qt6.qrc)
else()
target_sources(libmediaelch PUBLIC ../ui_qt5.qrc)
target_sources(libmediaelch PUBLIC ../ui_qt5.qrc)
endif()
target_sources(libmediaelch PUBLIC ../data/MediaElch.qrc)
target_sources(libmediaelch PRIVATE qml/AlbumImageProvider.cpp)
Expand Down
15 changes: 9 additions & 6 deletions src/data/movie/MovieController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,21 +162,24 @@ void MovieController::loadData(QHash<mediaelch::scraper::MovieScraper*, mediaelc

const MovieIdentifier id = ids.constBegin().value();

MovieScrapeJob::Config config;
config.details = details;
config.locale = locale;
config.identifier = id;

MovieScraper* scraper = nullptr;
MovieScrapeJob* scrapeJob = nullptr;

if (ids.size() > 1) {
// Requires custom movie scraper
CustomMovieScraper::instance()->setScraperMovieIds(std::move(ids));
scraper = CustomMovieScraper::instance();
// Only needed for details list.
MovieScrapeJob::Config config;
config.details = details;
scrapeJob = scraper->loadMovie(config);

} else {
MovieScrapeJob::Config config;
config.details = details;
config.locale = locale;
config.identifier = id;

scraper = ids.constBegin().key();
const auto scraperId = scraper->meta().identifier;
const bool isImdbId = ImdbId::isValidFormat(id.str());
Expand All @@ -193,7 +196,7 @@ void MovieController::loadData(QHash<mediaelch::scraper::MovieScraper*, mediaelc

connect(scrapeJob, &MovieScrapeJob::loadFinished, this, [this, scraper](MovieScrapeJob* job) { //
job->deleteLater();
copyDetailsToMovie(*m_movie, job->movie(), job->config().details);
copyDetailsToMovie(*m_movie, job->movie(), job->config().details, Settings::instance()->usePlotForOutline());
scraperLoadDone(scraper, job);
});
scrapeJob->start();
Expand Down
2 changes: 2 additions & 0 deletions src/scrapers/ScraperInfos.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <QSet>
#include <QString>

// TODO: Rename Info -> Detail

// clang-format: off

enum class MovieScraperInfo : int
Expand Down
14 changes: 11 additions & 3 deletions src/scrapers/movie/MovieMerger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace mediaelch {
namespace scraper {

static void copyDetailToMovie(Movie& target, const Movie& source, MovieScraperInfo detail)
static void copyDetailToMovie(Movie& target, const Movie& source, MovieScraperInfo detail, bool usePlotForOutline)
{
if (source.tmdbId().isValid()) {
target.setTmdbId(source.tmdbId());
Expand Down Expand Up @@ -52,6 +52,11 @@ static void copyDetailToMovie(Movie& target, const Movie& source, MovieScraperIn
}
case MovieScraperInfo::Overview: {
target.setOverview(source.overview());
if (source.outline().isEmpty()) {
target.setOutline(source.outline());
} else if (usePlotForOutline) {
target.setOutline(source.overview());
}
break;
}
case MovieScraperInfo::Poster: {
Expand Down Expand Up @@ -171,10 +176,13 @@ static void copyDetailToMovie(Movie& target, const Movie& source, MovieScraperIn
}
}

void copyDetailsToMovie(Movie& target, const Movie& source, const QSet<MovieScraperInfo>& details)
void copyDetailsToMovie(Movie& target,
const Movie& source,
const QSet<MovieScraperInfo>& details,
bool usePlotForOutline)
{
for (MovieScraperInfo detail : details) {
copyDetailToMovie(target, source, detail);
copyDetailToMovie(target, source, detail, usePlotForOutline);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/scrapers/movie/MovieMerger.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ class Movie;
namespace mediaelch {
namespace scraper {

void copyDetailsToMovie(Movie& target, const Movie& source, const QSet<MovieScraperInfo>& details);
void copyDetailsToMovie(Movie& target,
const Movie& source,
const QSet<MovieScraperInfo>& details,
bool usePlotForOutline);

} // namespace scraper
} // namespace mediaelch
3 changes: 2 additions & 1 deletion src/scrapers/movie/MovieScraper.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ class MovieScraper : public QObject, public ScraperInterface

public:
explicit MovieScraper(QObject* parent = nullptr) : QObject(parent) {}
~MovieScraper() override = default;
/// \brief Information about the scraper.
virtual const ScraperMeta& meta() const = 0;

virtual void initialize() = 0;
virtual bool isInitialized() const = 0;
ELCH_NODISCARD virtual bool isInitialized() const = 0;

/// \brief Search for the given \p query.
///
Expand Down
3 changes: 0 additions & 3 deletions src/scrapers/movie/aebn/AebnScrapeJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ void AebnScrapeJob::parseAndAssignInfos(const QString& html, QStringList& actorI
match = rx.match(html);
if (match.hasMatch()) {
m_movie->setOverview(match.captured(1));
if (Settings::instance()->usePlotForOutline()) {
m_movie->setOutline(match.captured(1));
}
}

rx.setPattern("<div id=\"md-boxCover\"><a href=\"([^\"]*)\" target=\"_blank\" onclick=\"([^\"]*)\"><img "
Expand Down
9 changes: 6 additions & 3 deletions src/scrapers/movie/custom/CustomMovieScrapeJob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
#include "data/movie/Movie.h"
#include "scrapers/movie/MovieMerger.h"
#include "scrapers/movie/MovieScraper.h"
#include "scrapers/movie/imdb/ImdbMovie.h"
#include "settings/Settings.h"

namespace mediaelch {
namespace scraper {

CustomMovieScrapeJob::CustomMovieScrapeJob(CustomScraperConfig _config, QObject* parent) :
MovieScrapeJob(_config.config, parent), m_customScraperConfig{std::move(_config)}
MovieScrapeJob(MovieScrapeJob::Config{MovieIdentifier{""}, Locale::NoLocale, _config.details}, parent),
m_customScraperConfig{std::move(_config)}
{
}

void CustomMovieScrapeJob::doStart()
{
if (m_customScraperConfig.scraperMap.isEmpty()) {
Expand All @@ -29,7 +31,8 @@ void CustomMovieScrapeJob::doStart()

void CustomMovieScrapeJob::onScraperFinished(MovieScrapeJob* scrapeJob)
{
copyDetailsToMovie(*m_movie, scrapeJob->movie(), scrapeJob->config().details);
copyDetailsToMovie(
*m_movie, scrapeJob->movie(), scrapeJob->config().details, Settings::instance()->usePlotForOutline());
scrapeJob->deleteLater();

const bool isRemoved = m_jobs.removeOne(scrapeJob);
Expand Down
5 changes: 3 additions & 2 deletions src/scrapers/movie/custom/CustomMovieScrapeJob.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class CustomMovieScrapeJob : public MovieScrapeJob
public:
struct CustomScraperConfig
{
/// \brief Config passed to MovieScrapeJob constructor.
MovieScrapeJob::Config config;
/// \brief Details to load via custom movie scraper.
/// \details The details per scraper are determined via global settings.
QSet<MovieScraperInfo> details;
/// \brief Actual custom scraper configuration.
QHash<MovieScraper*, MovieScrapeJob::Config> scraperMap;
};
Expand Down
2 changes: 1 addition & 1 deletion src/scrapers/movie/custom/CustomMovieScraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ MovieScrapeJob* CustomMovieScraper::loadMovie(MovieScrapeJob::Config config)
}

CustomMovieScrapeJob::CustomScraperConfig scraperConfig;
scraperConfig.config = std::move(config);
scraperConfig.details = config.details;
scraperConfig.scraperMap = scraperMap;

return new CustomMovieScrapeJob(scraperConfig, this);
Expand Down
2 changes: 2 additions & 0 deletions src/ui/movies/MovieMultiScrapeDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ void MovieMultiScrapeDialog::scrapeNext()
config.query = m_currentMovie->name();

const bool isCustomScraper = (m_currentScraper->meta().identifier == CustomMovieScraper::ID);
// TODO: What if there is no title scraper?
const QString& customTitleScraperID = CustomMovieScraper::instance()->titleScraper()->meta().identifier;
if (isCustomScraper) {
if ((customTitleScraperID == ImdbMovie::ID || customTitleScraperID == TmdbMovie::ID)
Expand Down Expand Up @@ -406,6 +407,7 @@ void MovieMultiScrapeDialog::onCustomScraperSearchFinished(mediaelch::scraper::M
}

// TODO: Use some sort of intersection-operator function.
// TODO(Andre): Check here
QVector<MovieScraper*> scrapersLeft;
for (MovieScraper* searchScraper : asConst(customScrapersToUse)) {
if (!m_currentIds.contains(searchScraper)) {
Expand Down
14 changes: 14 additions & 0 deletions test/helpers/normalize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ static double roundCommaFirstDigit(double number)

namespace test {

QString approxMagnitude(int number)
{
MediaElch_Expects(number >= 0);
if (number < 2) {
return QStringLiteral("=%1").arg(number);
} else if (number < 6) {
return QStringLiteral("<6");
} else if (number <= 10) {
return QStringLiteral(">5");
} else {
return QStringLiteral(">%1").arg(roundToMagnitude(number));
}
}

void normalizeForReferenceFile(Movie& movie)
{
for (auto& rating : movie.ratings()) {
Expand Down
9 changes: 9 additions & 0 deletions test/helpers/normalize.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <QString>

class Artist;
class Album;
class Movie;
Expand All @@ -8,6 +10,13 @@ class TvShowEpisode;

namespace test {

/// Rounds the given number magnitude aware and returns a human readable string.
/// Examples:
/// 3 -> "3"
/// 123 -> ">120"
/// 1234 -> ">1200"
QString approxMagnitude(int number);

/// Normalize a movie for reference files. Some fields change a lot
/// on sites like IMDb, for example, ratings change a lot (voteCount for
/// Godfather changes every few minutes).
Expand Down
38 changes: 28 additions & 10 deletions test/helpers/reference_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ static auto writeToReference(QTextStream& out, const QString& key, const T& valu

static void writeToReference(QTextStream& out, const QString& key, const QStringList& value)
{
out << key << ": (N=" << value.size() << ")\n";
out << key << ": (N" << test::approxMagnitude(value.size()) << ")\n";
const int count = std::min(10, qsizetype_to_int(value.size()));
for (int i = 0; i < count; ++i) {
out << " - " << value[i] << "\n";
}
if (count < value.size()) {
out << " - ... and " << value.size() - count << " more\n";
out << " - ... and " << test::approxMagnitude(value.size() - count) << " more\n";
}
}
static void writeToReference(QTextStream& out, const QString& key, int value)
Expand All @@ -86,15 +86,33 @@ static void writeToReference(QTextStream& out, const QString& key, const QUrl& v
}
static void writeToReference(QTextStream& out, const QString& key, const QDate& value)
{
out << key << ": " << value.toString(Qt::DateFormat::ISODate) << "\n";
out << key << ": ";
if (value.isValid()) {
out << value.toString(Qt::DateFormat::ISODate);
} else {
out << "<not set or invalid>";
}
out << "\n";
}
static void writeToReference(QTextStream& out, const QString& key, const QDateTime& value)
{
out << key << ": " << value.toString(Qt::DateFormat::ISODate) << "\n";
out << key << ": ";
if (value.isValid()) {
out << value.toString(Qt::DateFormat::ISODate);
} else {
out << "<not set or invalid>";
}
out << "\n";
}
static void writeToReference(QTextStream& out, const QString& key, const QTime& value)
{
out << key << ": " << value.toString(Qt::DateFormat::ISODate) << "\n";
out << key << ": ";
if (value.isValid()) {
out << value.toString(Qt::DateFormat::ISODate);
} else {
out << "<not set or invalid>";
}
out << "\n";
}
static void writeToReference(QTextStream& out, const QString& key, const std::chrono::minutes& value)
{
Expand Down Expand Up @@ -124,7 +142,7 @@ static void writeToReference(QTextStream& out, const QString& key, const DiscTyp
}
static void writeToReference(QTextStream& out, const QString& key, const Ratings& value)
{
out << key << " (N=" << value.size() << ")\n";
out << key << " (N" << test::approxMagnitude(value.size()) << ")\n";
for (const auto& rating : value) {
out << " " //
<< "source=" << rating.source << " | " //
Expand All @@ -137,7 +155,7 @@ static void writeToReference(QTextStream& out, const QString& key, const Ratings
}
static void writeToReference(QTextStream& out, const QString& key, const QVector<Poster>& value)
{
out << key << ": (N=" << value.size() << ")\n";
out << key << ": (N" << test::approxMagnitude(value.size()) << ")\n";
const int count = std::min(10, qsizetype_to_int(value.size()));
for (int i = 0; i < count; ++i) {
const Poster& poster = value[i];
Expand All @@ -157,7 +175,7 @@ static void writeToReference(QTextStream& out, const QString& key, const QVector

static void writeToReference(QTextStream& out, const QString& key, const Actors& value)
{
out << key << ": (N=" << value.size() << ")\n";
out << key << ": (N" << test::approxMagnitude(value.size()) << ")\n";
const auto& actors = value.actors();
const int count = std::min(20, qsizetype_to_int(value.size()));
for (int i = 0; i < count; ++i) {
Expand Down Expand Up @@ -198,7 +216,7 @@ static void writeToReference(QTextStream& out, const QString& key, const StreamD

static void writeToReference(QTextStream& out, const QString& key, const QVector<Subtitle*>& value)
{
out << key << ": (N=" << value.size() << ")\n";
out << key << ": (N" << test::approxMagnitude(value.size()) << ")\n";
for (const Subtitle* subtitle : value) {
writeToReference(out, " - language", subtitle->language());
writeToReference(out, " forced", subtitle->forced());
Expand All @@ -208,7 +226,7 @@ static void writeToReference(QTextStream& out, const QString& key, const QVector

static void writeToReference(QTextStream& out, const QString& key, const QVector<DiscographyAlbum>& value)
{
out << key << ": (N=" << value.size() << ")\n";
out << key << ": (N" << test::approxMagnitude(value.size()) << ")\n";
for (const DiscographyAlbum& album : value) {
writeToReference(out, " - title", album.title);
writeToReference(out, " year", album.year);
Expand Down
Loading

0 comments on commit 48c0db6

Please sign in to comment.