From d5acae870b99a56cba971fe083f7537a64b26505 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Sun, 14 May 2023 17:48:10 +0200 Subject: [PATCH] Extract comic vine json parsing to its own file + add support for extra fields --- YACReaderLibrary/comic_vine/comic_vine.pri | 2 + .../comic_vine/comic_vine_dialog.cpp | 197 +-------------- .../comic_vine/comic_vine_dialog.h | 5 - .../comic_vine/comic_vine_json_parser.cpp | 233 ++++++++++++++++++ .../comic_vine/comic_vine_json_parser.h | 13 + 5 files changed, 251 insertions(+), 199 deletions(-) create mode 100644 YACReaderLibrary/comic_vine/comic_vine_json_parser.cpp create mode 100644 YACReaderLibrary/comic_vine/comic_vine_json_parser.h diff --git a/YACReaderLibrary/comic_vine/comic_vine.pri b/YACReaderLibrary/comic_vine/comic_vine.pri index 85232a53..1c84f938 100644 --- a/YACReaderLibrary/comic_vine/comic_vine.pri +++ b/YACReaderLibrary/comic_vine/comic_vine.pri @@ -1,5 +1,6 @@ HEADERS += \ + $$PWD/comic_vine_json_parser.h \ comic_vine/comic_vine_dialog.h \ comic_vine/comic_vine_client.h \ comic_vine/scraper_lineedit.h \ @@ -24,6 +25,7 @@ HEADERS += \ $$PWD/comic_vine_all_volume_comics_retriever.h SOURCES += \ + $$PWD/comic_vine_json_parser.cpp \ comic_vine/comic_vine_dialog.cpp \ comic_vine/comic_vine_client.cpp \ comic_vine/scraper_lineedit.cpp \ diff --git a/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp b/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp index 28781ada..025b9b76 100644 --- a/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp +++ b/YACReaderLibrary/comic_vine/comic_vine_dialog.cpp @@ -10,12 +10,11 @@ #include #include #include "data_base_management.h" -#include -#include #include #include "yacreader_busy_widget.h" #include "comic_vine_client.h" +#include "comic_vine_json_parser.h" #include "scraper_lineedit.h" #include "title_header.h" #include "series_question.h" @@ -461,7 +460,7 @@ void ComicVineDialog::getComicsInfo(QList> matchingInfo, QByteArray result = comicVineClient->getComicDetail(p.second, error, timeout); // TODO check timeOut or Connection error if (error || timeout) continue; // TODO - ComicDB comic = parseComicInfo(p.first, result, count, publisher); // TODO check result error + ComicDB comic = YACReader::parseCVJSONComicInfo(p.first, result, count, publisher); // TODO check result error comic.info.comicVineID = p.second; comics.push_back(comic); @@ -489,7 +488,7 @@ void ComicVineDialog::getComicInfo(const QString &comicId, int count, const QStr } } - ComicDB comic = parseComicInfo(comics[currentIndex], result, count, publisher); // TODO check result error + ComicDB comic = YACReader::parseCVJSONComicInfo(comics[currentIndex], result, count, publisher); // TODO check result error comic.info.comicVineID = comicId; setLoadingMessage(tr("Retrieving tags for : %1").arg(comics[currentIndex].getFileName())); QString connectionName = ""; @@ -512,196 +511,6 @@ void ComicVineDialog::getComicInfo(const QString &comicId, int count, const QStr } } -ComicDB ComicVineDialog::parseComicInfo(ComicDB &comic, const QString &json, int count, const QString &publisher) -{ - QJsonParseError Err; - - QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap(); - if (Err.error != QJsonParseError::NoError) { - qDebug("Error detected"); - return comic; - } - - int numResults = sc.value("number_of_total_results").toInt(); // fix to weird behaviour using hasNext - - if (numResults > 0) { - QVariantMap result = sc.value("results").toMap(); - comic.info.title = result.value("name"); - comic.info.number = result.value("issue_number"); - comic.info.volume = result.value("volume").toMap().value("name"); - - if (result.contains("person_credits") && !result.value("person_credits").isNull()) { - auto authors = getAuthors(result.value("person_credits")); - - QString writer = authors.values("writer").join("\n"); - QString penciller = authors.values("penciller").join("\n"); - QString inker = authors.values("inker").join("\n"); - QString colorist = authors.values("colorist").join("\n"); - QString letterer = authors.values("letterer").join("\n"); - QString coverArtist = authors.values("cover").join("\n"); - - comic.info.writer = writer; - comic.info.penciller = penciller; - comic.info.inker = inker; - comic.info.colorist = colorist; - comic.info.letterer = letterer; - comic.info.coverArtist = coverArtist; - } - - if (result.contains("cover_date") && !result.value("cover_date").isNull()) { - QString date = result.value("cover_date").toString(); - - QStringList tempList = date.split("-"); - - if (tempList.length() == 3) { - std::reverse(tempList.begin(), tempList.end()); - comic.info.date = tempList.join("/"); - } - } - - if (result.contains("description") && !result.value("description").isNull()) { - comic.info.synopsis = result.value("description"); - } - - if (result.contains("character_credits") && !result.value("character_credits").isNull()) { - comic.info.characters = getCharacters(result.value("character_credits")); - } - - if (result.contains("story_arc_credits") && !result.value("story_arc_credits").isNull()) { - QPair storyArcIdAndName = getFirstStoryArcIdAndName(result.value("story_arc_credits")); - QString storyArcId = storyArcIdAndName.first; - QString storyArcName = storyArcIdAndName.second; - if (!storyArcId.isNull()) { - - QString comicId = result.value("id").toString(); - - QPair arcNumberAndArcCount = getArcNumberAndArcCount(storyArcId, comicId); - if (!arcNumberAndArcCount.first.isNull()) { - QString arcNumber = arcNumberAndArcCount.first; - QString arcCount = arcNumberAndArcCount.second; - - comic.info.storyArc = storyArcName; - comic.info.arcNumber = arcNumber; - comic.info.arcCount = arcCount; - } - } - } - - comic.info.count = count; - - comic.info.publisher = publisher; - - comic.info.edited = true; - } - - return comic; -} - -QString ComicVineDialog::getCharacters(const QVariant &json_characters) -{ - QStringList characters; - - QListIterator it(json_characters.toList()); - QVariantMap resultsValue; - while (it.hasNext()) { - resultsValue = it.next().toMap(); - - characters << resultsValue.value("name").toString(); - } - - return (characters.isEmpty()) ? "" : (characters.join("\n") + "\n"); -} - -QMultiMap ComicVineDialog::getAuthors(const QVariant &json_authors) -{ - QMultiMap authors; - - QListIterator it(json_authors.toList()); - QVariantMap resultsValue; - while (it.hasNext()) { - resultsValue = it.next().toMap(); - - QString authorName = resultsValue.value("name").toString(); - - QStringList roles = resultsValue.value("role").toString().split(","); - foreach (QString role, roles) { - if (role.trimmed() == "writer") - authors.insert("writer", authorName); - else if (role.trimmed() == "inker") - authors.insert("inker", authorName); - else if (role.trimmed() == "penciler" || role.trimmed() == "penciller") - authors.insert("penciller", authorName); - else if (role.trimmed() == "colorist") - authors.insert("colorist", authorName); - else if (role.trimmed() == "letterer") - authors.insert("letterer", authorName); - else if (role.trimmed() == "cover") - authors.insert("cover", authorName); - } - } - - return authors; -} - -QPair ComicVineDialog::getFirstStoryArcIdAndName(const QVariant &json_story_arcs) -{ - QString story_arc_id = QString(); - QString story_arc_name = QString(); - - QListIterator it(json_story_arcs.toList()); - QVariantMap resultsValue; - while (it.hasNext()) { - resultsValue = it.next().toMap(); - story_arc_id = resultsValue.value("id").toString(); - story_arc_name = resultsValue.value("name").toString(); - break; - } - return qMakePair(story_arc_id, story_arc_name); -} - -QPair ComicVineDialog::getArcNumberAndArcCount(const QString &storyArcId, const QString &comicId) -{ - auto comicVineClient = new ComicVineClient; - bool error; - bool timeout; - QByteArray result = comicVineClient->getStoryArcDetail(storyArcId, error, timeout); - if (error || timeout) - return qMakePair(QString(), QString()); - QString json = result; - - QJsonParseError Err; - QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap(); - - if (Err.error != QJsonParseError::NoError) { - qDebug("Error detected"); - return qMakePair(QString(), QString()); - } - - int numResults = sc.value("number_of_total_results").toInt(); // fix to weird behaviour using hasNext - - if (numResults > 0) { - QVariantMap result = sc.value("results").toMap(); - - if (result.contains("issues")) { - QListIterator it(result.value("issues").toList()); - int arcNumber = 0; - int arcCount = 0; - - QVariantMap resultsValue; - while (it.hasNext()) { - resultsValue = it.next().toMap(); - if (comicId == resultsValue.value("id").toString()) { - arcNumber = arcCount + 1; - } - arcCount++; - } - return qMakePair(QString::number(arcNumber), QString::number(arcCount)); - } - return qMakePair(QString(), QString()); - } - return qMakePair(QString(), QString()); -} - void ComicVineDialog::toggleSkipButton() { if (mode == SingleComicInList) diff --git a/YACReaderLibrary/comic_vine/comic_vine_dialog.h b/YACReaderLibrary/comic_vine/comic_vine_dialog.h index 9ea4b7f1..fa686150 100644 --- a/YACReaderLibrary/comic_vine/comic_vine_dialog.h +++ b/YACReaderLibrary/comic_vine/comic_vine_dialog.h @@ -58,16 +58,11 @@ protected slots: void showSelectComic(const QString &json); void showSortVolumeComics(const QString &json); void queryTimeOut(); - ComicDB parseComicInfo(ComicDB &comic, const QString &json, int count, const QString &publisher); void setLoadingMessage(const QString &message); void goToNextComic(); private: void clearState(); - QString getCharacters(const QVariant &json_characters); - QMultiMap getAuthors(const QVariant &json_authors); - QPair getFirstStoryArcIdAndName(const QVariant &json_story_arcs); - QPair getArcNumberAndArcCount(const QString &storyArcId, const QString &comicId); void toggleSkipButton(); diff --git a/YACReaderLibrary/comic_vine/comic_vine_json_parser.cpp b/YACReaderLibrary/comic_vine/comic_vine_json_parser.cpp new file mode 100644 index 00000000..f6a03813 --- /dev/null +++ b/YACReaderLibrary/comic_vine/comic_vine_json_parser.cpp @@ -0,0 +1,233 @@ + +#include "comic_vine_json_parser.h" + +#include "comic_vine_client.h" + +#include +#include + +QString getCharacters(const QVariant &json_characters); +QMultiMap getAuthors(const QVariant &json_authors); +QPair getFirstStoryArcIdAndName(const QVariant &json_story_arcs); +QPair getArcNumberAndArcCount(const QString &storyArcId, const QString &comicId); +QList getNamesFromList(const QVariant &json_list); + +ComicDB YACReader::parseCVJSONComicInfo(ComicDB &comic, const QString &json, int count, const QString &publisher) +{ + QJsonParseError Err; + + QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap(); + if (Err.error != QJsonParseError::NoError) { + qDebug("Error detected"); + return comic; + } + + int numResults = sc.value("number_of_total_results").toInt(); // fix to weird behaviour using hasNext + + if (numResults > 0) { + QVariantMap result = sc.value("results").toMap(); + comic.info.title = result.value("name"); + comic.info.number = result.value("issue_number"); + + // changed in 9.13, volume actually means series in ComicVine + comic.info.series = result.value("volume").toMap().value("name"); + + if (result.contains("person_credits") && !result.value("person_credits").isNull()) { + auto authors = getAuthors(result.value("person_credits")); + + QString writer = authors.values("writer").join("\n"); + QString penciller = authors.values("penciller").join("\n"); + QString inker = authors.values("inker").join("\n"); + QString colorist = authors.values("colorist").join("\n"); + QString letterer = authors.values("letterer").join("\n"); + QString coverArtist = authors.values("cover").join("\n"); + + comic.info.writer = writer; + comic.info.penciller = penciller; + comic.info.inker = inker; + comic.info.colorist = colorist; + comic.info.letterer = letterer; + comic.info.coverArtist = coverArtist; + } + + if (result.contains("cover_date") && !result.value("cover_date").isNull()) { + QString date = result.value("cover_date").toString(); + + QStringList tempList = date.split("-"); + + if (tempList.length() == 3) { + std::reverse(tempList.begin(), tempList.end()); + comic.info.date = tempList.join("/"); + } + } + + if (result.contains("description") && !result.value("description").isNull()) { + comic.info.synopsis = result.value("description"); + } + + if (result.contains("character_credits") && !result.value("character_credits").isNull()) { + comic.info.characters = getCharacters(result.value("character_credits")); + } + + if (result.contains("story_arc_credits") && !result.value("story_arc_credits").isNull()) { + QPair storyArcIdAndName = getFirstStoryArcIdAndName(result.value("story_arc_credits")); + QString storyArcId = storyArcIdAndName.first; + QString storyArcName = storyArcIdAndName.second; + if (!storyArcId.isNull()) { + + QString comicId = result.value("id").toString(); + + QPair arcNumberAndArcCount = getArcNumberAndArcCount(storyArcId, comicId); + if (!arcNumberAndArcCount.first.isNull()) { + QString arcNumber = arcNumberAndArcCount.first; + QString arcCount = arcNumberAndArcCount.second; + + comic.info.storyArc = storyArcName; + comic.info.arcNumber = arcNumber; + comic.info.arcCount = arcCount; + } + } + } + + if (result.contains("location_credits") && !result.value("location_credits").isNull()) { + comic.info.locations = getNamesFromList(result.value("location_credits")).join("\n"); + } + + if (result.contains("team_credits") && !result.value("team_credits").isNull()) { + comic.info.teams = getNamesFromList(result.value("team_credits")).join("\n"); + } + + if (result.contains("character_credits") && !result.value("character_credits").isNull()) { + comic.info.characters = getNamesFromList(result.value("character_credits")).join("\n"); + } + + comic.info.count = count; + + comic.info.publisher = publisher; + + comic.info.edited = true; + } + + return comic; +} + +QString getCharacters(const QVariant &json_characters) +{ + QStringList characters; + + QListIterator it(json_characters.toList()); + QVariantMap resultsValue; + while (it.hasNext()) { + resultsValue = it.next().toMap(); + + characters << resultsValue.value("name").toString(); + } + + return (characters.isEmpty()) ? "" : (characters.join("\n") + "\n"); +} + +QMultiMap getAuthors(const QVariant &json_authors) +{ + QMultiMap authors; + + QListIterator it(json_authors.toList()); + QVariantMap resultsValue; + while (it.hasNext()) { + resultsValue = it.next().toMap(); + + QString authorName = resultsValue.value("name").toString(); + + QStringList roles = resultsValue.value("role").toString().split(","); + foreach (QString role, roles) { + if (role.trimmed() == "writer") + authors.insert("writer", authorName); + else if (role.trimmed() == "inker") + authors.insert("inker", authorName); + else if (role.trimmed() == "penciler" || role.trimmed() == "penciller") + authors.insert("penciller", authorName); + else if (role.trimmed() == "colorist") + authors.insert("colorist", authorName); + else if (role.trimmed() == "letterer") + authors.insert("letterer", authorName); + else if (role.trimmed() == "cover") + authors.insert("cover", authorName); + } + } + + return authors; +} + +QPair getFirstStoryArcIdAndName(const QVariant &json_story_arcs) +{ + QString story_arc_id = QString(); + QString story_arc_name = QString(); + + QListIterator it(json_story_arcs.toList()); + QVariantMap resultsValue; + while (it.hasNext()) { + resultsValue = it.next().toMap(); + story_arc_id = resultsValue.value("id").toString(); + story_arc_name = resultsValue.value("name").toString(); + break; + } + return qMakePair(story_arc_id, story_arc_name); +} + +QPair getArcNumberAndArcCount(const QString &storyArcId, const QString &comicId) +{ + auto comicVineClient = new ComicVineClient; + bool error; + bool timeout; + QByteArray result = comicVineClient->getStoryArcDetail(storyArcId, error, timeout); + if (error || timeout) + return qMakePair(QString(), QString()); + QString json = result; + + QJsonParseError Err; + QVariantMap sc = QJsonDocument::fromJson(json.toUtf8(), &Err).toVariant().toMap(); + + if (Err.error != QJsonParseError::NoError) { + qDebug("Error detected"); + return qMakePair(QString(), QString()); + } + + int numResults = sc.value("number_of_total_results").toInt(); // fix to weird behaviour using hasNext + + if (numResults > 0) { + QVariantMap result = sc.value("results").toMap(); + + if (result.contains("issues")) { + QListIterator it(result.value("issues").toList()); + int arcNumber = 0; + int arcCount = 0; + + QVariantMap resultsValue; + while (it.hasNext()) { + resultsValue = it.next().toMap(); + if (comicId == resultsValue.value("id").toString()) { + arcNumber = arcCount + 1; + } + arcCount++; + } + return qMakePair(QString::number(arcNumber), QString::number(arcCount)); + } + return qMakePair(QString(), QString()); + } + return qMakePair(QString(), QString()); +} + +QList getNamesFromList(const QVariant &json_list) +{ + QList names; + + QListIterator it(json_list.toList()); + QVariantMap resultsValue; + while (it.hasNext()) { + resultsValue = it.next().toMap(); + + QString name = resultsValue.value("name").toString(); + names.append(name); + } + + return names; +} diff --git a/YACReaderLibrary/comic_vine/comic_vine_json_parser.h b/YACReaderLibrary/comic_vine/comic_vine_json_parser.h new file mode 100644 index 00000000..6b7bf4c1 --- /dev/null +++ b/YACReaderLibrary/comic_vine/comic_vine_json_parser.h @@ -0,0 +1,13 @@ + +#ifndef COMIC_VINE_JSON_PARSER_H +#define COMIC_VINE_JSON_PARSER_H + +#include "comic_db.h" + +namespace YACReader { + +ComicDB parseCVJSONComicInfo(ComicDB &comic, const QString &json, int count, const QString &publisher); + +} + +#endif // COMIC_VINE_JSON_PARSER_H