Wednesday, August 21, 2013

颱風飛機航班取消告知程式

颱風天,但不久後就要出國,於是開啓航空公司的網頁不斷重新整理看自己的航班有沒有出現在「取消」的清單中,然而這樣真是麻煩,於是寫了一個小程式就可以安心去準備行李了!

當航空公司頁面有更新的時候這隻程式便會發出聲音,而我設定的聲音則是「I am a bad feeling about this」,是個從免費網站下載的聲音檔。


#!/usr/bin/python
import sched, time
import urllib2
import pygame


s = sched.scheduler(time.time, time.sleep)
url = "http://www.china-airlines.com/ch/schedule/cancel.htm"

original_html = ""

def do_something(sc):

    global url, original_html

    # do your stuff
    response = urllib2.urlopen(url)
    html = response.read()

    if cmp(original_html, html):
        print "New!!!"
        pygame.init()
        pygame.mixer.Sound('sound.wav').play()
        exit()

    sc.enter(2, 1, do_something, (sc,))

def original():
    global original_html
    response = urllib2.urlopen(url)
    original_html = response.read()

def main():

    original()

    s.enter(2, 1, do_something, (s,))
    s.run()

if __name__ == "__main__":
    main()