Add wrapper for GOG's (unofficial) search API.
This commit is contained in:
parent
8ce7859f08
commit
1fc385e440
31
dailyreleases/gog.py
Normal file
31
dailyreleases/gog.py
Normal file
|
@ -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])
|
Reference in a new issue