Make string matching in Steam and GOG store search case insensitive.
This commit is contained in:
parent
e62e608c48
commit
419333fac3
|
@ -1,6 +1,7 @@
|
||||||
import difflib
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
from dailyreleases import util
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
@ -14,18 +15,15 @@ class GOG(object):
|
||||||
"limit": 5,
|
"limit": 5,
|
||||||
"search": query
|
"search": query
|
||||||
}
|
}
|
||||||
results = self.cache.get("https://www.gog.com/games/ajax/filtered", params=payload).json()["products"]
|
products = {p["title"]: p
|
||||||
|
for p in self.cache.get("https://www.gog.com/games/ajax/filtered", params=payload).json()["products"]
|
||||||
|
if p["isGame"]}
|
||||||
|
|
||||||
best_match = difflib.get_close_matches(query,
|
best_match = util.case_insensitive_close_matches(query, products, n=1, cutoff=0.90)
|
||||||
[r["title"] for r in results if r["isGame"]],
|
|
||||||
n=1,
|
|
||||||
cutoff=0.90)
|
|
||||||
|
|
||||||
if not best_match:
|
if not best_match:
|
||||||
logger.debug("Unable to find %s in GOG search results", query)
|
logger.debug("Unable to find %s in GOG search results", query)
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.debug("Best match is '%s'", best_match[0])
|
logger.debug("Best match is '%s'", best_match[0])
|
||||||
return next("https://gog.com{}".format(r["url"])
|
return "https://gog.com{url}".format(**products[best_match[0]])
|
||||||
for r in results
|
|
||||||
if r["title"] == best_match[0])
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
import difflib
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -51,18 +50,14 @@ class Steam(object):
|
||||||
payload = {
|
payload = {
|
||||||
"term": query
|
"term": query
|
||||||
}
|
}
|
||||||
results = self.cache.get("https://store.steampowered.com/api/storesearch", params=payload).json()["items"]
|
items = {i["name"]: i for i in self.cache.get("https://store.steampowered.com/api/storesearch",
|
||||||
|
params=payload).json()["items"]}
|
||||||
|
|
||||||
best_match = difflib.get_close_matches(query,
|
best_match = util.case_insensitive_close_matches(query, items, n=1, cutoff=0.90)
|
||||||
[r["name"] for r in results],
|
|
||||||
n=1,
|
|
||||||
cutoff=0.90)
|
|
||||||
|
|
||||||
if not best_match:
|
if not best_match:
|
||||||
logger.debug("Unable to find %s in Steam search results", query)
|
logger.debug("Unable to find %s in Steam search results", query)
|
||||||
return
|
return
|
||||||
|
|
||||||
logger.debug("Best match is '%s'", best_match[0])
|
logger.debug("Best match is '%s'", best_match[0])
|
||||||
return next("https://store.steampowered.com/{}/{}".format(r["type"], r["id"])
|
return "https://store.steampowered.com/{type}/{id}".format(**items[best_match[0]])
|
||||||
for r in results
|
|
||||||
if r["name"] == best_match[0])
|
|
||||||
|
|
11
dailyreleases/util.py
Normal file
11
dailyreleases/util.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
import difflib
|
||||||
|
|
||||||
|
|
||||||
|
def case_insensitive_close_matches(word, possibilities, n=3, cutoff=0.6):
|
||||||
|
"""
|
||||||
|
Python's difflib.get_close_matches does case sensitive sequence matching, this function decorates the library
|
||||||
|
function to make it case insensitive.
|
||||||
|
"""
|
||||||
|
possibilities = {sequence.lower(): sequence for sequence in possibilities}
|
||||||
|
close_matches = difflib.get_close_matches(word.lower(), possibilities, n=n, cutoff=cutoff)
|
||||||
|
return [possibilities[m] for m in close_matches]
|
Reference in a new issue