{"id":1040,"date":"2021-07-19T22:42:06","date_gmt":"2021-07-19T17:12:06","guid":{"rendered":"https:\/\/cbsepython.in\/?p=1040"},"modified":"2021-09-29T22:10:37","modified_gmt":"2021-09-29T16:40:37","slug":"alarm-clock-python-project-for-class-12","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/alarm-clock-python-project-for-class-12\/","title":{"rendered":"Alarm Clock Python Project for Class 12"},"content":{"rendered":"<p>Alarm Clock Python Project for Class 12<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Source Code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\"># https:\/\/cbsepython.in\/\r\n# Alarm Clock\r\n\r\n\"\"\"Simple Python script to set an alarm for a specific time.\r\n   When the alarm goes off, a random youtube video will be opened.\r\n   The possible youtube video URLs are taken from \"youtube_alarm_videos.txt\"\r\n\"\"\"\r\n\r\nimport datetime\r\nimport os\r\nimport time\r\nimport random\r\nimport webbrowser\r\n\r\n# If video URL file does not exist, create one\r\nif not os.path.isfile(\"youtube_alarm_videos.txt\"):\r\n    print('Creating \"youtube_alarm_videos.txt\"...')\r\n    with open(\"youtube_alarm_videos.txt\", \"w\") as alarm_file:\r\n        alarm_file.write(\"https:\/\/www.youtube.com\/watch?v=3wc2lG_dp6g\")\r\n\r\ndef check_alarm_input(alarm_time):\r\n    \"\"\"Checks to see if the user has entered in a valid alarm time\"\"\"\r\n    if len(alarm_time) == 1: # [Hour] Format\r\n        if alarm_time[0] &lt; 24 and alarm_time[0] &gt;= 0:\r\n            return True\r\n    if len(alarm_time) == 2: # [Hour:Minute] Format\r\n        if alarm_time[0] &lt; 24 and alarm_time[0] &gt;= 0 and \\\r\n           alarm_time[1] &lt; 60 and alarm_time[1] &gt;= 0:\r\n            return True\r\n    elif len(alarm_time) == 3: # [Hour:Minute:Second] Format\r\n        if alarm_time[0] &lt; 24 and alarm_time[0] &gt;= 0 and \\\r\n           alarm_time[1] &lt; 60 and alarm_time[1] &gt;= 0 and \\\r\n           alarm_time[2] &lt; 60 and alarm_time[2] &gt;= 0:\r\n            return True\r\n    return False\r\n\r\n# Get user input for the alarm time\r\nprint(\"Set a time for the alarm (Ex. 06:30 or 18:30:00)\")\r\nwhile True:\r\n    alarm_input = input(\"&gt;&gt; \")\r\n    try:\r\n        alarm_time = [int(n) for n in alarm_input.split(\":\")]\r\n        if check_alarm_input(alarm_time):\r\n            break\r\n        else:\r\n            raise ValueError\r\n    except ValueError:\r\n        print(\"ERROR: Enter time in HH:MM or HH:MM:SS format\")\r\n\r\n# Convert the alarm time from [H:M] or [H:M:S] to seconds\r\nseconds_hms = [3600, 60, 1] # Number of seconds in an Hour, Minute, and Second\r\nalarm_seconds = sum([a*b for a,b in zip(seconds_hms[:len(alarm_time)], alarm_time)])\r\n\r\n# Get the current time of day in seconds\r\nnow = datetime.datetime.now()\r\ncurrent_time_seconds = sum([a*b for a,b in zip(seconds_hms, [now.hour, now.minute, now.second])])\r\n\r\n# Calculate the number of seconds until alarm goes off\r\ntime_diff_seconds = alarm_seconds - current_time_seconds\r\n\r\n# If time difference is negative, set alarm for next day\r\nif time_diff_seconds &lt; 0:\r\n    time_diff_seconds += 86400 # number of seconds in a day\r\n\r\n# Display the amount of time until the alarm goes off\r\nprint(\"Alarm set to go off in %s\" % datetime.timedelta(seconds=time_diff_seconds))\r\n\r\n# Sleep until the alarm goes off\r\ntime.sleep(time_diff_seconds)\r\n\r\n# Time for the alarm to go off\r\nprint(\"Wake Up!\")\r\n\r\n# Load list of possible video URLs\r\nwith open(\"youtube_alarm_videos.txt\", \"r\") as alarm_file:\r\n    videos = alarm_file.readlines()\r\n\r\n# Open a random video from the list\r\nwebbrowser.open(random.choice(videos))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Output:<\/p>\n<pre>Set a time for the alarm (Ex. 06:30 or 18:30:00)\r\n&gt;&gt; 22:45:00\r\nAlarm set to go off in 0:04:11<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Alarm Clock Python Project for Class 12 &nbsp; &nbsp; Source Code: # https:\/\/cbsepython.in\/ # Alarm Clock &#8220;&#8221;&#8221;Simple Python script to set an alarm for a specific time. When the alarm goes off, a random youtube video will be opened. The possible youtube video URLs are taken from &#8220;youtube_alarm_videos.txt&#8221; &#8220;&#8221;&#8221; import datetime import os import time [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[],"class_list":["post-1040","post","type-post","status-publish","format-standard","hentry","category-python-projects"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/1040","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=1040"}],"version-history":[{"count":0,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/1040\/revisions"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=1040"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=1040"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=1040"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}