Ensimmäinen versio

This commit is contained in:
Saku Laesvuori 2023-09-02 23:13:17 +03:00
commit 64a1c97e74
2 changed files with 74 additions and 0 deletions

13
README.md Normal file
View File

@ -0,0 +1,13 @@
# poiminta.py
Tämä skripti hakee kuksasta sähköpostit joko perhetiedotteeseen tai
johtajatiedotteeseen. Se on historiallisesti toiminut luotettavammin kuin kuksa
itse, niin oudolta kuin se kuulostaakin, mutta koska se perustuu kuksan
käyttöliittymän tunnisteisiin, se saattaa hajota milloin tahansa.
Käyttääksesi skriptiä tarvitset pythonin, seleniumin pythonille, firefoxin ja
geckodriverin. Skripti poimii sähköpostit perhetiedotteeseen, jos sille ei anna
mitään argumentteja, ja johtajatiedotteeseen, jos sille antaa argumentiksi `jt`
tai `johtajatiedote`. Kun ajat skriptin, se avaa ensin kuksan kirjautumissivun,
johon joudut kirjautumaan käsin (ks. skriptin koodi jos haluat automatisoida
tämän). Tämän jälkeen se poimii sähköpostit ja tulostaa ne.

61
poiminta.py Executable file
View File

@ -0,0 +1,61 @@
#!/usr/bin/env python3
import time
import os
import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
if "help" in sys.argv or "h" in sys.argv or "-h" in sys.argv or "--help" in sys.argv:
print(f"""{sys.argv[0]} [jt|johtajatiedote]
Hakee kuksasta sähköpostit joko perhe- tai johtajatiedotteeseen. Oletuksena perhetiedote.""")
sys.exit(0)
def find_element(by, something):
"""
Try driver.find_element until the element is found.
Make sure the element eventually appears or this can stall for ever.
"""
try:
return driver.find_element(by, something)
except NoSuchElementException:
time.sleep(1)
return find_element(by, something)
driver = webdriver.Firefox()
emails = []
driver.get("https://kuksa.partio.fi")
# This is my (slaesvuo) way to implement unattended login and probably will not
# work for you as is
#passwd = os.popen("pass show web/id.partio.fi | head -n 1").read()
#username_field = find_element(By.ID, 'username')
#username_field.send_keys("saku-laesvuori" + Keys.TAB + passwd + Keys.ENTER)
find_element(By.ID, 'sivu_poiminta').click()
if "johtajatiedote" in sys.argv or "jt" in sys.argv:
find_element(By.ID, 'cphContent_IYleiset').click()
birthdate_upper_bound_input = find_element(By.ID, 'cphContent_TAHSyntymaaika2_txtPvm')
birthdate_upper_bound_input.send_keys("31.12.2008" + Keys.ENTER)
else:
find_element(By.ID, 'cphContent_iJasenyys').click()
find_element(By.ID, 'cphContent_chkMyosJasenenHuoltajat').click()
find_element(By.ID, 'cphContent_btnPoimi').click()
while True:
find_element(By.CSS_SELECTOR, 'tbody') # Wait for the table to be loaded
emails += [x.text for x in driver.find_elements(By.CSS_SELECTOR, 'tr > td:nth-child(5)')]
nextLink = find_element(By.XPATH, "//span[@id='cphContent_dpPoiminta']/a[text()='>']")
if nextLink.get_attribute("class") == "aspNetDisabled":
break
nextLink.click()
for email in {x.lower() for x in emails}:
if email != "":
print(email)
driver.close()