From 64a1c97e7482ae617c9c030bf07d2688524294ad Mon Sep 17 00:00:00 2001 From: Saku Laesvuori Date: Sat, 2 Sep 2023 23:13:17 +0300 Subject: [PATCH] =?UTF-8?q?Ensimm=C3=A4inen=20versio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 ++++++++++++ poiminta.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 README.md create mode 100755 poiminta.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..5a9013c --- /dev/null +++ b/README.md @@ -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. diff --git a/poiminta.py b/poiminta.py new file mode 100755 index 0000000..a82f95b --- /dev/null +++ b/poiminta.py @@ -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()