Skip to content

Instantly share code, notes, and snippets.

@hejmsdz
Last active January 26, 2022 18:40
Show Gist options
  • Save hejmsdz/bb4acc23df5a2613bf7abc7e7753f48b to your computer and use it in GitHub Desktop.
Save hejmsdz/bb4acc23df5a2613bf7abc7e7753f48b to your computer and use it in GitHub Desktop.

Sposób użycia

# pobierz skrypt
wget https://gist.githubusercontent.com/hejmsdz/bb4acc23df5a2613bf7abc7e7753f48b/raw/hrnest.py

# opcjonalnie ustaw wirtualne środowisko
python3 -m venv .venv && . .venv/bin/activate

# zainstaluj Selenium
pip install selenium

# wpisz login i hasło do HRnesta do zmiennych środowiskowych
read HRNEST_USERNAME
read -s HRNEST_PASSWORD
export HRNEST_USERNAME HRNEST_PASSWORD

# zedytuj parametry year, month i excluded_days w ostatniej linii skryptu
$EDITOR hrnest.py

# i uruchom go ;)
python3 hrnest.py
from datetime import timedelta
from calendar import Calendar
import os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
def login(driver, username, password):
driver.get('https://hrnest.io/Account/LogOn')
driver.find_element(By.ID, 'UserName').send_keys(username)
driver.find_element(By.ID, 'Password').send_keys(password)
driver.find_element(
By.CSS_SELECTOR, '#login-panel button[type=submit]').click()
def submit_request(driver, start_date, end_date):
driver.get('https://hrnest.io/create-request')
def wait_for_loader():
loader = driver.find_element(By.ID, 'updating')
WebDriverWait(driver, 5).until_not(EC.visibility_of(loader))
def fill_input(input_id, value):
field = driver.find_element(By.ID, input_id)
field.clear()
field.send_keys(value, Keys.TAB)
wait_for_loader()
wait_for_loader()
driver.find_element(
By.CSS_SELECTOR, '[data-id=u_Requests_HolidayType]').click()
wait_for_loader()
driver.find_element(By.LINK_TEXT, 'Praca z domu').click()
wait_for_loader()
fill_input('u_Requests_StartDate', start_date.strftime('%d.%m.%Y'))
fill_input('u_Requests_EndDate', end_date.strftime('%d.%m.%Y'))
driver.find_element(By.CSS_SELECTOR, 'input[type=submit]').click()
def get_date_ranges(year, month, excluded_days):
weeks = Calendar().monthdatescalendar(year, month)
days = []
for week in weeks:
working_week = [day for day in week[0:5] if day.month ==
month and day.day not in excluded_days]
days.extend(working_week)
ranges = []
last_day = None
current_range_start = None
current_range_end = None
for day in days:
if not last_day or (day - last_day) > timedelta(days=1):
if current_range_start and current_range_end:
ranges.append((current_range_start, current_range_end))
current_range_start = day
current_range_end = day
else:
current_range_end = day
last_day = day
ranges.append((current_range_start, current_range_end))
return ranges
def main(**kwargs):
driver = webdriver.Chrome()
driver.implicitly_wait(2)
login(driver, os.environ['HRNEST_USERNAME'], os.environ['HRNEST_PASSWORD'])
for start_date, end_date in get_date_ranges(**kwargs):
print(start_date.isoformat(), end_date.isoformat())
submit_request(driver, start_date, end_date)
if __name__ == '__main__':
main(year=2022, month=1, excluded_days=[6, 7, 11, 13, 20])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment