-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (114 loc) · 4.36 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import asyncio
import hashlib
import logging
import os
import sys
import requests
import yaml
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from telegram import Bot
# Configure logging
logging.basicConfig(filename='./app.log', level=logging.INFO,
format='%(asctime)s %(levelname)s:%(message)s')
# Load Telegram credentials from .env file
load_dotenv()
TELEGRAM_TOKEN = os.getenv('TELEGRAM_TOKEN')
TELEGRAM_CHANNEL = os.getenv('TELEGRAM_CHANNEL') # Telegram channel ID
# Website to monitor
COURSE_URL = 'https://hacker-school.de/unterstuetzen/inspirer/checkin-inspirer-yourschool/?formats%5B%5D=ys'
bot = Bot(token=TELEGRAM_TOKEN)
async def post_new_courses(new_courses):
""" Post new courses to the Telegram channel. """
for course in new_courses:
message = f"New course available:\n\n{course['title']}\n{course['date']}\n\nCheck it out at {COURSE_URL}"
try:
await bot.send_message(chat_id=TELEGRAM_CHANNEL, text=message)
escaped_message = message.replace("\n", "\\n")
logging.info("Message sent: %s", escaped_message)
except Exception as e:
logging.error("Error: %s", e)
sys.exit(1)
def load_existing_courses():
""" Load existing courses from a YAML file. """
if os.path.exists('courses.yml'):
with open('courses.yml', 'r', encoding='utf-8') as file:
return yaml.safe_load(file)
return []
def save_courses(courses):
""" Save current courses to a YAML file. """
with open('courses.yml', 'w', encoding='utf-8') as file:
yaml.dump(courses, file)
def get_courses():
""" Find out number of pages and get all courses. """
try:
response = requests.get(COURSE_URL, timeout=10)
except Exception as e:
logging.error("Error fetching courses: %s", e)
sys.exit(1)
soup = BeautifulSoup(response.text, 'html.parser')
pagination_container = soup.find('ul', class_='pagination')
if pagination_container:
pagination_items = pagination_container.find_all('li')
else:
logging.error("Pagination container not found")
return []
# get first page with previous response to avoid another request
courses = get_course_page(1, response)
# last page number is number of items - 2 to remove the next and previous buttons
last_page = len(pagination_items) - 2
if last_page < 2:
return courses
for page in range(2, last_page+1):
courses += get_course_page(page)
return courses
def get_course_page(page_number, page=None):
""" Get a specific page of courses. """
if page is None:
try:
page = requests.get(
f"{COURSE_URL}&event_page={page_number}", timeout=10)
except Exception as e:
logging.error("Error fetching courses: %s", e)
sys.exit(1)
soup = BeautifulSoup(page.text, 'html.parser')
course_elements = soup.find_all('div', class_='hs-event')
courses = []
for course in course_elements:
courses.append(parse_course(course))
return courses
def parse_course(course):
""" Parse a course element and return the details. """
title = course.find('h3', class_='hs-event-titel').text.strip()
date = course.find(
'div', class_='hs-dates').span.find_next_sibling().text.strip()
# not a bug the element is spelled wrong
description = course.find(
'span', class_='hs-curse-discription').text.strip()
unique_string = title + date + description
course_id = hashlib.md5(unique_string.encode()).hexdigest()
course_info = {
'id': course_id,
'title': title,
'date': date,
'description': description
}
return course_info
def main():
""" Main function to run the course update check and notification. """
logging.info("Running!")
existing_courses = load_existing_courses()
current_courses = get_courses()
loop = asyncio.get_event_loop()
new_courses = []
for e in current_courses:
if not any(e["id"] == f["id"] for f in existing_courses):
new_courses.append(e)
if new_courses:
loop.run_until_complete(post_new_courses(new_courses))
save_courses(existing_courses + new_courses)
logging.info("New courses found and posted.")
else:
logging.info("No new courses found.")
if __name__ == '__main__':
main()