-
Notifications
You must be signed in to change notification settings - Fork 0
/
htmlPdf.py
59 lines (52 loc) · 2.32 KB
/
htmlPdf.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
import pdfkit
import requests
import json
import re
urlCorsi = "https://ludotosk.it/corsi"
urlMaster = "https://json-server-corsi.herokuapp.com/master"
theadCorsi = "<thead><tr><th scope='col'>#</th><th scope='col'>Nome corso</th><th scope='col'>Unviersità</th><th scope='col'>Test</th><th scope='col'>Città</th></tr></thead>"
theadMaster = "<thead><tr><th scope='col'>#</th><th scope='col'>Nome corso</th><th scope='col'>Unviersità</th><th scope='col'>Durata</th><th scope='col'>Lingua</th><th scope='col'>Città</th></tr></thead>"
tipoMaster = 'tipo'
tipoCorsi = 't'
def pdf(name, db, tipo):
global theadCorsi
global urlCorsi
global tipoCorsi
global tipoMaster
global urlMaster
global theadMaster
tbody = '<tbody>'
i = 1
if db == 1:
thead = theadCorsi
url = urlCorsi
varTipo = tipoCorsi
elif db == 2:
url = urlMaster
thead = theadMaster
varTipo = tipoMaster
ploads = {varTipo: tipo}
r = requests.get(url, params=ploads)
corsi = json.loads(r.text)
if db == 1:
for corso in corsi:
if re.search(name, corso["n"], re.IGNORECASE):
tbody += '<tr><th scope="row">' + str(i) + '</th><td><a href="' + corso["h"] + '">' + corso["n"].replace('à', 'à') + '</a></td><td>' + corso["u"].replace(
'à', 'à') + '</td><td>' + corso["a"].replace('ì', 'ì') + '</td><td>' + corso["s"] + '</td><tr>'
i += 1
tbody += '</tbody>'
elif db == 2:
for corso in corsi:
if re.search(name, corso["corso"], re.IGNORECASE):
tbody += '<tr><th scope="row">' + str(i) + '</th><td><a href="' + corso["link"] + '">' + corso["corso"] + '</a></td><td>' + corso["uni"].replace('à', 'à') + '</td><td>' + corso["durata"] + '</td><td>' + corso["lingua"].upper() + '</td><td>' + corso["citta"] + '</td><tr>'
i += 1
tbody += '</tbody>'
if i == 1:
return 0
f = open("prova.html", "r")
html = f.read()
f.close()
html = html.replace("<h1 class='display-6'></h1>", "<h1 class='display-6'>Report " + tipo + " in " + name + ".</h1>")
html = html.replace("<thead></thead>", thead)
html = html.replace("<tbody></tbody>", tbody)
return pdfkit.from_string(html, False)