From a624475349bf71ada1f2410eb8cd05fb0f3bface Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Thu, 11 May 2023 19:47:49 +0200 Subject: [PATCH] Add files via upload --- example_show_accessable_pages.py | 42 +++++++++++++++++++ example_surf_to_pages.py | 47 +++++++++++++++++++++ get_tree/get_layout_url.py | 18 ++++++++ get_tree/get_tree.py | 41 ++++++++++++++++++ login/close_login_popup.json | 3 ++ login/close_login_popup.py | 26 ++++++++++++ login/login.json | 7 ++++ login/login.py | 72 ++++++++++++++++++++++++++++++++ username.json | 3 ++ 9 files changed, 259 insertions(+) create mode 100644 example_show_accessable_pages.py create mode 100644 example_surf_to_pages.py create mode 100644 get_tree/get_layout_url.py create mode 100644 get_tree/get_tree.py create mode 100644 login/close_login_popup.json create mode 100644 login/close_login_popup.py create mode 100644 login/login.json create mode 100644 login/login.py create mode 100644 username.json diff --git a/example_show_accessable_pages.py b/example_show_accessable_pages.py new file mode 100644 index 0000000..9f2f626 --- /dev/null +++ b/example_show_accessable_pages.py @@ -0,0 +1,42 @@ +import json +from login.login import login +from login.close_login_popup import close_login_popup + +from get_tree.get_tree import get_tree + +from getpass import getpass + +from selenium import webdriver +from selenium.webdriver.firefox.service import Service as FirefoxService +from webdriver_manager.firefox import GeckoDriverManager + +list_of_page_ids: list[int] = [59451, 59246] + +base_url: str = "https://www.uni-bremen.de" + +zfn_password: str = getpass() + +with open("username.json", "r") as file: + json_dict = json.load(file) +zfn_user: str = json_dict["zfn_user"] + +driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install())) + +# Login +if login(driver, base_url, zfn_user, zfn_password) is False: + print("Login failed") + exit(1) + +# Get rid of the popup +close_login_popup(driver) + +# Get the protected URL to the pages +page_list = get_tree(driver, base_url) + +for page_entry in page_list: + print(page_entry) + print("---") + +print("Close shop") +driver.close() +driver.quit() diff --git a/example_surf_to_pages.py b/example_surf_to_pages.py new file mode 100644 index 0000000..0bfa5e1 --- /dev/null +++ b/example_surf_to_pages.py @@ -0,0 +1,47 @@ +import json +from login.login import login +from login.close_login_popup import close_login_popup + +from get_tree.get_layout_url import get_layout_url + +from getpass import getpass + +from selenium import webdriver +from selenium.webdriver.firefox.service import Service as FirefoxService +from webdriver_manager.firefox import GeckoDriverManager + +import time + +list_of_page_ids: list[int] = [59451, 59246] + +base_url: str = "https://www.uni-bremen.de" + +zfn_password: str = getpass() + +with open("username.json", "r") as file: + json_dict = json.load(file) +zfn_user: str = json_dict["zfn_user"] + +driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install())) + +# Login +if login(driver, base_url, zfn_user, zfn_password) is False: + print("Login failed") + exit(1) + +# Get rid of the popup +close_login_popup(driver) + +# Get the protected URL to the pages +page_url_base = get_layout_url(driver, base_url) + +for page_id in list_of_page_ids: + print(f"Go to page {int(page_id)}") + page_url: str = page_url_base + f"&id={int(page_id)}" + driver.get(page_url) + print("Wait for 5 sec") + time.sleep(5) + +print("Close shop") +driver.close() +driver.quit() diff --git a/get_tree/get_layout_url.py b/get_tree/get_layout_url.py new file mode 100644 index 0000000..a2fc675 --- /dev/null +++ b/get_tree/get_layout_url.py @@ -0,0 +1,18 @@ +from selenium import webdriver + + +def get_layout_url( + driver: webdriver.firefox.webdriver.WebDriver, + base_url: str, +) -> str: + data_elements = str(driver.page_source).split('"') + + lost_and_found = [] + for element in data_elements: + if str(element).find("index.php?route=%2Fweb%2Flayout%2F&token=") != -1: + lost_and_found.append(element) + + assert len(lost_and_found) > 0 + page_url = base_url + str(lost_and_found[0]).replace("\\", "") + + return page_url diff --git a/get_tree/get_tree.py b/get_tree/get_tree.py new file mode 100644 index 0000000..afc2175 --- /dev/null +++ b/get_tree/get_tree.py @@ -0,0 +1,41 @@ +import json +from selenium import webdriver + + +def get_tree( + driver: webdriver.firefox.webdriver.WebDriver, + base_url: str, +) -> list: + data_elements = str(driver.page_source).split('"') + + lost_and_found = [] + for element in data_elements: + if ( + str(element).find( + "index.php?route=%2Fajax%2Fpage%2Ftree%2FfetchData&token=" + ) + != -1 + ): + lost_and_found.append(element) + + assert len(lost_and_found) == 1 + ajax_url = base_url + str(lost_and_found[0]).replace("\\", "") + + backup_url = driver.current_url + driver.get(ajax_url) + + data = str(driver.page_source) + data_lines = data.split('