-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmean.py
72 lines (55 loc) · 2.74 KB
/
mean.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
66
67
68
69
70
71
72
import requests
from lxml import html
from urlparse import urlparse
from bs4 import BeautifulSoup
import json
if __name__ == '__main__':
s = requests.Session()
r = s.get(url="https://paco.ua.pt/secvirtual/c_historiconotas.asp")
username = "EMAIL"
password = "PASSWORD"
tree = html.fromstring(r.content)
o = urlparse(r.url)
url_auth = o.scheme + "://" + o.netloc + tree.body.forms[0].attrib["action"]
r = s.post(url_auth, data={'j_password': password, 'j_username': username, 'Submeter': 'OK'})
tree = html.fromstring(r.content)
inputs = tree.xpath("//input")
payload = {'RelayState': inputs[0].value, 'SAMLResponse': inputs[1].value}
r = s.post(tree.body.forms[0].attrib["action"], data=payload)
r = s.get("https://paco.ua.pt/secvirtual/c_planocurr.asp")
soup = BeautifulSoup(r.content)
table = soup.find('table', attrs={'width':'95%', 'align':'center', 'cellspadding':'2'})
cadeiras = []
for row in table.findAll("tr"):
cells = row.findAll("td")
if len(cells) == 8 and cells[1].text.rstrip().replace("\r\n\t", "") != 'Codigo':
if len(cells[7].text.rstrip().replace("\r\n\t", "")) != 0:
cadeiras += [{'codigo': int(cells[1].text.rstrip().replace("\r\n\t", "")),
'nome': cells[2].text.rstrip().replace("\r\n\t", ""),
'ano': int(cells[3].text.rstrip().replace("\r\n\t", "")),
'semestre': int(cells[4].text.rstrip().replace("\r\n\t", "")),
'ects':float(cells[6].text.rstrip().replace("\r\n\t", "").replace(",", ".")),
'nota': float(cells[7].text.rstrip().replace("\r\n\t", "").replace(",", "."))}]
nota = 0.0
creditos = 0.0
for cadeira in cadeiras:
nota += (cadeira["nota"] * cadeira["ects"])
creditos += cadeira["ects"]
nota = nota / creditos
semestres_ano = []
for cadeira in cadeiras:
if {"ano": cadeira["ano"], "semestre": cadeira["semestre"]} not in semestres_ano:
semestres_ano += [{"ano": cadeira["ano"], "semestre": cadeira["semestre"]}]
# notas por semestre e ano
for semestre in semestres_ano:
semestre["ects"] = 0
semestre["nota"] = 0
for cadeira in cadeiras:
if cadeira["ano"] == semestre["ano"] and cadeira["semestre"] == semestre["semestre"]:
semestre["ects"] += cadeira["ects"]
semestre["nota"] += (cadeira["nota"] * cadeira["ects"])
semestre["nota"] /= semestre["ects"]
print "ECTS: " + str(creditos)
print "Nota: " + str(nota)
print "N. de Cadeiras: " + str(len(cadeiras))
# print json.dumps({'cadeiras': cadeiras, 'media': nota, 'semestres': semestres_ano})