sensors/export/main.py

78 lines
2.3 KiB
Python
Raw Normal View History

2022-12-19 19:30:06 +00:00
import time
import traceback
2022-12-19 19:30:06 +00:00
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
2023-04-18 20:25:22 +00:00
from selenium.webdriver.chrome.service import Service
from selenium.common.exceptions import NoSuchElementException, WebDriverException
2023-04-18 20:25:22 +00:00
from webdriver_manager.chrome import ChromeDriverManager
2023-10-03 09:33:06 +00:00
import undetected_chromedriver as uc
print('Sleeping 10s before loading page...')
time.sleep(10)
2022-12-19 19:30:06 +00:00
2023-04-18 20:25:22 +00:00
ser = Service('/usr/lib/chromium-browser/chromedriver')
2023-10-03 09:33:06 +00:00
chrome_options = uc.ChromeOptions()
2022-12-19 19:30:06 +00:00
chrome_options.add_argument('--headless')
chrome_options.add_argument('start-maximized')
2022-12-19 19:30:06 +00:00
chrome_options.add_argument('--no-sandbox')
2023-05-08 21:15:39 +00:00
try:
driver = uc.Chrome(service=ser, options=chrome_options)
except WebDriverException as e:
print('Wrong chrome driver version, extracting correct version...')
tb = traceback.format_exc()
version_string = tb.split('Current browser version is ')[1]
major_version = version_string.split('.')[0]
print('Trying version:', major_version)
chrome_options = uc.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('start-maximized')
chrome_options.add_argument('--no-sandbox')
driver = uc.Chrome(service=ser, options=chrome_options, version_main=int(major_version))
2023-05-08 21:15:39 +00:00
try:
driver.get('https://sensors.dns.t0.vc')
time.sleep(3)
driver.execute_script("return document.getElementsByClassName('menu')[0].remove();")
graphs = [
'Solar_Power',
'Living_Room_Air',
'Outside_Temperature',
'Bedroom_Temperature',
'Nook_Temperature',
2024-07-15 19:41:03 +00:00
#'Misc_Temperature',
'Basement_Temperature',
2023-05-08 21:15:39 +00:00
'Nook_Thermostat',
'Gas_Usage',
'Water_Usage',
'Living_Room_Lux',
]
for graph in graphs:
print('Capturing', graph, 'graph...')
try:
element = driver.find_element(By.ID, graph)
except NoSuchElementException:
print('Graph not found, skipping.')
continue
driver.execute_script('arguments[0].scrollIntoView({block: "center"});', element)
with open('/home/tanner/sensors/export/data/{}.png'.format(graph), 'wb') as f:
f.write(element.screenshot_as_png)
finally:
driver.close()
driver.quit()
2023-04-18 20:25:22 +00:00
print('done.')