-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from fga-eps-mds/54-grade-programacao
54 grade programacao
- Loading branch information
Showing
7 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import requests | ||
from typing import Optional | ||
from fastapi import APIRouter | ||
from bs4 import BeautifulSoup | ||
from unidecode import unidecode | ||
from starlette.responses import JSONResponse | ||
|
||
from utils import enumeration | ||
from constants import errorMessages | ||
|
||
schedule = APIRouter( | ||
prefix="/schedule" | ||
) | ||
|
||
@schedule.get("/") | ||
async def get_schedule_day(day: Optional[str] = None): | ||
if day: | ||
day = unidecode(day).upper() | ||
if not enumeration.ScheduleDaysEnum.has_value(day): | ||
return JSONResponse(status_code=400, content={ "detail": errorMessages.INVALID_SCHEDULE_DAY }) | ||
|
||
try: | ||
re = requests.get('https://unbtv.unb.br/grade') | ||
html = re.text | ||
|
||
soup = BeautifulSoup(html, 'html.parser') | ||
tables = soup.find_all("table") | ||
|
||
schedule_data = {} | ||
current_day = None | ||
|
||
for table in tables: | ||
for row in table.find_all("tr"): | ||
if len(row.find_all("td")) == 1: | ||
cell = row.find_all("td")[0] | ||
schedule_day = unidecode(cell.text.replace("-FEIRA", "")) | ||
|
||
if day: | ||
if current_day: | ||
return schedule_data | ||
if day == schedule_day: | ||
current_day = schedule_day | ||
schedule_data[schedule_day] = [] | ||
else: | ||
current_day = schedule_day | ||
schedule_data[schedule_day] = [] | ||
else: | ||
if current_day: | ||
day_schedule = [item.text for item in row.find_all("td")[:2]] | ||
if (day_schedule[0].strip() != "" and day_schedule[1].strip() != ""): | ||
schedule_data[current_day].append({ "time": day_schedule[0], "activity": day_schedule[1] }) | ||
|
||
return schedule_data | ||
except: | ||
return JSONResponse(status_code=400, content={ "error": errorMessages.ERROR_RETRIEVING_SCHEDULE }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from enum import Enum | ||
|
||
class ScheduleDaysEnum(Enum): | ||
SEGUNDA = "SEGUNDA" | ||
TERCA = "TERCA" | ||
QUARTA = "QUARTA" | ||
QUINTA = "QUINTA" | ||
SEXTA = "SEXTA" | ||
SABADO = "SABADO" | ||
DOMINGO = "DOMINGO" | ||
|
||
@classmethod | ||
def has_value(cls, value): | ||
return value in cls._value2member_map_ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import pytest | ||
from fastapi.testclient import TestClient | ||
|
||
from src.main import app | ||
from src.constants import errorMessages | ||
|
||
client = TestClient(app) | ||
|
||
class TestSchedule: | ||
def test_schedule_get_schedule_day(self): | ||
response = client.get("/api/schedule/") | ||
data = response.json() | ||
assert response.status_code == 200 | ||
assert len(list(data.keys())) == 7 | ||
assert all([a == b for a, b in zip(list(data.keys()), ['SEGUNDA', 'TERCA', 'QUARTA', 'QUINTA', 'SEXTA', 'SABADO', 'DOMINGO'])]) | ||
|
||
def test_schedule_get_schedule_specific_day_invalid(self): | ||
params = { 'day': 'INVALID' } | ||
response = client.get("/api/schedule/", params=params) | ||
data = response.json() | ||
assert response.status_code == 400 | ||
assert data['detail'] == errorMessages.INVALID_SCHEDULE_DAY | ||
|
||
def test_schedule_get_schedule_specific_day(self): | ||
params = { 'day': 'segunda' } | ||
response = client.get("/api/schedule/", params=params) | ||
data = response.json() | ||
assert response.status_code == 200 | ||
assert len(data) > 0 |