GithubHelp home page GithubHelp logo

alarm-clock's Introduction

Alarm System

Description

Its a handy alarm system made using python.

Tools and Languages:

Python

pip

VS Code


Installing Libraries

pip install datetime
pip install os
pip install time
pip install random
pip install webbrowser

-> Mostly probably you should already have OS. datetime, time and random libraries installed as it comes with python distribution.

Steps to follow

-Install the given libraries
-Download the code from the given github repository
-Save your desired video link in file -Run the code

Breaking the code

if not os.path.isfile("youtube_alarm_videos.txt"):
    print('Creating "youtube_alarm_videos.txt"...')
    with open("youtube_alarm_videos.txt", "w") as alarm_file:
        alarm_file.write("https://youtu.be/xaXLLF2qm20")

In this conditional block, the code searches for the youtube_alarm_video.txt file. If there is no such file, it creates one. Then it writes the link of your desired video into the file.

def check_alarm_input(alarm_time):
    if len(alarm_time) == 1: 
        if alarm_time[0] < 24 and alarm_time[0] >= 0:
            return True
    if len(alarm_time) == 2:
        if alarm_time[0] < 24 and alarm_time[0] >= 0 and \
           alarm_time[1] < 60 and alarm_time[1] >= 0:
            return True
    elif len(alarm_time) == 3: 
        if alarm_time[0] < 24 and alarm_time[0] >= 0 and \
           alarm_time[1] < 60 and alarm_time[1] >= 0 and \
           alarm_time[2] < 60 and alarm_time[2] >= 0:
            return True
    return False

This function checkes whether the user entered a valid time, otherwise it returns false.

print("Set a time for the alarm (Ex. 06:30 or 18:30:00)")
while True:
    alarm_input = input(">> ")
    try:
        alarm_time = [int(n) for n in alarm_input.split(":")]
        if check_alarm_input(alarm_time):
            break
        else:
            raise ValueError
    except ValueError:
        print("ERROR: Enter time in HH:MM or HH:MM:SS format")

This block here takes input of time and checks for its validity.

seconds_hms = [3600, 60, 1] 
alarm_seconds = sum([a*b for a,b in zip(seconds_hms[:len(alarm_time)], alarm_time)])
now = datetime.datetime.now()
current_time_seconds = sum([a*b for a,b in zip(seconds_hms, [now.hour, now.minute, now.second])])
time_diff_seconds = alarm_seconds - current_time_seconds
if time_diff_seconds < 0:
    time_diff_seconds += 86400 
print("Alarm set to go off in %s" % datetime.timedelta(seconds=time_diff_seconds))
time.sleep(time_diff_seconds)
print("Wake Up!")
with open("youtube_alarm_videos.txt", "r") as alarm_file:
    videos = alarm_file.readlines()
webbrowser.open(random.choice(videos))

Once the time is verified to be proper, the timer is set. When the timer goes off the video saved earlier is played in the browser window.

Developed by:

Ankush Mishra

alarm-clock's People

Contributors

ankush0939 avatar binayaksadangi avatar pygeekspro avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.