Restructure web search into own file.
This commit is contained in:
parent
d24bbf9cf6
commit
c99da77155
2 changed files with 26 additions and 19 deletions
|
@ -1,30 +1,13 @@
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
from typing import Dict, List
|
from typing import Dict
|
||||||
from urllib.error import HTTPError
|
|
||||||
|
|
||||||
from .. import cache
|
from .web import web_search
|
||||||
from ..config import CONFIG
|
|
||||||
from ..stores import steam, gog
|
from ..stores import steam, gog
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
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]:
|
def find_store_links(game_name: str) -> Dict[str, str]:
|
||||||
links = {}
|
links = {}
|
||||||
for store, name in ((steam, "Steam"), (gog, "GOG")):
|
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