1
0
Fork 0

Add wrapper for GOG's (unofficial) search API.

This commit is contained in:
Casper V. Kristensen 2018-08-01 03:35:22 +02:00
parent 8ce7859f08
commit 1fc385e440
Signed by: caspervk
GPG key ID: B1156723DB3BDDA8

31
dailyreleases/gog.py Normal file
View 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])