Create a Website Alarm Using Python


In this section we will see how to create the website alarm system using Python.

Problem Statement

Open a website URL on the browser by taking a website URL and time. When the system time reaches the specified time, the webpage will be opened.

We can store different web pages in our bookmark sections. Sometimes we need to open some web pages every day on a specific time to do some work. For that purpose, we can set this type of website alarm to do the work.

In this case we are using some standard library modules like sys, web browser and time.

Steps to open web pages on a specific time

  • Take the URL which will be opened.
  • Take the time to open the webpage on that time.
  • Check whether the current time is matched with the specified time or not.
    • If the time matches, open the webpage. Otherwise wait for one second.
    • Repeat the step 3 in each second until the time matches.
  • End the process

Example Code

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)
web_alarm(sys.argv[1], sys.argv[2])    #Set the alarm using url and time

Output

$ python3 397.Website_Alarm.py https://www.tutorialspoint.com/ 02:01:00
Current time is: 02:00:46
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...
$

Website Alarm

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

357 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements