1+ import requests
2+ from bs4 import BeautifulSoup
3+ import pandas as pd
4+ from datetime import datetime , time
5+ import pytz
6+ import os
7+ import time as time_module
8+
9+ def fetch_ride_wait_times ():
10+ # URL of the webpage with the Disneyland ride wait times
11+ url = 'https://wdwpassport.com/wait-times/magic-kingdom'
12+
13+ # Set the timezone for Orlando
14+ orlando_tz = pytz .timezone ('America/New_York' )
15+
16+ # Get the current time in Orlando
17+ crawl_time = datetime .now (orlando_tz ).strftime ('%Y-%m-%d %H:%M' )
18+
19+ # Send a GET request to the website to fetch the HTML content
20+ response = requests .get (url )
21+
22+ # Check if the request was successful
23+ if response .status_code == 200 :
24+ # Parse the HTML content using BeautifulSoup
25+ soup = BeautifulSoup (response .text , 'html.parser' )
26+
27+ # List to store the ride names and wait times
28+ rides_data = []
29+
30+ # Find all the list items with the ride and wait time information
31+ rides_list = soup .find_all ('li' , class_ = 'relative flex items-center justify-between border-b border-gray-200 px-4 py-2 last:border-b-0' )
32+
33+ # Loop through each ride entry and extract the ride name and wait time
34+ for ride_item in rides_list :
35+ # Extract the ride name (inside <h3> tag)
36+ ride_name = ride_item .find ('h3' , class_ = 'relative z-10 mr-4 text-sm font-medium leading-tight' ).text .strip ()
37+
38+ # Extract the regular wait time (for most rides)
39+ wait_time_div = ride_item .find ('div' , class_ = 'inline-flex h-8 w-8 items-center justify-center rounded-full bg-teal-200 font-medium text-teal-800' )
40+
41+ # Special case for Tiana's Bayou Adventure wait time format
42+ special_wait_time_div = ride_item .find ('div' , class_ = 'ml-1 inline-flex h-8 w-8 items-center justify-center whitespace-nowrap rounded-full bg-teal-200 text-base font-medium text-teal-800' )
43+
44+ # Determine which wait time div to use (prefer the special one if it exists)
45+ if special_wait_time_div :
46+ wait_time = special_wait_time_div .text .strip ()
47+ elif wait_time_div :
48+ wait_time = wait_time_div .text .strip ()
49+ else :
50+ wait_time = 'N/A' # Handle cases where wait time is missing
51+
52+ # Append the ride name and wait time to the list
53+ rides_data .append ([ride_name , wait_time ])
54+
55+ # Create a DataFrame from the scraped data
56+ rides_df = pd .DataFrame (rides_data , columns = ['Ride Name' , crawl_time ])
57+
58+ # Check if the file already exists
59+ file_name = 'wait_times.csv'
60+ if os .path .exists (file_name ):
61+ # Read the existing file
62+ existing_df = pd .read_csv (file_name )
63+
64+ # Merge the new data with the existing data based on 'Ride Name'
65+ merged_df = pd .merge (existing_df , rides_df , on = 'Ride Name' , how = 'left' )
66+
67+ # Save the updated dataframe
68+ merged_df .to_csv (file_name , index = False )
69+ print (f"Updated wait times for { crawl_time } ." )
70+ else :
71+ # Create a new file with the current data
72+ rides_df .to_csv (file_name , index = False )
73+ print (f"Created a new wait times file for { crawl_time } ." )
74+
75+ return rides_df
76+
77+ else :
78+ print (f"Failed to retrieve the webpage. Status code: { response .status_code } " )
79+ return None
80+
81+ if __name__ == '__main__' :
82+ # Define the Orlando timezone
83+ orlando_tz = pytz .timezone ('America/New_York' )
84+
85+ while True :
86+ # Get the current time in Orlando
87+ current_time_orlando = datetime .now (orlando_tz ).time ()
88+
89+ # Define park open and close times
90+ park_open_time = time (7 , 00 ) # Park opens at 07:00 AM
91+ park_close_time = time (0 , 30 ) # Park closes at 23:30 PM (next day)
92+ # fetch_ride_wait_times()
93+
94+ # Check if the current time is within the park's operating hours
95+ if (park_open_time <= current_time_orlando <= time (23 , 59 )) or (time (0 , 0 ) <= current_time_orlando <= park_close_time ):
96+ # if (park_open_time <= current_time_orlando <= park_close_time):
97+ fetch_ride_wait_times () # Fetch and save wait times
98+ else :
99+ print ("Park is closed. Skipping fetch." , current_time_orlando )
100+
101+ # Sleep for 5 minutes (300 seconds)
102+ time_module .sleep (300 )
0 commit comments