Created
December 27, 2020 12:55
-
-
Save Jeremiah-England/257344998f7ff16a633a38380b50d046 to your computer and use it in GitHub Desktop.
Start playing a song randomly between two times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
A short script to start a song at a random time on Christmas morning. | |
Usage | |
----- | |
You will need a way to keep your computer from falling asleep. I | |
used the Caffiene program which I believe is crossplatform. If your | |
computer does fall asleep, run out of batteries, get stolen by | |
Santa, etc; the music will not play in the morning. | |
The values you will want to configure are in the | |
`if __name__ == '__main__':` section at the end of the code. You will | |
need to set the minimum time and maximum time the song can play and | |
the file path to the song file. | |
You should probably test the setup by running the script with the | |
start and end times set to next two minutes or something. | |
When you are ready, run `python christmas_music_timer.py` in your terminal | |
and wait for the music! | |
""" | |
import os | |
import sys | |
import subprocess | |
import time | |
import datetime | |
import random | |
# From https://stackoverflow.com/a/17317468/9849440 | |
def open_file(filename): | |
if sys.platform == "win32": | |
os.startfile(filename) | |
else: | |
opener = "open" if sys.platform == "darwin" else "xdg-open" | |
subprocess.call([opener, filename]) | |
def start_between(file_path, start_time, end_time): | |
now = datetime.datetime.now() | |
random_seconds = random.random() * (end_time - start_time).seconds | |
sleep_seconds = (start_time - now).seconds + random_seconds | |
print(f'sleep_seconds: {sleep_seconds}') | |
time.sleep(sleep_seconds) | |
open_file(file_path) | |
if __name__ == '__main__': | |
music_file = '/home/jeremiah/Music/Sleigh Ride - Boston Pops Orchestra - Arthur Fiedler-restored.flac' | |
start_time = datetime.datetime(year=2020, month=12, day=25, hour=7, minute=0) | |
end_time = datetime.datetime(year=2020, month=12, day=25, hour=7, minute=30) | |
start_between(music_file, start_time, end_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment