Alarm clock writen in Python

Ngủ nướng quá, nên code cái nì báo thứ :) . Sẽ update tiếp các phiên bản sau "chất lượng" hơn .


System requirement :
      - Gnome-mplayer (maybe swich to your favor media player)
      - Internet connection (Default sound, change it if you want)
      - Python 2.6

Source code :


#!/usr/bin/env python
import sys, time, os
import getopt
import datetime

def usage():
    print '''
Alarm clock v0.1 beta
            
Usage : ./alarm.py -h hour -m minute
Type : --help for more imformation
        '''
link = ""
def gettime():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "h:m:l:",["help"])
    except getopt.GetoptError, err:
        print str(err)
        usage()
        sys.exit(2)
    hour = 7
    minute = 0
    link = "link direct to mp3 file"
    for opt, args in opts:
        if opt == "-h":
            hour = int(args)
        elif opt in ("-m"):
            minute = int(args)
        elif opt in ("--help"):
            usage()
            sys.exit()
        elif opt in ("-l"):
            link = args
        else:
            assert False, "unhandled option"
    print "Program will active alarm time in : %.2d:%.2d" %( hour, minute)
    print "Sound : %s" %link
    now = datetime.datetime.now()
    if (hour - now.hour)>=0 :
        t = (hour - now.hour)*3600
    else:
        t = 24 - (now.hour - hour)*3600
    if (minute -now.minute ) >= 0:
        t = t + (minute - now.minute)*60
    else:
        t = t - (now.minute - minute)*60
    if t<0:
        t = 24*3600 + t
    return t, link
if __name__ == "__main__":
    waitTime, link = gettime()
    time.sleep(waitTime)
    links = "gnome-mplayer %s  --random --loop" % link
    os.popen(links)

_http://crazyboy.pastebin.com/BjctSLf1

cb_

No comments:

Post a Comment