diff --git a/dailyreleases/gog.py b/dailyreleases/gog.py new file mode 100644 index 0000000..518a486 --- /dev/null +++ b/dailyreleases/gog.py @@ -0,0 +1,31 @@ +import difflib +import logging + +logger = logging.getLogger(__name__) + + +class Gog(object): + def __init__(self, cache) -> None: + self.cache = cache + + def search(self, query): + logger.debug("Searching GOG for %s", query) + payload = { + "limit": 5, + "search": query + } + results = self.cache.get("https://www.gog.com/games/ajax/filtered", params=payload).json()["products"] + + best_match = difflib.get_close_matches(query, + [r["title"] for r in results if r["isGame"]], + n=1, + cutoff=0.90) + + if not best_match: + logger.debug("Unable to find %s in GOG search results", query) + return + + logger.debug("Best match is '%s'", best_match[0]) + return next("https://gog.com{}".format(r["url"]) + for r in results + if r["title"] == best_match[0])