forked from wallforfry/ADE-ESIEE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunites_api.py
125 lines (102 loc) · 3.4 KB
/
unites_api.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Project : ADE-ESIEE
File : unites_api.py
Author : DELEVACQ Wallerand
Date : 23/05/2017
"""
import csv
from html.parser import HTMLParser
import requests
class UnitesParser(HTMLParser):
"""
Parse data unites intra website
"""
def __init__(self):
super().__init__()
self.recording = 0
self.data = []
self._data = ""
self._starttag = None
def handle_starttag(self, tag, attrs):
if tag != 'font':
return
self._starttag = tag
def handle_data(self, data):
self._data = data
def handle_endtag(self, tag):
if self._starttag == "font":
self.data.append(self._data)
self._data = ""
self._starttag = None
def generate_csv_file(row):
"""
Generate csv file from the row
:param row: Row of the web site
:return: None
"""
parser = UnitesParser()
parser.feed(row)
data = parser.data
with open('unites.csv', 'w', newline='', encoding="iso-8859-1") as csvfile:
writer = csv.writer(csvfile)
for i in range(2, len(data), 9):
writer.writerow(
[data[i + 1].replace("\n", "").replace("\t", ""), data[i + 2].replace("\n", "").replace("\t", ""),
data[i + 3].replace("\n", "").replace("\t", ""), data[i + 4].replace("\n", "").replace("\t", ""),
data[i + 5].replace("\n", "").replace("\t", ""), data[i + 6].replace("\n", "").replace("\t", ""),
data[i + 7].replace("\n", "").replace("\t", "")])
return None
def get_row_on_website():
"""
Return row of website
:return: All row of the website
"""
"""url = 'https://extra.esiee.fr/intranet/consultation_brochures/index.php'
payload = {"s_annee_scolaire": "",
"s_pays": "FRA",
"s_ecole": "",
"s_id_laboratoire": "",
"s_cursus": "",
"unite": "",
"affichage": "0",
"b_rechercher": "Rechercher"
}
result = requests.session().post(
url,
headers=dict(referer=url),
data=payload
)
result = None
"""
#Encodage for linux server only
with open("site.html", encoding="iso-8859-1", mode="r") as f:
#Encodage for windows
#with open("site.html", mode="r") as f:
result = f.read()
return result
def search_unite(unite_code):
"""
Search unite in csv file and get its natural name
:param unite_code: Code like "IGI-2102"
:return: "Name of the unite"
"""
with open("unites.csv", encoding="iso-8859-1") as f:
csv_file = csv.reader(f, delimiter=",")
for row in csv_file:
# if current rows 2nd value is equal to input, print that row
if unite_code == row[1]:
return row[2]
def search_unite_from_csv(unite_code):
url = "http://test.wallforfry.fr/BDE_UNITES.csv"
response = requests.get(url)
#f = response.content.decode("ISO-8859-1")
f = response.content.decode("utf-8")
fieldsnames = ["Code.Unité", "Libellé.Unité"]
data = csv.DictReader(f.splitlines(), fieldsnames, delimiter=';')
for row in data:
if unite_code.replace("-", "_") in row.get(fieldsnames[0]):
return row.get(fieldsnames[1])
return ""
if __name__ == "__main__":
result = get_row_on_website()
generate_csv_file(result)