-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryo_parse.py
95 lines (80 loc) · 3.79 KB
/
cryo_parse.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
from bs4 import BeautifulSoup
import re
from datetime import datetime
import lxml
from requests_html import HTMLSession
import sqlite3 as sq
import logging
# logging configuration
logging.basicConfig(filename=r'C:\Users\operator\PycharmProjects\cryobot\parse.log',
format='%(asctime)s '
'LOGGER=%(name)s '
'MODULE=%(module)s.py '
'FUNC=%(funcName)s'
' %(levelname)s '
'%(message)s',
datefmt='%d-%m-%Y %H:%M:%S',
level='INFO',
encoding='utf8')
logger = logging.getLogger('parse')
# URLS = ['http://10.1.150.247/accueil.zhtml', 'http://10.1.150.246/accueil.zhtml']
URLS = ['http://10.1.150.247', 'http://10.1.150.246']
def cryo(url):
"""parsing web interface of cryo storage"""
session = HTMLSession()
response = session.get(url)
try:
response.html.render(timeout=50)
bs = BeautifulSoup(response.html.html, 'lxml')
table = bs.find_all('table')[5]
row_temper = table.find_next('span', class_='Style4').get_text()
temper = int(re.findall(r'-\d+', row_temper)[0])
row_level = table.find_next('span', class_='Style4').find_next('span', class_='Style4').get_text()
level = float(re.findall(r'\d+.\d+|\d+', row_level)[0])
logger.info(f'parsing web interface cryostorage {url}')
except Exception as e:
logger.error(f'Parsing error, URL={url}, ERROR={e}')
temper = 'Timeout'
level = 'Timeout'
return temper, level
if __name__ == '__main__':
# making a list with parsed data
res = []
i = 1
for url in URLS:
temper, level = cryo(url)
res.append((i, temper, level))
i += 1
# temporary code for planned defrosting ##########
# temper, level = cryo(URLS[0])
# res.append((i, temper, level))
# i += 1
# temper, level = 'ПЛАНОВАЯ РАЗМОРОЗКА', 'ПЛАНОВАЯ РАЗМОРОЗКА'
# res.append((i, temper, level))
# write data to file cryodata ###
try:
with open(r'C:\Users\operator\PycharmProjects\cryobot\cryodata', 'w', encoding="utf-8") as f:
for item in res:
f.write(f'{datetime.now().strftime("%Y-%m-%d %H:%M:%S")} Криохран № {item[0]} Температура: {item[1]}°C Уровень: {item[2]} см \n')
logger.info('Record data to cryodata file was successful')
except Exception as e:
logger.error(f'Error was occured during writing the file cryodata : {e}')
# write data to file cryoweb ###
try:
with open(r'C:\Users\operator\PycharmProjects\cryobot\cryoweb', 'w', encoding="utf-8") as c:
for item in res:
c.write(f'{datetime.now().strftime("%d-%m-%Y %H:%M")} Cryogenic storage # {item[0]} Temp: {item[1]}°C Level: {item[2]} cm \n')
logger.info('Record data to cryoweb file was successful')
except Exception as e:
logger.error(f'Error was occured during writing the file cryoweb : {e}')
# res = [(1, -166, 4.6), (2, -159, 4.4)] - образец финального списка с данными
# write data to database SQLite cryodata.db ###
try:
with sq.connect(r"C:\Users\operator\PycharmProjects\cryobot\cryodata.db") as con:
cur = con.cursor()
dt = datetime.now()
date = dt.strftime('%d-%m-%Y')
cur.execute(f'INSERT INTO cryo (date, time, temper_1, level_1, temper_2, level_2) VALUES ("{date}",time("now", "localtime"),{res[0][1]},{res[0][2]},{res[1][1]},{res[1][2]})')
logger.info('Record data to database was successful')
except Exception as e:
logger.error(f'Database record error was occured : {e}')