Skip to content

Commit

Permalink
Merge pull request #77 from unb-mds/test/webscraping
Browse files Browse the repository at this point in the history
refactor(utils): cria um app django separado p/ funções do web scraping
  • Loading branch information
mateusvrs authored Nov 7, 2023
2 parents c6a6613 + 0dbc653 commit 8b47f14
Show file tree
Hide file tree
Showing 15 changed files with 558 additions and 3 deletions.
2 changes: 1 addition & 1 deletion api/api/management/commands/updatedb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any
from django.core.management.base import BaseCommand
from api.utils import sessions, web_scraping, db_handler
from utils import sessions, web_scraping, db_handler
from time import time

class Command(BaseCommand):
Expand Down
1 change: 1 addition & 0 deletions api/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
'corsheaders',
'api',
'users',
'utils',
'rest_framework',
'rest_framework_simplejwt',
'rest_framework_simplejwt.token_blacklist'
Expand Down
6 changes: 6 additions & 0 deletions api/core/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@
"""
from django.contrib import admin
from django.urls import path, include
import sys

urlpatterns = [
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
]

if 'test' in sys.argv:
urlpatterns += [
path('utils/', include('utils.urls'))
]
Empty file added api/utils/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions api/utils/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class UtilsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'utils'
File renamed without changes.
Empty file.
Empty file.
21 changes: 21 additions & 0 deletions api/utils/management/commands/updatemock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from typing import Any
from utils.sessions import get_response, create_request_session
from django.core.management.base import BaseCommand
from pathlib import Path
import os


class Command(BaseCommand):
"""Comando para atualizar os arquivos de mock do SIGAA."""

help = "Atualiza os arquivos do mock com as informações do SIGAA requisitadas."

def handle(self, *args: Any, **options: Any):
current_path = Path(__file__).parent.parent.parent.absolute()

os.remove(current_path / "mock/departments.html")
with open(current_path / "mock/departments.html", "a") as mockfile:
response = get_response(create_request_session())
encoding = response.encoding if response.encoding else 'utf-8'

mockfile.write(response.content.decode(encoding))
487 changes: 487 additions & 0 deletions api/utils/mock/departments.html

Large diffs are not rendered by default.

File renamed without changes.
16 changes: 16 additions & 0 deletions api/utils/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from rest_framework.test import APITestCase
from .web_scraping import get_list_of_departments
from django.urls import reverse


class WebScrapingTest(APITestCase):

url = reverse('utils:departments')

def test_get_list_of_departments(self):
response = self.client.get(self.url)

departments = get_list_of_departments(response)
self.assertEqual(type(list()), type(departments))
if len(departments):
self.assertEqual(type(str()), type(departments[0]))
8 changes: 8 additions & 0 deletions api/utils/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path
from .views import mocked_departments

app_name = 'utils'

urlpatterns = [
path('mock/departments/', mocked_departments, name='departments'),
]
11 changes: 11 additions & 0 deletions api/utils/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.http import HttpResponse
from pathlib import Path


def mocked_departments(request):
mocked_html_path = Path(__file__).parent.absolute()

with open(mocked_html_path / 'mock/departments.html', 'r') as html_file:
content = html_file.read()

return HttpResponse(content, content_type='text/html')
3 changes: 1 addition & 2 deletions api/api/utils/web_scraping.py → api/utils/web_scraping.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
disciplines.get_disciplines() # Retorna um dicionário com as disciplinas
'''

def get_list_of_departments() -> Optional[List]:
def get_list_of_departments(response = get_response(create_request_session())) -> Optional[List]:
"""Obtem a lista de departamentos da UnB."""
response = get_response(create_request_session()) # Get the response from the request session
soup = BeautifulSoup(response.content, "html.parser") # Create a BeautifulSoup object
departments = soup.find("select", attrs={"id": "formTurma:inputDepto"}) # Find the <select> tag with id "formTurma:inputDepto"

Expand Down

0 comments on commit 8b47f14

Please sign in to comment.