Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- from datetime import datetime, timedelta
- import re
- def fetch_f1_races():
- URL = "https://en.wikipedia.org/wiki/2025_Formula_One_World_Championship"
- response = requests.get(URL)
- soup = BeautifulSoup(response.content, "html.parser")
- tables = soup.find_all("table", class_="wikitable")
- races = []
- for table in tables:
- headers = [th.get_text(strip=True) for th in table.find_all("th")]
- if "Race date" in headers and "Grand Prix" in headers:
- date_col_idx = headers.index("Race date")
- gp_col_idx = headers.index("Grand Prix")
- for row in table.find_all("tr")[1:]:
- cols = row.find_all(["td", "th"])
- if len(cols) <= max(date_col_idx, gp_col_idx):
- continue
- gp_name = cols[gp_col_idx].get_text(strip=True)
- date_cell = cols[date_col_idx].get_text(" ", strip=True)
- date_clean = re.sub(r"\[.*?\]", "", date_cell).strip()
- try:
- date_obj = datetime.strptime(f"{date_clean} 2025", "%d %B %Y").date()
- races.append((date_obj, gp_name))
- except ValueError:
- print(f"⚠️ Could not parse date: '{date_clean}'")
- continue
- return sorted(races, key=lambda x: x[0])
- def analyze_busiest_30_day_windows(races):
- max_count = 0
- busiest_windows = []
- for i in range(len(races)):
- window_start = races[i][0]
- window_end = window_start + timedelta(days=30)
- window_races = [r for r in races if window_start <= r[0] <= window_end]
- count = len(window_races)
- if count > max_count:
- max_count = count
- busiest_windows = [(window_start, window_end, window_races)]
- elif count == max_count:
- busiest_windows.append((window_start, window_end, window_races))
- return busiest_windows
- #def count_future_races(races, today=None):
- # if today is None:
- # today = datetime.today().date()
- # return [(d, name) for d, name in races if d >= today]
- def count_races_next_30_days(races, today=None):
- if today is None:
- today = datetime.today().date()
- end_date = today + timedelta(days=30)
- return [(d, name) for d, name in races if today <= d <= end_date]
- def main():
- races = fetch_f1_races()
- if not races:
- print("❌ Δεν βρέθηκαν Grand Prix.")
- return
- # ✅ Υπολογισμός busiest 30ήμερων
- busiest = analyze_busiest_30_day_windows(races)
- print(f"\n📊 Περισσότερα Grand Prix μέσα σε 30 ημέρες: {len(busiest[0][2])} αγώνες")
- for start, end, gp_list in busiest:
- print(f"\n🗓️ Από {start.strftime('%d %b')} έως {end.strftime('%d %b')}:\n")
- for date, name in gp_list:
- print(f" 📍 {date.strftime('%d %b')}: {name}")
- # ✅ Πόσα Grand Prix απομένουν από σήμερα
- today = datetime.today().date()
- # upcoming = count_future_races(races, today)
- monthly_gp = count_races_next_30_days(races, today)
- print(f"\n📅 Σήμερα είναι {today.strftime('%d %b %Y')}")
- print(f"📺 Αν κάνεις *μηνιαία* συνδρομή στο F1 TV σήμερα, θα δεις {len(monthly_gp)} Grand Prix τις επόμενες 30 ημέρες:")
- for date, name in monthly_gp:
- print(f" ▶️ {date.strftime('%d %b')}: {name}")
- # print(f"\n📅 Σήμερα είναι {today.strftime('%d %b %Y')}")
- # print(f"📺 Αν κάνεις συνδρομή στο F1 TV σήμερα, θα δεις {len(upcoming)} Grand Prix:")
- # for date, name in upcoming:
- # print(f" ▶️ {date.strftime('%d %b')}: {name}")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement