Skip to content

Instantly share code, notes, and snippets.

@horimislime
Created June 3, 2012 14:31
Show Gist options
  • Save horimislime/2863746 to your computer and use it in GitHub Desktop.
Save horimislime/2863746 to your computer and use it in GitHub Desktop.
指定時間にiTunesのプレイリストを再生する
#-*-coding:utf-8-*-
#Usage: python tunes-alarm.py -t "2012-06-04 07:00:00" -p "Playlist"
import sched, time
import datetime
import os
from optparse import OptionParser
def playTunes(playlist):
script = """osascript -e '
tell application "iTunes"
play playlist "%s"
end tell
'
""" % playlist
os.system(script)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-t", "--time", dest = "time", help = "Time to alert", default = False)
parser.add_option("-p", "--playlist", dest = "playlist", help = "Name of List to play", default = False)
(opts, args) = parser.parse_args()
if not opts.time or not opts.playlist:
parser.error("-t and -p are required!")
parser.print_help()
exit(-1)
dtime = datetime.datetime.strptime(opts.time, "%Y-%m-%d %H:%M:%S")
now = datetime.datetime.now()
delta = dtime-now
if delta.days < 0:
raise "Time should be later than now!"
sch = sched.scheduler(time.time, time.sleep)
sch.enter(delta.seconds, 1, playTunes, (opts.playlist,))
sch.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment