From 21f21c20f55a0fc24fcbd1ecde8cb907e87f843e Mon Sep 17 00:00:00 2001
From: Leon Styhre <leon@leonstyhre.com>
Date: Sat, 9 Apr 2022 15:14:48 +0200
Subject: [PATCH] Added a scraper setting to convert underscores _ to spaces
 when searching.

---
 es-app/src/guis/GuiScraperMenu.cpp         | 14 ++++++++++++++
 es-app/src/guis/GuiScraperSearch.cpp       |  3 +++
 es-app/src/scrapers/GamesDBJSONScraper.cpp |  3 +++
 es-app/src/scrapers/ScreenScraper.cpp      |  3 +++
 es-core/src/Settings.cpp                   |  1 +
 5 files changed, 24 insertions(+)

diff --git a/es-app/src/guis/GuiScraperMenu.cpp b/es-app/src/guis/GuiScraperMenu.cpp
index 858631591..0b1d3d069 100644
--- a/es-app/src/guis/GuiScraperMenu.cpp
+++ b/es-app/src/guis/GuiScraperMenu.cpp
@@ -879,6 +879,20 @@ void GuiScraperMenu::openOtherOptions()
             ->setOpacity(DISABLED_OPACITY);
     }
 
+    // Convert underscores to spaces when searching.
+    auto scraperConvertUnderscores = std::make_shared<SwitchComponent>();
+    scraperConvertUnderscores->setState(
+        Settings::getInstance()->getBool("ScraperConvertUnderscores"));
+    s->addWithLabel("CONVERT UNDERSCORES TO SPACES WHEN SEARCHING", scraperConvertUnderscores);
+    s->addSaveFunc([scraperConvertUnderscores, s] {
+        if (scraperConvertUnderscores->getState() !=
+            Settings::getInstance()->getBool("ScraperConvertUnderscores")) {
+            Settings::getInstance()->setBool("ScraperConvertUnderscores",
+                                             scraperConvertUnderscores->getState());
+            s->setNeedsSaving();
+        }
+    });
+
     // Retry search on peer verification errors (TLS/certificate issues).
     auto retry_peer_verification = std::make_shared<SwitchComponent>();
     retry_peer_verification->setState(
diff --git a/es-app/src/guis/GuiScraperSearch.cpp b/es-app/src/guis/GuiScraperSearch.cpp
index a6124ad11..4bb2ef1db 100644
--- a/es-app/src/guis/GuiScraperSearch.cpp
+++ b/es-app/src/guis/GuiScraperSearch.cpp
@@ -901,6 +901,9 @@ void GuiScraperSearch::openInputScreen(ScraperSearchParams& params)
         searchString = params.nameOverride;
     }
 
+    if (Settings::getInstance()->getBool("ScraperConvertUnderscores"))
+        searchString = Utils::String::replace(searchString, "_", " ");
+
     if (Settings::getInstance()->getBool("VirtualKeyboard")) {
         mWindow->pushGui(new GuiTextEditKeyboardPopup(getHelpStyle(), "REFINE SEARCH", searchString,
                                                       searchForFunc, false, "SEARCH",
diff --git a/es-app/src/scrapers/GamesDBJSONScraper.cpp b/es-app/src/scrapers/GamesDBJSONScraper.cpp
index 6d8dc9d19..fb94ea406 100644
--- a/es-app/src/scrapers/GamesDBJSONScraper.cpp
+++ b/es-app/src/scrapers/GamesDBJSONScraper.cpp
@@ -178,6 +178,9 @@ void thegamesdb_generate_json_scraper_requests(
         // Trim leading and trailing whitespaces.
         cleanName = Utils::String::trim(cleanName);
 
+        if (Settings::getInstance()->getBool("ScraperConvertUnderscores"))
+            cleanName = Utils::String::replace(cleanName, "_", " ");
+
         path += "/Games/ByGameName?" + apiKey +
                 "&fields=players,publishers,genres,overview,last_updated,rating,"
                 "platform,coop,youtube,os,processor,ram,hdd,video,sound,alternates&name=" +
diff --git a/es-app/src/scrapers/ScreenScraper.cpp b/es-app/src/scrapers/ScreenScraper.cpp
index a54bcfc11..3ad6de289 100644
--- a/es-app/src/scrapers/ScreenScraper.cpp
+++ b/es-app/src/scrapers/ScreenScraper.cpp
@@ -692,6 +692,9 @@ std::string ScreenScraperRequest::ScreenScraperConfig::getGameSearchUrl(
     // Trim leading and trailing whitespaces.
     searchName = Utils::String::trim(searchName);
 
+    if (Settings::getInstance()->getBool("ScraperConvertUnderscores"))
+        searchName = Utils::String::replace(searchName, "_", " ");
+
     // If only whitespaces were entered as the search string, then search using a random string
     // that will not return any results. This is a quick and dirty way to avoid french error
     // messages about malformed URLs that would surely confuse the user.
diff --git a/es-core/src/Settings.cpp b/es-core/src/Settings.cpp
index b167781af..656dff342 100644
--- a/es-core/src/Settings.cpp
+++ b/es-core/src/Settings.cpp
@@ -126,6 +126,7 @@ void Settings::setDefaults()
     mBoolMap["ScraperSemiautomatic"] = {true, true};
     mBoolMap["ScraperRespectExclusions"] = {true, true};
     mBoolMap["ScraperExcludeRecursively"] = {true, true};
+    mBoolMap["ScraperConvertUnderscores"] = {true, true};
     mBoolMap["ScraperRetryPeerVerification"] = {false, false};
 
     // UI settings.