-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_waybills.py
78 lines (59 loc) · 2.67 KB
/
upload_waybills.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
import psycopg2
import os
import xml.etree.ElementTree as ET
from ftp_dowload import dowload_new_waybills
def update_fact_waybills():
read_conn = psycopg2.connect(dbname='taxi', user='etl_tech_user',
password='etl_tech_user_password', host='de-edu-db.chronosavant.ru')
read_cursor = read_conn.cursor()
write_conn = psycopg2.connect(dbname='dwh', user='dwh_krasnoyarsk',
password='dwh_krasnoyarsk_uBPaXNSx', host='de-edu-db.chronosavant.ru')
write_cursor = write_conn.cursor()
#Загрузка новых файлов
dowload_new_waybills()
directory = 'waybills/'
files = os.listdir(directory)
error_files = []
write_cursor.execute("SELECT MAX(waybill_num) FROM fact_waybills")
res = write_cursor.fetchall()
if res != [(None,)]:
waybill_num = res[-1][0] + 1
else:
waybill_num = 0
# print(files)
for file in files:
try:
#выборка данных
tree = ET.parse('waybills/'+file)
plate_num = tree.findall('waybill/car')[0].text #car num
license = tree.findall('waybill/driver/license')[0].text
work_start_dt = tree.findall('waybill/period/start')[0].text
work_end_dt = tree.findall('waybill/period/stop')[0].text
issue_dt = tree.findall('./')[0].attrib['issuedt']
write_cursor.execute(f"SELECT personnel_num FROM dim_drivers WHERE driver_license_num = '{license}'")
driver_pers_num = write_cursor.fetchall()[-1][0]
# print(waybill_num, driver_pers_num, plate_num, work_start_dt, work_end_dt, issue_dt)
#запись данных
write_cursor.execute('INSERT INTO fact_waybills VALUES(%s, %s, %s, %s, %s, %s);',
(waybill_num, driver_pers_num, plate_num, work_start_dt, work_end_dt, issue_dt))
write_conn.commit()
waybill_num += 1
os.remove('waybills/'+file)
except Exception as e:
# print(e)
# print(file)
# os.remove('waybills/'+file)
error_files.append(file)
#возможно стоити перенести в другое место и добавить проверку на ошибки
if files != []:
with open('last_waybill.txt', 'w') as f:
f.write(files[-1])
if error_files != []:
print('Problems with files:' + ' '.join(error_files))
write_cursor.close()
write_conn.close()
read_cursor.close()
read_conn.close()
return error_files
if __name__ == '__main__':
update_fact_waybills()