ADD callbook.py init release 1.0.0

This commit is contained in:
Thomas Kuschel 2024-06-14 00:20:26 +02:00
parent 99912ce78c
commit 656ed78288
4 changed files with 47 additions and 89 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode
*.pdf

View File

@ -60,7 +60,7 @@ If it is not already installed, then:
#### Chromium Browser #### Chromium Browser
$ sudo apt install chromium $ sudo apt install chromium-driver
#### Manager (selenium) #### Manager (selenium)
Afterwards, selenium is installed: Afterwards, selenium is installed:

View File

@ -1,115 +1,71 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Thomas Kuschel, following with more ....
# changes to python-seleniumbase 4.25/4.27.5-1 python-selenium 4.21.0-1 (installed)
# changes:
# #001 The Tab URL at phpmyadmin has changed from server_export to server/export
# #002 The the visibility of the site is critically, so we have to put in the line option.add_argument('window-size=1200x600')
import argparse import argparse
#import datetime
import os import os
import time
#import re
import sys import sys
import time
#import grab __version__ = '1.0.0'
# yay extra/python-pip __website__ = 'https://www.fb.gv.at/Funk/amateurfunkdienst.html'
# pip install selenium
# ! pip install webdriver-manager
# yay geckodriver (for Firefox browser)
import selenium
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.common.by import By from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.service import Service as ChromiumService
from selenium.webdriver.chrome.service import Service
__version__ = '2.0.1' def call_description():
print(f'Download and Parse the Austrian Callbook Version {__version__}')
def call_website(url,username,password,server,verbose=0,interactive=False): def call_parser():
# using Chrome to access web parser = argparse.ArgumentParser(
# driver = webdriver.Firefox(executable_path=r'/usr/bin/geckodriver') description='Download and Parse the Austrian Callbook',
epilog=f'''
Written by Thomas Kuschel,
Version {__version__}
'''
)
parser.add_argument('--interactive', '-i', action='store_true', default=False)
# parser.add_argument('--server', '-s', default=__website__, required=False)
parser.add_argument('--version', '-v', action='version', version='{} {}'.format(os.path.split(__file__)[1],__version__))
parser.add_argument('url', metavar='URL', nargs='?', default=__website__)
return parser.parse_args()
def call_website(url,interactive=False):
if(interactive): if(interactive):
print('Interactive') print('Interactive')
driver = webdriver.Chrome() driver=webdriver.Chrome()
else: # do a headless script base work (default) else:
print('Headless Script') print('Headless Script')
service = Service(executable_path='/usr/bin/chromedriver') options = webdriver.ChromeOptions()
option = webdriver.ChromeOptions() options.add_argument('--headless')
option.add_argument('headless') options.add_argument('--no-sandbox')
option.add_argument('window-size=1200x600') options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=option, service=service) driver = webdriver.Chrome(options=options)
driver.get(url) driver.get(url)
assert "phpMyAdmin" in driver.title print(driver.title)
elements = driver.find_elements(By.XPATH,'//a[contains(@href,"Rufzeichen")]')
print('Driver-Title: ' + driver.title) if elements:
elem = driver.find_element(By.ID, "input_username") element = elements[0]
elem.clear() filename = element.click()
elem.send_keys(username) else:
elem = driver.find_element(By.ID, "input_password") print('Sorry, no Link containing "Rufzeichen" found.')
elem.clear() driver.close()
elem.send_keys(password) sys.exit(2)
elem = driver.find_element(By.ID, "select_server")
from selenium.webdriver.support.ui import Select print(element.text)
select = Select(driver.find_element(By.ID, 'select_server')) print()
select.select_by_visible_text(server)
driver.find_element(By.ID, "input_go").click()
time.sleep(1)
#driver.find_element(By.LINK_TEXT, "Export").click()
#driver.find_element_by_xpath('//a[contains(@href,"server_export")]').click()
#print(driver.page_source.encode("utf-8"))
element = driver.find_element(By.XPATH,'//a[contains(@href,"server/export")]')
print("Element is visible? " + str(element.is_displayed()))
element.click()
time.sleep(5) time.sleep(5)
driver.find_element(By.ID, "buttonGo").click()
time.sleep(1)
print(driver.current_window_handle)
#w = driver.window_handles[1]
#driver.switch_to.window(child)
#driver.fullscreen_window()
try:
# WebDriverWait(browser, 3).until(EC.alert_is_present(),
# 'Timed out waiting for PA creation ' +
# 'confirmation popup to appear.')
alert = driver.switch_to.alert
alert.accept()
print("alert accepted")
except:
print("no alert")
time.sleep(20)
driver.close() driver.close()
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser( # call_description()
description='Automates the download of the amateur radio callsign list from Austria', args = call_parser()
epilog='''
Written by Thomas Kuschel,
Version {}
'''.format(__version__))
parser.add_argument('--interactive', '-i', action='store_true', default=False)
parser.add_argument('--verbose', '-v', action='count', default=0)
parser.add_argument('--version', action='version', version='{} {}'.format(os.path.split(__file__)[1],__version__))
# parser.parse_args(['--version'])
parser.add_argument('url', metavar='URL', help='phpMyAdmin login page URL')
parser.add_argument('--username', '-u', default='', help='phpMyAdmin login username', required=False)
parser.add_argument('--password', '-p', default='', help='phpMyAdmin login password', required=False)
parser.add_argument('--server', '-s', default='kreios', required=False)
args = parser.parse_args()
try: try:
print(f'Try calling {args.url}')
filename = call_website(**vars(args)) filename = call_website(**vars(args))
sys.exit(0) sys.exit(0)
except Exception as e: except Exception as e:
print('Error: {}'.format(e), file=sys.stderr) print('Error: {}'.format(e), file=sys.stderr)
sys.exit(1) sys.exit(1)
print('Verbose level: {}'.format(args.verbose))

View File

@ -106,7 +106,7 @@ if __name__ == '__main__':
args = parser.parse_args() args = parser.parse_args()
try: try:
print(f'Try calling {args.url}') print(f'Try calling {args.url}')
filename = call_website(**vars(args)) filename = call_website(**vars(args))
sys.exit(0) sys.exit(0)
except Exception as e: except Exception as e: