Advertisement
dalagiorgos

F1_calendar_and_subscription

Jul 8th, 2025
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.87 KB | Sports | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from datetime import datetime, timedelta
  4. import re
  5.  
  6. def fetch_f1_races():
  7.     URL = "https://en.wikipedia.org/wiki/2025_Formula_One_World_Championship"
  8.     response = requests.get(URL)
  9.     soup = BeautifulSoup(response.content, "html.parser")
  10.  
  11.     tables = soup.find_all("table", class_="wikitable")
  12.     races = []
  13.  
  14.     for table in tables:
  15.         headers = [th.get_text(strip=True) for th in table.find_all("th")]
  16.         if "Race date" in headers and "Grand Prix" in headers:
  17.             date_col_idx = headers.index("Race date")
  18.             gp_col_idx = headers.index("Grand Prix")
  19.  
  20.             for row in table.find_all("tr")[1:]:
  21.                 cols = row.find_all(["td", "th"])
  22.                 if len(cols) <= max(date_col_idx, gp_col_idx):
  23.                     continue
  24.  
  25.                 gp_name = cols[gp_col_idx].get_text(strip=True)
  26.                 date_cell = cols[date_col_idx].get_text(" ", strip=True)
  27.                 date_clean = re.sub(r"\[.*?\]", "", date_cell).strip()
  28.  
  29.                 try:
  30.                     date_obj = datetime.strptime(f"{date_clean} 2025", "%d %B %Y").date()
  31.                     races.append((date_obj, gp_name))
  32.                 except ValueError:
  33.                     print(f"⚠️ Could not parse date: '{date_clean}'")
  34.                     continue
  35.  
  36.     return sorted(races, key=lambda x: x[0])
  37.  
  38. def analyze_busiest_30_day_windows(races):
  39.     max_count = 0
  40.     busiest_windows = []
  41.  
  42.     for i in range(len(races)):
  43.         window_start = races[i][0]
  44.         window_end = window_start + timedelta(days=30)
  45.         window_races = [r for r in races if window_start <= r[0] <= window_end]
  46.         count = len(window_races)
  47.  
  48.         if count > max_count:
  49.             max_count = count
  50.             busiest_windows = [(window_start, window_end, window_races)]
  51.         elif count == max_count:
  52.             busiest_windows.append((window_start, window_end, window_races))
  53.  
  54.     return busiest_windows
  55.  
  56. #def count_future_races(races, today=None):
  57. #    if today is None:
  58. #        today = datetime.today().date()
  59. #    return [(d, name) for d, name in races if d >= today]
  60.  
  61. def count_races_next_30_days(races, today=None):
  62.     if today is None:
  63.         today = datetime.today().date()
  64.     end_date = today + timedelta(days=30)
  65.     return [(d, name) for d, name in races if today <= d <= end_date]
  66.  
  67.  
  68. def main():
  69.     races = fetch_f1_races()
  70.     if not races:
  71.         print("❌ Δεν βρέθηκαν Grand Prix.")
  72.         return
  73.  
  74.     # ✅ Υπολογισμός busiest 30ήμερων
  75.     busiest = analyze_busiest_30_day_windows(races)
  76.     print(f"\n📊 Περισσότερα Grand Prix μέσα σε 30 ημέρες: {len(busiest[0][2])} αγώνες")
  77.  
  78.     for start, end, gp_list in busiest:
  79.         print(f"\n🗓️ Από {start.strftime('%d %b')} έως {end.strftime('%d %b')}:\n")
  80.         for date, name in gp_list:
  81.             print(f"  📍 {date.strftime('%d %b')}: {name}")
  82.  
  83.     # ✅ Πόσα Grand Prix απομένουν από σήμερα
  84.     today = datetime.today().date()
  85.     # upcoming = count_future_races(races, today)
  86.  
  87.     monthly_gp = count_races_next_30_days(races, today)
  88.  
  89.     print(f"\n📅 Σήμερα είναι {today.strftime('%d %b %Y')}")
  90.     print(f"📺 Αν κάνεις *μηνιαία* συνδρομή στο F1 TV σήμερα, θα δεις {len(monthly_gp)} Grand Prix τις επόμενες 30 ημέρες:")
  91.     for date, name in monthly_gp:
  92.         print(f"  ▶️ {date.strftime('%d %b')}: {name}")
  93.  
  94. #    print(f"\n📅 Σήμερα είναι {today.strftime('%d %b %Y')}")
  95. #    print(f"📺 Αν κάνεις συνδρομή στο F1 TV σήμερα, θα δεις {len(upcoming)} Grand Prix:")
  96. #    for date, name in upcoming:
  97. #        print(f"  ▶️ {date.strftime('%d %b')}: {name}")
  98.  
  99. if __name__ == "__main__":
  100.     main()
  101.  
Tags: #f1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement