uulm-coronang-autojoin/main.py.old

107 lines
4.4 KiB
Python

import time
import requests
from bs4 import BeautifulSoup
import re
# URL to scrape
url = "https://campusonline.uni-ulm.de/CoronaNG/user/userDetails.html?cvid=18456614"
# Cookie to include in the request
cookie = "JSESSIONID=5729EE2B769FE54D4663C1B7620E4B43"
# Headers for the GET request
headers = {
"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-GB,en;q=0.5",
"Accept-Encoding": "gzip, deflate, br",
"Referer": "https://campusonline.uni-ulm.de/CoronaNG/user/userDetails.html?cvid=18456614",
"Connection": "keep-alive",
"Cookie": cookie,
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-User": "?1",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
}
def make_post_request(url, headers, payload, max_retries=3):
retries = 0
while retries < max_retries:
try:
response = requests.post(url, headers=headers, data=payload)
response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
return response
except requests.exceptions.RequestException as e:
print(f"Error occurred during POST request: {str(e)}")
retries += 1
if retries < max_retries:
print(f"Retrying in 5 seconds... (Attempt {retries+1}/{max_retries})")
time.sleep(5)
else:
print("Max retries reached. Exiting.")
exit(1)
while True:
try:
print("Scraping...")
# Send GET request to the URL with the specified headers
response = requests.get(url, headers=headers)
response.raise_for_status() # Raise an exception for 4xx or 5xx status codes
# Parse the HTML content using BeautifulSoup
soup = BeautifulSoup(response.content, "html.parser")
# Find the <tr> element with class "dbu"
tr_elements = soup.find_all("tr", class_="dbu")
for tr_element in tr_elements:
th_element = tr_element.find("th")
if th_element and "Max. Teilnehmer" in th_element.text:
# Extract the max and current participant numbers
td_element = tr_element.find("td")
participant_info = td_element.text.strip()
# regex to find the numbers in a string like "10 (aktuell 10)"
regex = r"(\d+) \(aktuell (\d+)\)"
match = re.search(regex, participant_info)
if match:
max_participants = int(match.group(1))
current_participants = int(match.group(2))
print("Max participants:", max_participants, "; Current participants:", current_participants)
else:
print("Failed to parse participant info:", participant_info)
continue
# Check if there is a free spot
if current_participants < max_participants:
# Send POST request to participate
post_url = "https://campusonline.uni-ulm.de/CoronaNG/user/userDetails.html"
payload = "id=18456649&command=participate"
post_headers = headers.copy()
post_headers["Content-Type"] = "application/x-www-form-urlencoded"
post_headers["Content-Length"] = str(len(payload))
post_headers["Origin"] = "https://campusonline.uni-ulm.de"
post_response = make_post_request(post_url, post_headers, payload)
if post_response.status_code == 200:
print("Successfully participated!")
exit(0)
else:
print("Failed to participate. Status code:", post_response.status_code)
exit(1)
else:
print("No free spots available.")
break
else:
print("Participant information not found.")
except requests.exceptions.RequestException as e:
print(f"Error occurred during GET request: {str(e)}")
print(f'Current Time: {time.strftime("%H:%M:%S")}. Sleeping for 30 seconds...')
time.sleep(30)