Skip to content

Commit

Permalink
commit version v3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
LovelK7 committed Feb 22, 2024
1 parent 845ffc9 commit fd399ae
Show file tree
Hide file tree
Showing 11 changed files with 1,061 additions and 569 deletions.
Binary file modified __pycache__/mag_decl_webscrape.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/speleoliti_handler.cpython-311.pyc
Binary file not shown.
26 changes: 11 additions & 15 deletions config_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"Calculate declination"
],
"parse_file": [
"Generiraj CSV",
"Generate CSV"
"Pohrani u CSV",
"Save to CSV"
],
"readme_file_path": [
"surveyscraper_README.txt",
Expand All @@ -29,17 +29,13 @@
"Pogre\u0161ka prilikom u\u010ditanja readme datoteke!",
"Error while reading readme file!"
],
"file_path_csv": [
"Otvori csv datoteku",
"Open csv file"
],
"file_path_txt": [
"Otvori txt datoteku",
"Open txt file"
"file_path": [
"Otvori datoteku",
"Open file"
],
"file_path_error": [
"Nije otvorena pravilna datoteka!",
"Incorrect file was openned!"
"Incorrect file was opened!"
],
"success_run_true": [
"Uspje\u0161na konverzija!",
Expand All @@ -53,7 +49,11 @@
"Bez predznaka Excel \u0107e prepoznati to\u010dke kao brojeve i maknuti nule!",
"Without prefix Excel will read shots as numbers!"
],
"write_file_path": [
"empty_write_path": [
"Putanja do datoteke za pohranu je prazna!",
"The file write path is empty!"
],
"write_csv_file_path": [
"Pohrani csv datoteku",
"Save csv"
],
Expand All @@ -69,10 +69,6 @@
"Nije mogu\u0107e kreirati csv datoteku! Molim zatvori datoteku!",
"Unable to write to a csv file! Please close the file!"
],
"gui_md.title": [
"Magnetska deklinacija",
"Magnetic declination"
],
"location_error": [
"Prazan unos!",
"Empty input!"
Expand Down
50 changes: 32 additions & 18 deletions mag_decl_webscrape.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import ast
import json
import requests

class Retrieve_lat_lon():
def __init__(self, location):
self.location = location
self.user_agent = "SurveyScraper/3.0 (https://github.com/LovelK7/SurveyScraper)"

def retrieve_lat_lon(self):
"""retreive location coordinates through API with requests"""
url_location = (f"https://nominatim.openstreetmap.org/search?q={self.location}&format=json")
response = requests.get(url_location)
json_result = response.json()
latitude = float(json_result[0]['lat'])
longitude = float(json_result[0]['lon'])
return latitude, longitude

url_location = f"https://nominatim.openstreetmap.org/search.php?q={self.location}&format=jsonv2"
headers = {'User-Agent': self.user_agent}
response = requests.get(url_location, headers=headers)

if response.status_code == 200:
json_result = response.json()
latitude = float(json_result[0]['lat'])
longitude = float(json_result[0]['lon'])
return latitude, longitude
else:
print(f'Erorr while accessing nominatim.openstreetmap! \n {response.text}')
return None, None

class Retrieve_magn_decl():
def __init__(self, latitude, longitude, model, year, month, day):
Expand All @@ -24,17 +34,21 @@ def __init__(self, latitude, longitude, model, year, month, day):

def retrieve_magn_decl(self):
"""retreive magnetic declination through API with requests"""
url_decl = (f"https://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?lat1={self.latitude}&lon1={self.longitude}"
f"&model={self.model}&startYear={self.year}&startMonth={self.month}&startDay={self.day}&key=zNEw7&resultFormat=json")
response = requests.get(url_decl)
json_result = response.json()
magnetic_declination = json_result['result'][0]['declination']
return magnetic_declination
if self.latitude and self.longitude:
url_decl = (f"https://www.ngdc.noaa.gov/geomag-web/calculators/calculateDeclination?lat1={self.latitude}&lon1={self.longitude}"
f"&model={self.model}&startYear={self.year}&startMonth={self.month}&startDay={self.day}&key=zNEw7&resultFormat=json")
response = requests.get(url_decl)
json_result = response.json()
magnetic_declination = json_result['result'][0]['declination']
return magnetic_declination
else:
return None

#*************** ONLY FOR TESTING ************************
# if __name__ == '__main__':
# location, model, year, month, day = 'krkuz','IGRF','2022','6','16'
# lat_lon_app = Retrieve_lat_lon(location)
# latitude, longitude = lat_lon_app.retrieve_lat_lon()
# magn_decl_app = Retrieve_magn_decl(latitude, longitude, model, year, month, day)
# print(magn_decl_app.retrieve_magn_decl())
if __name__ == '__main__':

location, model, year, month, day = 'krkuz','IGRF','2022','6','16'
lat_lon_app = Retrieve_lat_lon(location)
latitude, longitude = lat_lon_app.retrieve_lat_lon()
magn_decl_app = Retrieve_magn_decl(latitude, longitude, model, year, month, day)
print(magn_decl_app.retrieve_magn_decl())
142 changes: 142 additions & 0 deletions speleoliti_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import os
import sys
import time
from tkinter import messagebox
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager

class Speleoliti_online():
"""
This script handles Speleoliti Online (https://speleoliti.speleo.net/online/app_en.html)
Methods: constructor, open_object, retrieve_cave_data, find_highest_point, update_fixed_station
"""
def __init__(self, headless, survey_path=None):
"""Initialize Selenium ChromeDriver"""

self.online = True
try:
driver_path = ChromeDriverManager().install()
except: # Exception as e:
#messagebox.showerror('Error', f'Error accessing Speleoliti Online !\n\n{e} \n\n Try using SurveyScraper offline!')
self.online = False
#driver_path = 'c:/Users/Lovel.IZRK-LK-NB/.wdm/drivers/chromedriver/win64/120.0.6099.225/chromedriver-win32/chromedriver.exe'
if self.online:
service = Service(driver_path)
options = webdriver.ChromeOptions()
self.headless = headless
if headless:
options.add_argument("--headless=new")
try:
self.driver = webdriver.Chrome(service=service, options=options)
except Exception as e:
messagebox.showerror('Error', f'Error initializing ChromeDriver: !\n\n{e}')
self.driver.quit()
if not headless:
self.driver.minimize_window()
self.handle_of_the_window = self.driver.current_window_handle
self.url = f"https://speleoliti.speleo.net/online/app_en.html"
# Access survey data file
survey_data_filename = 'survey_data.json'
if getattr(sys,'frozen', False): #check if the app runs as a script or as a frozen exe file
self.survey_data_filepath = os.path.join(os.path.dirname(sys.executable), survey_data_filename)
elif __file__:
self.survey_data_filepath = os.path.join(os.path.dirname(__file__), survey_data_filename)
else:
self.survey_data_filepath = survey_path

def open_empty_object(self):
"""Opens an empty window of Speleoliti Online"""
self.driver.get(self.url)

def open_object(self):
"""Opens a cave survey file into Speleoliti Online"""
try:
self.driver.get(self.url)
WebDriverWait(self.driver, 3).until(expected_conditions.element_to_be_clickable((By.XPATH, '//*[@id="DefOpt2"]'))).click()
self.driver.find_element("xpath",'//*[@id="impexRadio_file"]').click() # find file import radiobutton
file_input = self.driver.find_element("xpath",'//*[@id="UploadFileFld"]') # insert cave survey file path
file_input.send_keys(self.survey_data_filepath) # direct input of json file content to an input field
self.driver.find_element("xpath",'//*[@id="impex"]/table/tbody/tr/td/div/input').click() # confirm import
WebDriverWait(self.driver, 3).until(expected_conditions.alert_is_present())
self.driver.switch_to.alert.accept() # accept alert message
except Exception as e:
messagebox.showerror('Error', f'Error while opening object!\n\n{e}')
return e

def retrieve_cave_data(self):
"""retrieve survey data from"""
poly_length = self.driver.find_element("xpath",'//*[@id="table99"]/tbody/tr[4]/td[2]').text.split()[0]
hor_length = self.driver.find_element("xpath",'//*[@id="table99"]/tbody/tr[5]/td[2]').text.split()[0]
elevation = self.driver.find_element("xpath",'//*[@id="table99"]/tbody/tr[6]/td[2]').text.split()[0]
depth = self.driver.find_element("xpath",'//*[@id="table99"]/tbody/tr[7]/td[2]').text.split()[0]
return poly_length, hor_length, elevation, depth

def find_highest_point(self):
"""Find highest elevated station so it becomes the fixed station by default"""
try:
WebDriverWait(self.driver, 3).until(expected_conditions.element_to_be_clickable((By.XPATH, '//*[@id="ico_coords"]'))).click()
#self.driver.find_element("xpath",'//*[@id="ico_coords"]').click() # switch to coordinate table
data_tbl = self.driver.find_element("xpath",'//*[@id="table2b"]/tbody')
station_alts = {}
for index, row in enumerate(data_tbl.find_elements('xpath','.//tr')):
if index == 0:
continue
station = row.find_element('xpath',f'//*[@id="table2b"]/tbody/tr[{index+1}]/td[1]').text
alt = row.find_element('xpath',f'//*[@id="table2b"]/tbody/tr[{index+1}]/td[4]/div').text
station_alts[station] = float(alt)
self.driver.find_element("xpath",'//*[@id="ico_main"]').click() # return to main menu
key_of_max_value = max(station_alts, key=station_alts.get)
return key_of_max_value
except Exception as e:
messagebox.showerror('Error', f'Error finding the highest point!\n\n{e}')
return e

def update_fixed_station(self, fixed_station):
"""Update the given fixed station"""
try:
self.driver.find_element("xpath",'//*[@id="ico_survey"]').click() # switch to edit survey table
fixed_station_elm = self.driver.find_element("xpath",'//*[@id="survey_fix"]') # find fixed station element
self.driver.execute_script('arguments[0].value = ""', fixed_station_elm) # delete current entry
fixed_station_elm.click()
fixed_station_elm.send_keys(fixed_station) # insert new entry
self.driver.find_element("xpath",'//*[@id="ico_main"]').click() # return to main menu
except Exception as e:
messagebox.showerror('Error', f'Error updating the fixed station!\n\n{e}')
return e

def restore_window(self):
self.driver.switch_to.window(self.handle_of_the_window)
self.driver.set_window_rect(0, 0)

def close_driver(self):
if self.driver:
self.driver.quit()

#*************** ONLY FOR TESTING ************************
if __name__ == '__main__':

curr_time = time.time()
# Set path to a current cave survey data
survey_data_filename = 'survey_data.json'
if getattr(sys,'frozen', False): #check if the app runs as a script or as a frozen exe file
survey_data_filepath = os.path.join(os.path.dirname(sys.executable), survey_data_filename)
elif __file__:
survey_data_filepath = os.path.join(os.path.dirname(__file__), survey_data_filename)

speleoliti_app = Speleoliti_online(headless=False, survey_path=survey_data_filepath)
if speleoliti_app.online:
speleoliti_app.open_object()
fixed_station = speleoliti_app.find_highest_point()
speleoliti_app.update_fixed_station(fixed_station)
dimensions = speleoliti_app.retrieve_cave_data()
#speleoliti_app.restore_window()
speleoliti_app.close_driver()
print(dimensions)
duration = time.time() - curr_time
print('Duration: ',duration)
else:
print('Unable to reach Speleoliti Online due to internet connection!')
51 changes: 51 additions & 0 deletions survey_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"fix": "ed3",
"x": "",
"y": "",
"z": "",
"dcl": 4.332,
"name": "jama_Edison_2",
"descr": "ed",
"viz": [
"null",
{
"t1": "ed1",
"t2": "ed0",
"l": 6.13,
"a": 61.93,
"f": -82.4,
"left": "null",
"right": "null",
"up": "null",
"down": "null",
"note": "",
"flags": ""
},
{
"t1": "ed1",
"t2": "ed2",
"l": 4.02,
"a": 122.83,
"f": 71.9,
"left": "null",
"right": "null",
"up": "null",
"down": "null",
"note": "",
"flags": ""
},
{
"t1": "ed2",
"t2": "ed3",
"l": 1.91,
"a": 287.83,
"f": 66.6,
"left": "null",
"right": "null",
"up": "null",
"down": "null",
"note": "",
"flags": ""
}
]
}
Loading

0 comments on commit fd399ae

Please sign in to comment.