Skip to content

Commit

Permalink
feat(API): Implemented function that build the Cutter Table dictionary.
Browse files Browse the repository at this point in the history
Was implemented a function that makes a request to a UFRRJ Library website and with Beautifulsoup4 library build a dictionary with Cutter Table. This function is called on start fastaAPI project and this dictionary stay cached in memory all the time.

Closes #9
  • Loading branch information
Riverfount committed Nov 27, 2022
1 parent 2414ebf commit 42e7b2c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120
Empty file added api/business_rules/__init__.py
Empty file.
23 changes: 23 additions & 0 deletions api/business_rules/cutter_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import httpx
from bs4 import BeautifulSoup


def gen_cutter_table():
cutter_table = {}
for index in range(1, 10):
url = f'https://academico.ufrrj.br/biblioteca/cutter/cutter{index}.html'

resp = httpx.get(url)

soup = BeautifulSoup(resp.text, 'html.parser')

letters = soup.find_all('b')[0].getText()

for i, letter in enumerate(letters):
cutter_table[letter.lower()] = []
for element in soup.find_all('pre')[i].getText().split('\n'):
if element.strip() and element.strip().isascii():
code, abreviaton = element.strip().split(' ' * 5)
cutter_table[letter.lower()].append((code, abreviaton.lower()))

return cutter_table
12 changes: 11 additions & 1 deletion api/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
from fastapi import FastAPI

from api.business_rules.cutter_table import gen_cutter_table

api = FastAPI()

cutter_table = {}


@api.on_event("startup")
async def startup_event():
global cutter_table
cutter_table = gen_cutter_table()


@api.get('/health')
def health():
async def health():
return {'message': 'The API is 100% ok!'}
28 changes: 14 additions & 14 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ packages = [{ include = "gen_cutter_sanborn" }]
[tool.poetry.dependencies]
python = "^3.11"
unidecode = "^1.3.6"
requests = "^2.28.1"
beautifulsoup4 = "^4.11.1"
fastapi = "^0.87.0"
fastapi = "^0.88.0"
gunicorn = "^20.1.0"
uvicorn = { extras = ["standard"], version = "^0.20.0" }

Expand Down

0 comments on commit 42e7b2c

Please sign in to comment.