Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Create a Website Alarm Using Python
In this section we will see how to create a website alarm system using Python. This tool automatically opens a specified website URL in your browser at a predetermined time.
Problem Statement
Create a program that opens a website URL on the browser by taking a website URL and time as input. When the system time reaches the specified time, the webpage will be opened automatically.
We can store different web pages in our bookmark sections. Sometimes we need to open some web pages every day at a specific time to do some work. For that purpose, we can set this type of website alarm to automate the task.
In this case we are using standard library modules like sys, webbrowser and time.
Steps to Open Web Pages at Specific Time
- Take the URL which will be opened.
- Take the time to open the webpage at that specified time.
- Check whether the current time matches with the specified time or not.
- If the time matches, open the webpage. Otherwise wait for one second.
- Repeat step 3 every second until the time matches.
- End the process
Implementation
Here's the complete implementation of the website alarm system −
import time
import webbrowser
import sys
def web_alarm(url, alarm_time):
current_time = time.strftime('%I:%M:%S')
while(current_time != alarm_time): # repeatedly check for current time and the alarm time
print('Current time is: ' + current_time)
current_time = time.strftime('%I:%M:%S')
time.sleep(1) # wait for one second
if current_time == alarm_time: # when the time matches, open the browser
print('Opening the ' + url + ' now...')
webbrowser.open(url)
# Example usage
web_alarm('https://www.tutorialspoint.com/', '02:01:00') # Set the alarm using url and time
Command Line Usage
You can also run this script from the command line by passing arguments −
web_alarm(sys.argv[1], sys.argv[2]) # Set the alarm using url and time from command line
Run the script using −
python3 website_alarm.py https://www.tutorialspoint.com/ 02:01:00
Output
Current time is: 02:00:46 Current time is: 02:00:47 Current time is: 02:00:48 Current time is: 02:00:49 Current time is: 02:00:50 Current time is: 02:00:51 Current time is: 02:00:52 Current time is: 02:00:53 Current time is: 02:00:54 Current time is: 02:00:55 Current time is: 02:00:56 Current time is: 02:00:57 Current time is: 02:00:58 Current time is: 02:00:59 Opening the https://www.tutorialspoint.com/ now...
How It Works
The program uses time.strftime('%I:%M:%S') to get the current time in 12-hour format with seconds. It continuously compares the current time with the alarm time in a while loop. When the times match, webbrowser.open() launches the default browser and opens the specified URL.
Enhanced Version with Better User Experience
Here's an improved version with input validation and better formatting −
import time
import webbrowser
from datetime import datetime
def enhanced_web_alarm():
url = input("Enter the website URL: ")
alarm_time = input("Enter alarm time (HH:MM:SS in 24-hour format): ")
try:
# Validate time format
datetime.strptime(alarm_time, '%H:%M:%S')
print(f"Alarm set for {alarm_time} to open {url}")
while True:
current_time = time.strftime('%H:%M:%S')
if current_time == alarm_time:
print(f"\nTime reached! Opening {url}...")
webbrowser.open(url)
break
else:
print(f"Current time: {current_time} | Waiting for {alarm_time}", end='\r')
time.sleep(1)
except ValueError:
print("Invalid time format! Please use HH:MM:SS format.")
# Example usage
enhanced_web_alarm()
Conclusion
The website alarm system provides an automated way to open web pages at specific times. This tool is useful for daily routines like checking emails, news, or social media at scheduled times. The enhanced version includes better error handling and user input validation for a more robust experience.
