#!/usr/bin/env python
import BeautifulSoup
import urllib
import os,time

def find_tag_name(tags, key, val):
    for x in tags:
        if ( key, val) in x.attrs:
            return x

def find_tag_name_all(tags, key, val):
    l = []
    for x in tags:
        if ( key, val) in x.attrs:
            l.append(x)
    return l

def FindNameAndPrint( tags, last_time ):
    print "Length of Tags", len(tags)
    list_name = []
    for x in tags:
        name = x.findAll('a')[0].contents[0].strip()
        if len(name)>0: list_name.append(name)
    for x in list_name:
        print x
    #if len(list_name)>0:
    # SMS
    #   urllib.urlopen("SMSURL").read()
    return last_time

def find_modulecontent(html, last_time ):
    soup = BeautifulSoup.BeautifulSoup(html)
    divs = soup.findAll('div')
    aos_page = find_tag_name(divs, 'class', 'modulecontent')
    return FindNameAndPrint( aos_page.findAll('td'), last_time )

def loop():
    last_time = 0
    while 1:
        try:
            html = urllib.urlopen('http://store.apple.com/kr/browse/home/specialdeals/mac').read()
            last_time = find_modulecontent(html, last_time )
            time.sleep(30)
        except:
            print "Error"
            pass

if __name__ == '__main__':
    loop()