Created
July 2, 2023 19:48
-
-
Save S0PEX/b62f76b1c6b59e1bcf408b2b7090506c to your computer and use it in GitHub Desktop.
Bot.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
import requests | |
import argparse | |
import schedule | |
def send_register_request(course_id: int): | |
data = { | |
'state': 'studentAnmelden', | |
'type': 'student', | |
# Entsprechend die ID eintragen, weiß nicht für was die offerId=36 war | |
'offerCourseID': str(course_id), | |
'vorname': 'Name', | |
'nachname': 'Nachname', | |
'telefon': '0176000000', | |
'matrikel': 'XXXXXXX', | |
'email': '[email protected]', | |
'hochschulen': '1', | |
'hochschulenextern': 'null', | |
'office': 'null', | |
} | |
response = requests.post( | |
'https://anmeldung.hochschulsport-koeln.de/inc/methods.php', data=data) | |
if response.status_code != 200: | |
raise f'Failed to register with reason: {response.text}!' | |
return response | |
def get_course_information(course_id: int): | |
course_response = requests.get( | |
f'https://api.hochschulsport-koeln.de/json/v2/course/{course_id}') | |
if course_response.status_code != 200: | |
raise f'Unable to query course: {course_id}.\nError: {course_response.text}' | |
course_dates_response = requests.get( | |
f'https://api.hochschulsport-koeln.de/json/v2/course/{course_id}/dates') | |
if course_dates_response.status_code != 200: | |
raise f'Unable to query course dates: {course_id}.\nError: {course_dates_response.text}' | |
return course_response.json()[0], course_dates_response.json() | |
def register_for_course(course_id: int): | |
response = send_register_request(course_id) | |
if "Ihre Anmeldung ist erfolgt." not in response.text and "Doppelanmeldungen" not in response.text: | |
raise f"HTTP Request returned Status 200 but response does'nt contain 'erfolgreich'!" | |
return response | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
prog='UzKBot', | |
description='Bot for automatic registration for university sports courses.', | |
epilog='Text at the bottom of help') | |
parser.add_argument('courseId', type=int, | |
help='Course ID contained in the \'Anmelden Url\', e.g., for https://anmeldung.hochschulsport-koeln.de/anmeldung.php?course=163&offer=93 the ID would be 163!') | |
args = parser.parse_args() | |
course_id = args.courseId | |
try: | |
logging.info(f'Fetching course information for course {course_id}') | |
course, dates = get_course_information(course_id) | |
logging.info( | |
f"You will be automatically registered for {course['title']}.") | |
response = register_for_course(course_id) | |
except Exception as e: | |
logging.error(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment