-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
65 lines (52 loc) · 2.06 KB
/
helpers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
import settings
from models import *
from parsel import Selector
def generate_sections(data):
sections = []
if len(data) > 0:
sections.append(Section(index=0, title="Todos", subsections=[]))
for index, element in enumerate(data):
sel = Selector(text=element)
title_text = sel.xpath(
"//h4/text()"
).get() # TODO: checar se esse xpath retorna o child e, caso não retornar, usar só "h4/text()"
if title_text is not None and title_text != "":
subsections = generate_subsections(element)
section = Section(
index=index + 1,
title=title_text,
subsections=subsections,
source=data,
)
sections.append(section)
return sections
def generate_subsections(data):
subsections = []
sel = Selector(text=data)
children = sel.xpath("//a").getall()
if len(children) > 0:
subsections.append(Subsection(index=0, title="Todos", source=None))
for index, element in enumerate(children):
sel = Selector(text=element)
title_text = sel.xpath("normalize-space(string(//a))").get()
url = sel.xpath("//a/@href").get()
if title_text is not None and title_text != "":
subsections.append(
Subsection(index=index + 1, title=title_text, source=data, url=url)
)
return subsections
def print_init(url):
print(
f"Aguarde um instante enquanto o portal do INEP está sendo acessado ({url})..."
)
def print_welcome(title, subtitle):
len_title = len(title)
len_subtitle = len(subtitle)
print("".join(str("#") for x in range(len_title * 2 + len_subtitle)))
print(f"#\t{title}\t#")
print(f"#\t{subtitle}\t#")
print("".join(str("#") for x in range(len_title * 2 + len_subtitle)) + "\n")
print(
f"Em caso de erro no programa, abra uma issue no link: { settings.OPEN_ISSUE_URL }\n"
)