Restructure web search into own file.
This commit is contained in:
parent
d24bbf9cf6
commit
c99da77155
|
@ -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")):
|
||||
|
|
24
dailyreleases/stores/web.py
Normal file
24
dailyreleases/stores/web.py
Normal 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 []
|
Reference in a new issue