1
0
Fork 0

Restructure web search into own file.

This commit is contained in:
Casper V. Kristensen 2019-05-10 00:45:49 +02:00
parent d24bbf9cf6
commit c99da77155
Signed by: caspervk
GPG key ID: 289CA03790535054
2 changed files with 26 additions and 19 deletions

View file

@ -1,30 +1,13 @@
import logging
import re
from typing import Dict, List
from urllib.error import HTTPError
from typing import Dict
from .. import cache
from ..config import CONFIG
from .web import web_search
from ..stores import steam, gog
logger = logging.getLogger(__name__)
def web_search(query: str) -> List[str]:
logger.debug("Searching Google for %s", query)
try:
r = cache.get("https://www.googleapis.com/customsearch/v1", params={
"key": CONFIG["google"]["key"],
"cx": CONFIG["google"]["cx"],
"q": query
})
return [result["link"] for result in r.json["items"]]
except (KeyError, HTTPError) as e:
logger.exception(e)
logger.warning("Google search failed (probably rate-limited)")
return []
def find_store_links(game_name: str) -> Dict[str, str]:
links = {}
for store, name in ((steam, "Steam"), (gog, "GOG")):

View file

@ -0,0 +1,24 @@
import logging
from typing import List
from urllib.error import HTTPError
from .. import cache
from ..config import CONFIG
logger = logging.getLogger(__name__)
def web_search(query: str) -> List[str]:
logger.debug("Searching Google for %s", query)
try:
# disable rate-limiting since we have a proper API-key (unlike the other APIs we are using)
r = cache.get("https://www.googleapis.com/customsearch/v1", ratelimit=None, params={
"key": CONFIG["google"]["key"],
"cx": CONFIG["google"]["cx"],
"q": query
})
return [result["link"] for result in r.json["items"]]
except (KeyError, HTTPError) as e:
logger.exception(e)
logger.warning("Google search failed (probably rate-limited)")
return []