From 1fc385e44010b508bac9ccae3797a5838844c70e Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Wed, 1 Aug 2018 03:35:22 +0200 Subject: [PATCH] Add wrapper for GOG's (unofficial) search API. --- dailyreleases/gog.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 dailyreleases/gog.py 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])