Sonnendach/Screenshot_test.py

134 lines
5.3 KiB
Python
Raw Normal View History

2022-03-19 11:23:37 +01:00
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from PIL import Image
import os
import qrcode
from selenium.webdriver.common.action_chains import ActionChains
file_split_char = ","
filename_adresslist = "GWR-Daten Schaffhausen.csv"
adress_file = open(filename_adresslist, "r")
adress_list = adress_file.read().splitlines()
adress_file.close()
s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)
driver.minimize_window()
driver.maximize_window()
driver.get("https://www.uvek-gis.admin.ch/BFE/sonnendach/")
2022-07-27 17:30:05 +02:00
driver.implicitly_wait(5)
2022-03-19 11:23:37 +01:00
try:
if(not os.path.exists("screenshots")):
os.makedirs("screenshots", exist_ok=False)
if (not os.path.exists("qrcodes")):
os.makedirs("qrcodes", exist_ok=False)
for i in range(len(adress_list)):
line = adress_list[i]
print(line)
adress = line.split(file_split_char)
if((line != adress_list[0]) & ((adress[20][0:11] != "screenshots"))):
print(adress)
search_string = adress[15] + " " + adress[3] + " " + adress[11] + " " + adress[13]
print(search_string)
search_bar = driver.find_element(By.ID, "searchTypeahead1")
search_bar.send_keys(Keys.CONTROL + "a")
search_bar.send_keys(Keys.DELETE)
search_bar.send_keys(search_string)
driver.implicitly_wait(10)
found = False
suggestions = driver.find_elements(By.XPATH, "//div[@class='tt-suggestion tt-selectable']")
for suggestion in suggestions:
if(suggestion.text == search_string):
found = True
suggestion.click()
driver.implicitly_wait(120)
break
if(found):
print("found")
time.sleep(2)
url = driver.current_url
eignung = driver.find_element(By.ID, "eignung")
image_filename = eignung.text + " - " + search_string
adress[18] = url
adress[19] = eignung.text
adress[20] = image_filename
#adress_file = open(filename_adresslist, "w")
new_line_string = ""
for j in adress:
new_line_string = new_line_string + j + file_split_char
adress_list[i] = new_line_string
new_adress_list = ""
for j in adress_list:
new_adress_list = new_adress_list + (j) + "\n"
#adress_file.write(new_adress_list)
#adress_file.close()
print("start")
# Create QR-Code
qr = qrcode.QRCode(version=1, box_size=10, border=5)
qr.add_data(url)
qr.make(fit=True)
qr.make_image(fill='black', back_color='white').save(
"qrcodes/" + image_filename + "production" + ".png")
# Create Screenshot
2022-07-27 17:30:05 +02:00
actions = ActionChains(driver)
2022-03-19 11:23:37 +01:00
try:
driver.execute_script("""var l = document.getElementsByClassName("ol-zoom ol-unselectable ol-control")[0];
l.parentNode.removeChild(l);""")
except:
pass
mapElement = driver.find_element(By.XPATH,
"//div[@id='map']//div[@class='ol-viewport']")
location = mapElement.location
size = mapElement.size
x = location["x"]
2022-07-27 17:30:05 +02:00
y = location["y"]
2022-03-19 11:23:37 +01:00
w = x + size["width"]
2022-07-27 17:30:05 +02:00
h = size["height"]
area = (x, 0, w, h)
driver.execute_script("window.scrollTo(0, " + str(y) + ")")
2022-03-19 11:23:37 +01:00
time.sleep(1)
driver.save_screenshot("screenshots/" + image_filename + "map" + ".png")
time.sleep(0.2)
Image.open("screenshots/" + image_filename + "map" + ".png").crop(area).save("screenshots/" + image_filename + "map" + ".png")
#take Screenshot 2
2022-07-27 17:30:05 +02:00
chartElement = driver.find_elements(By.XPATH, "//div[@id='chart']//*")[0]
2022-03-19 11:23:37 +01:00
location = chartElement.location
size = chartElement.size
2022-07-27 17:30:05 +02:00
x = location["x"]
y = location["y"]
w = x + size["width"]
h = size["height"]
area = (x, 0, w, h)
driver.execute_script("window.scrollTo(0, " + str(y) + ")")
print("X: " + str(x) + " Y: "+ str(y) + " W: "+ str(w) + " H: " + str(h))
2022-03-19 11:23:37 +01:00
time.sleep(1)
driver.save_screenshot("screenshots/" + image_filename + "production" + ".png")
time.sleep(0.2)
Image.open("screenshots/" + image_filename + "production" + ".png").crop(area).save("screenshots/" + image_filename + "production" + ".png")
print("stop")
print(image_filename + " was saved.")
else:
print("not found: " + search_string)
if(input("Enter zum fortfahren") == "exit"):
break
except Exception as e:
print(str(e))
driver.quit()