forked from kdngy/booking_automation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
119 lines (91 loc) · 3.84 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
'''
Booking automation
'''
from dataclasses import dataclass
from enum import Enum, auto
from abc import ABC, abstractmethod
from datetime import datetime, timedelta
import time
"""Imports for getting links from a webpage"""
from splinter import Browser
class Role(Enum):
"""Explicit definition of person roles for booking"""
STUDIERENDER = auto()
BESCHAEFTIGTER = auto()
EXTERNER = auto()
ALUMNI = auto()
@dataclass
class Person():
"""Basic representation of a person who can book a course"""
gender: str
first_name: str
last_name: str
street_address: str
city: str
postal_code: int
role: Role
mail: str
phone_number: str
iban: str
immatriculation_number: int = 0
@dataclass
class Course(ABC):
"""Basic representation of a course to book"""
name: str
@abstractmethod
def book(self, Person) -> None:
"""Let a person book the course"""
@dataclass
class HuepfenThomas(Course):
"""Fitness mit Musik Kurs von Thomas, sonntag um 19:45"""
name: str = "Fitness mit Musik Kurs von Thomas, sonntag um 19:45"
web_page = "https://buchung.hsz.rwth-aachen.de/angebote/Wintersemeseter_2021_22/_Fitness_mit_Musik.html"
# TODO: automate datetime for next course
course_time = datetime(2021, 10, 14, 19, 45, 00)
booking_time = course_time + timedelta(days=-1)
def book(self, Person) -> None:
pass
def main() -> None:
"""Main function"""
huepfen_thomas = HuepfenThomas()
person = Person("m", "Max", "Mustermann", "Königsallee 1", "Düsseldorf", "40215", Role.EXTERNER, "[email protected]", "xxxxxxxxxxxx", "xxxxxxxxxxxxxxxxxxxxxxx")
huepfen_thomas.book(person)
browser = Browser() # defaults to firefox
browser.visit('https://buchung.hsz.rwth-aachen.de/angebote/Wintersemeseter_2021_22/_Fitness_mit_Musik.html')
# Get all the inputs elements on the page
inputs = browser.driver.find_elements_by_tag_name('input')
print('Verfügbare Sessions:')
for input in inputs:
# if the input element is a "buchen" button
if 'bs_btn_buchen' in input.get_attribute('class').split():
# get its second degree parent (grand-parent I guess)
parent = input.find_element_by_xpath('../..')
all_children_by_css = parent.find_elements_by_xpath(".//*")
for child in all_children_by_css:
# find the element that contain the Leiter's name
if 'bs_skl' in child.get_attribute('class').split():
leiter = child.find_element_by_xpath(".//*").get_attribute('innerHTML')
print('* '+leiter)
# da muss man sich irgendein Mechanism überlegen für die Session Selection
# Bei mir gerade war eine Session von Janina zu verfügung, ich habe's dann
# hardcoded damit ich weitergehen kann.
if leiter == 'Janina Frey':
input.click()
# We close the first window
browser.windows[0].close()
# Get all the inputs on that second page
inputs = browser.driver.find_elements_by_tag_name('input')
print('Verfügbare Slots')
for input in inputs:
# For every input that has the "buchen" class (meaning it can be booked)
if 'buchen' in input.get_attribute('class').split():
parent = input.find_element_by_xpath('../..')
all_children_by_css = parent.find_elements_by_css_selector("*")
day = all_children_by_css[1].get_attribute('innerHTML')
date = all_children_by_css[2].get_attribute('innerHTML')
time = all_children_by_css[3].get_attribute('innerHTML')
print(f'* {day} {date} {time}')
# Then you gotta input.click() on the slot you wanna book...etc.
# browser.quit()
if __name__ == "__main__":
main()