Add basic search function

This commit is contained in:
Tyler Perkins 2023-04-11 18:41:59 -04:00
parent 9773cf11f2
commit c3c7ff61e9
8 changed files with 95 additions and 0 deletions

View File

@ -1,2 +1,3 @@
# OHLQ-api # OHLQ-api
API wrapper to scrape the [Ohio Liquour Agency](https://www.ohlq.com)

10
src/api/__init__.py Normal file
View File

@ -0,0 +1,10 @@
from flask_restx import Api
from .search import api as search_ns
api = Api(
title='OHLQ',
version='1.0',
description='Programatic interface to OHLQ'
)
api.add_namespace(search_ns)

0
src/api/availability.py Normal file
View File

39
src/api/search.py Normal file
View File

@ -0,0 +1,39 @@
from flask_restx import Namespace, Resource, fields
from flask_restx import reqparse
from flask import make_response, abort, request, Response
from functools import lru_cache
import requests
import logging
api = Namespace('search',
description='Search the OHLQ website')
@api.route('/<string:query>')
@api.doc(description='Search the OHLQ website. This is equivilant to the search box on the home page.')
class Search(Resource):
@api.doc('get')
@api.response(200, 'Success')
@api.response(500, 'Failure to retrieve data')
def get(self, query):
try:
reply = performSearch(query)
except Exception as e:
abort(500, str(e))
return reply
# We define the caching mechanism for web requests
@lru_cache(maxsize=32)
def performSearch(query, page=1, pagesize=10):
params = {
"query": query,
"page": page,
"pagesize": pagesize
}
response = requests.get("https://www.ohlq.com/api/search/site", params=params)
if response.status_code != 200:
logging.warn("Unable to perform search, got non 200 response code, recieved response %s", response.status_code)
raise Exception("Unable to perform search, got non 200 response code")
return response.json()

21
src/app.py Executable file
View File

@ -0,0 +1,21 @@
#!/usr/bin/python
# Entry point
from flask import Flask
from api import api
from config import config, setLogLevel
import os
import logging
setLogLevel()
port = config["server.port"]
app = Flask(__name__)
api.init_app(app)
app.run(host='0.0.0.0', port=port)

19
src/config.py Normal file
View File

@ -0,0 +1,19 @@
# Load configuration yaml
import envyaml
import os
import logging
config = envyaml.EnvYAML(os.environ.get('CONFIG_PATH', 'config.yaml'))
def setLogLevel():
"""
Set the logging level based on the config
"""
logLevel = config["log.level"]
if logLevel == "info":
logging.basicConfig(level=logging.INFO)
elif logLevel == "debug":
logging.basicConfig(level=logging.DEBUG)
logging.warn("Logging level set to %s", logLevel)

5
src/config.yaml Normal file
View File

@ -0,0 +1,5 @@
log:
level: "debug"
server:
port: 8080

0
src/requirements.txt Normal file
View File