Skip to content

Instantly share code, notes, and snippets.

@kyujin-cho
Last active November 22, 2017 14:02
Show Gist options
  • Select an option

  • Save kyujin-cho/3c43d2c6bc9b089a110e68b9d057986d to your computer and use it in GitHub Desktop.

Select an option

Save kyujin-cho/3c43d2c6bc9b089a110e68b9d057986d to your computer and use it in GitHub Desktop.
없어서 직접 만든 타이머
#!/usr/local/bin/python3
import datetime
import sys
import time
import webbrowser
import re
import urllib.request
from bs4 import BeautifulSoup as BSoup
import subprocess
def get_time():
current_time = datetime.datetime.now()
return [current_time.year, current_time.month, current_time.day, current_time.hour, \
current_time.minute, current_time.second]
if len(sys.argv) < 3:
print('Usage: timer.py URL [Year] [Month] [Day] Hour Minute')
exit()
current_time = get_time()
desired_time = current_time[:(7-len(sys.argv))] + [int(x) for x in sys.argv[2:]]
if not re.search(r'https?:\/\/www\.youtube\.com\/watch\?v\=[0-9A-Za-z]+', sys.argv[1]):
print('Not a valid URL Format!')
title = BSoup(urllib.request.urlopen(sys.argv[1]).read(), 'html.parser').find('title').text.replace(' - YouTube', '')
if current_time[:5] > desired_time:
print('Not a valid time')
exit()
if 'darwin' in sys.platform:
print('Running \'caffeinate\' on macOS to prevent the system from sleeping')
subprocess.Popen('caffeinate')
print('Playing {} at {}/{}/{} {}:{}:00...'.format(title, *map(lambda x: str(x) if x >= 10 else '0' + str(x), desired_time)))
while desired_time[:5] > current_time:
print('{}/{}/{} {}:{}:{}'.format(*map(lambda x: str(x) if x >= 10 else '0' + str(x), current_time)))
time.sleep(1)
current_time = get_time()
print('It\'s the time! Opening {}...'.format(title))
webbrowser.open(sys.argv[1], new=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment