-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
186 lines (171 loc) · 5.44 KB
/
main.py
File metadata and controls
186 lines (171 loc) · 5.44 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import asyncio
import base64
import logging
import os, glob
from dotenv import load_dotenv
import hashlib
import sys
from datetime import datetime
import argparse
from src.a_moins_b import a_moins_b
from src.export_pictures import export_pictures
from src.export_ind import export_ind
from src.import_ind import import_ind
from src.import_pictures import import_pictures
logging.basicConfig(level=logging.INFO)
logger: logging.Logger = logging.getLogger(__name__)
load_dotenv()
basic_auth = [
os.getenv('STC_API_USERNAME', ''),
os.getenv('STC_API_PASSWORD', ''),
]
basic_auth = [value for value in basic_auth if value is not None]
joined_auth = ':'.join(map(str, basic_auth)).encode('utf-8')
url = f"{os.getenv('STC_API_BASEURL', 'https://taiga.archi.fr')}/taiga_libext/JsonRPC/api.php"
headers = {
"Authorization": f"Basic {base64.b64encode(joined_auth).decode('utf-8')}",
"Content-Type": "application/json; charset=utf-8",
}
ensa_pass = os.getenv('STC_API_PASSENSA') + datetime.now().strftime('%Y%m%d')
ensa_infos = {
"code_ensa": os.getenv('STC_API_CODEENSA', 'lyon'),
"pass_ensa": hashlib.sha1(ensa_pass.encode()).hexdigest(),
}
print(ensa_infos)
# print(headers)
collections = [
{
"function": export_ind,
"method": "ExportInd",
"params": {
**ensa_infos,
"type": "pri",
"id": "*",
},
},
{
"function": export_ind,
"method": "ExportInd",
"params": {
**ensa_infos,
"type": "etd",
"id": "*",
},
},
{
"function": export_ind,
"method": "ExportInd",
"params": {
**ensa_infos,
"type": "adm",
"id": "*",
},
},
{
"function": export_ind,
"method": "ExportInd",
"params": {
**ensa_infos,
"type": "esn",
"id": "*",
},
},
{
"function": export_pictures,
"method": "ExportPhotos",
"methodBase64": "ExportPhoto",
"params": {
**ensa_infos,
"type": "etd",
"id": "*",
},
"paramsBase64": {
"type": "etd",
**ensa_infos,
},
},
{
"function": export_pictures,
"method": "ExportPhotos",
"methodBase64": "ExportPhoto",
"params": {
**ensa_infos,
"type": "adm",
"id": "*",
},
"paramsBase64": {
"type": "adm",
**ensa_infos,
},
},
{
"function": export_pictures,
"method": "ExportPhotos",
"methodBase64": "ExportPhoto",
"params": {
**ensa_infos,
"type": "esn",
"id": "*",
},
"paramsBase64": {
"type": "esn",
**ensa_infos,
},
},
]
async def main():
parser = argparse.ArgumentParser()
parser.add_argument('--run', help='all | taiga | sesame',default='all')
parser.add_argument('--imports', help='all | ind | pictures',default='all')
parser.add_argument('--an', help='Année universitaire à importer',default="0")
parser.add_argument('--force', help="Force l'import et bypass le check du fingerprint",default="0")
args = parser.parse_args()
if args.an != '0':
print(f"Import pour l'annee <{args.an}>")
if args.an.isdigit():
args.an = int(args.an)
if args.an < 2000 or args.an > datetime.now().year + 1:
print("Année invalide, doit être entre 2000 et l'année en cours + 1")
sys.exit(1)
for col in collections:
col.get('params')['au']=args.an
else:
annee_en_cours = datetime.now().year
print("Import pour l'annee en cours <" + str(annee_en_cours) + ">")
for col in collections:
col.get('params')['au']=annee_en_cours
if args.run == 'taiga' or args.run == 'all':
logger.info("Starting Taiga ind/pictures crawler...")
print(f"Imports: {args.imports}")
await a_moins_b(url, 0, -1, headers)
# suppression des fichiers cache
listjson=glob.glob('./cache/*.json')
for file in listjson:
os.remove(file)
if args.imports == 'ind':
collection_tasks = [col.get('function')(url, col, headers) for col in collections if col.get('method') != 'ExportPhotos']
elif args.imports == 'pictures':
collection_tasks = [col.get('function')(url, col, headers) for col in collections if col.get('method') == 'ExportPhotos']
else:
collection_tasks = [col.get('function')(url, col, headers) for col in collections]
await asyncio.gather(*collection_tasks)
print("Taiga crawler ended successful !!!")
if args.run == 'sesame' or args.run == 'all':
print("Starting import_ind/pictures...")
print(f"Imports: {args.imports}")
start_time = datetime.now()
if (args.imports == 'ind' or args.imports == 'all'):
await import_ind(args.force)
if (args.imports == 'pictures' or args.imports == 'all'):
await import_pictures()
end_time = datetime.now()
execution_time = end_time - start_time
print(f"import_ind completed in {execution_time}")
if __name__ == '__main__':
loop = asyncio.get_event_loop()
# A Activer pour debugging
#loop.set_debug(True)
try:
loop.run_until_complete(main())
finally:
loop.close()