-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·121 lines (92 loc) · 3.79 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 27 21:42:32 2019
@author: Alecto
"""
import re
import requests
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import yaml
def config_reader(filename: str):
with open(filename) as file:
documents = yaml.safe_load(file)
return documents
def connect_to_eurecia(eurecia_host: str, login: str, password: str) -> webdriver:
print(f"Connecting to {eurecia_host} using selenium")
driver = webdriver.Firefox()
driver.get("https://" + eurecia_host)
# set the login
element = driver.find_element_by_id("email")
element.clear()
element.send_keys(login)
element.send_keys(Keys.RETURN)
# set the password
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "password")))
element.clear()
element.send_keys(password)
element.send_keys(Keys.RETURN)
# https://stackoverflow.com/questions/26566799/wait-until-page-is-loaded-with-selenium-webdriver-for-python
delay = 10 # seconds
try:
_ = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, "homeRow")))
except TimeoutException:
print("Loading took too much time!")
driver.get(f"https://{eurecia_host}/eurecia/payslip/Open.do")
return driver
def driver_to_requests(driver: webdriver) -> requests.sessions.Session:
cookies = driver.get_cookies()
driver.close()
session = requests.Session()
for cookie in cookies:
session.cookies.set(cookie["name"], cookie["value"])
return session
def download_last_payslip(session: requests.sessions.Session, eurecia_host: str, payslip_name: str):
print("Download last payslip using API")
eurecia_host = config["eurecia_host"]
baseurl = f"https://{eurecia_host}/eurecia/api/v1/payslip"
response = session.get(baseurl)
if response.ok:
payslip_list = response.json()
else:
print(response.content)
raise ValueError(response.status_code)
last_payslip_url = (
f"https://{eurecia_host}/" + payslip_list["2020"][0]["files"][0]["urlContent"]
)
filename = payslip_name + payslip_list["2020"][0]["description"]
filename = filename.replace(" ", "-")
response = session.get(last_payslip_url)
if response.status_code == 200:
with open(f"{filename}.pdf", "wb") as f:
f.write(response.content)
print("OK")
def download_calendar(session: requests.sessions.Session, eurecia_host: str, calendar_name: str):
print("Download calendar using API")
baseurl = f"https://{eurecia_host}/eurecia/planningVacation/planning.do?print=all"
response = session.get(baseurl)
if response.ok:
calendar_raw = response.text
else:
print(response.content)
raise ValueError(response.status_code)
if response.ok:
with open(f"{calendar_name}.html", "w") as f:
f.write(calendar_raw)
print("OK")
title_regex = r"title=\"([\w\s,'\/()-\.]+)\s([0-9\/-]+)\s\(([\w\s,'\/()-\.]+)\)\s([\w\s,'\/()-\.]+)\s\""
calendar_extracted = re.findall(title_regex, calendar_raw, re.MULTILINE)
if __name__ == "__main__":
config = config_reader("secrets.yaml")
driver = connect_to_eurecia(config["eurecia_host"], config["login"], config["password"])
session = driver_to_requests(driver)
download_last_payslip(session, config["eurecia_host"], config["payslip"])
download_calendar(session, config["eurecia_host"], config["calendar"])
eurecia_host = config["eurecia_host"]
calendar_name = config["calendar"]