-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathreimport_som_crawlers_zips.py
65 lines (57 loc) · 2.08 KB
/
reimport_som_crawlers_zips.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 -*-
from datetime import datetime
import dbconfig
from erppeek import Client
"""_summary_
Reimporta fitxers ZIPs trobats en Resultats de som_crawlers que han acabat amb Failed
Aquest script s'ha d'executar després de la pimera onada de crawlers (7 hores)
"""
O = Client(**dbconfig.erppeek) # noqa: E741
# Obtenir la llista de resultats a tractar
craw_ids = O.SomCrawlersTask.search([("resultat_bool", "=", False)])
total_crawlers = 0
total_reintents = 0
total_correctes = 0
for craw_id in craw_ids:
attachment = []
craw = O.SomCrawlersTask.browse(craw_id)
if craw.run_ids:
last_result = craw.run_ids[0]
attachment_ids = O.IrAttachment.search(
[
("res_id", "=", last_result.id),
("name", "ilike", ".zip"),
]
)
if not attachment_ids:
continue
total_crawlers += 1
total_reintents += len(attachment_ids)
# Reimportar xml
result = True
output = "[{} REIMPORTACIONS]:\n\n".format(total_reintents)
for attachment_id in attachment_ids:
attachment = O.IrAttachment.browse(attachment_id)
try:
output += craw.task_step_ids[-1].import_wizard(attachment.name, attachment.datas)
except Exception as e:
output += "Error carregant el fitxer {}: {} \n".format(attachment.name, str(e))
result = False
else:
total_correctes += 1
output += "\n{} reimportats correctament".format(total_correctes)
# Afegir nou resultat al crawler
O.SomCrawlersResult.create(
{
"task_id": craw_id,
"data_i_hora_execucio": datetime.now().strftime("%Y-%m-%d_%H:%M"),
"resultat_bool": result,
"resultat_text": output,
}
)
print """
Hem trobat {} crawlers per reimportar.
S'han intentat reimportat {} fitxers.
S'han reimportat correctament {} fitxers.""".format( # noqa: E999
total_crawlers, total_reintents, total_correctes
)