Add files via upload

This commit is contained in:
David Rotermund 2023-05-11 19:47:49 +02:00 committed by GitHub
parent 490af24a18
commit a624475349
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 259 additions and 0 deletions

View file

@ -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()

47
example_surf_to_pages.py Normal file
View file

@ -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()

View file

@ -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

41
get_tree/get_tree.py Normal file
View file

@ -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('<div id="json">')
assert len(data_lines) == 2
data = data_lines[1]
data_lines = data.split("</div>")
assert len(data_lines) > 0
data = data_lines[0]
page_dict = json.loads(data)
driver.get(backup_url)
return page_dict

View file

@ -0,0 +1,3 @@
{
"button_class": "t3js-modal-close close"
}

View file

@ -0,0 +1,26 @@
import json
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
def close_login_popup(driver: webdriver.firefox.webdriver.WebDriver):
string_json_path: str = os.path.join("login", "close_login_popup.json")
time.sleep(1)
with open(string_json_path, "r") as file:
string_dict = json.load(file)
evil_element_found: bool = True
while evil_element_found is True:
found_element_list = driver.find_elements(By.TAG_NAME, "button")
evil_element_found = False
for found_element in found_element_list:
if found_element.get_dom_attribute("class") == string_dict["button_class"]:
evil_element_found = True
found_element.click()
time.sleep(1)
break

7
login/login.json Normal file
View file

@ -0,0 +1,7 @@
{
"login_url": "/typo3/index.php?route=%2Flogin",
"user_name_id": "t3-username",
"user_password_id": "t3-password",
"user_button_id": "t3-login-submit",
"bad_url": "/typo3/index.php?loginProvider="
}

72
login/login.py Normal file
View file

@ -0,0 +1,72 @@
import json
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
def login(
driver: webdriver.firefox.webdriver.WebDriver,
base_url: str,
zfn_user: str,
zfn_password: str,
) -> bool:
assert len(base_url) > 0
assert len(zfn_user) > 0
assert len(zfn_password) > 0
string_json_path: str = os.path.join("login", "login.json")
with open(string_json_path, "r") as file:
string_dict = json.load(file)
login_url: str = base_url + string_dict["login_url"]
# Login
driver.get(login_url)
print_new_line: bool = False
while driver.current_url != login_url:
print(".", end="")
time.sleep(1)
print_new_line = True
if print_new_line is True:
print()
login_name_list = driver.find_elements(By.ID, string_dict["user_name_id"])
login_password_list = driver.find_elements(By.ID, string_dict["user_password_id"])
login_button_list = driver.find_elements(By.ID, string_dict["user_button_id"])
if (
(len(login_name_list) != 1)
and (len(login_password_list) != 1)
and (len(login_button_list) != 1)
):
return False
login_name = login_name_list[0]
login_password = login_password_list[0]
login_button = login_button_list[0]
while (login_name.get_attribute("value") != zfn_user) or (
login_password.get_attribute("value") != zfn_password
):
login_name.clear()
login_password.clear()
login_name.send_keys(zfn_user)
login_password.send_keys(zfn_password)
login_button.click()
while driver.current_url == login_url:
print(".", end="")
time.sleep(1)
print_new_line = True
if print_new_line is True:
print()
if str(driver.current_url).startswith(base_url + string_dict["bad_url"]) is True:
return False
return True

3
username.json Normal file
View file

@ -0,0 +1,3 @@
{
"zfn_user": "myusername"
}