import time
import urllib2
baseurl = 'http://laits.utexas.edu/compeco/Courses/'
page = urllib2.urlopen(baseurl+'index392.html')
information = page.read()
information_links = information.split('href="')[1:]
list_of_links = [t[:t.find('"')] for t in information_links]
list_of_links = list(set(list_of_links))
for x in list_of_links:
    qq =  list_of_links.index(x)
    extension = x[::-1][:x[::-1].find('.')][::-1]
    name = x[::-1][:x[::-1].find('/')][::-1]
    print 'now working on file '+str(qq+1)+' of '+str(len(list_of_links))
    if len(extension) > 5:
        print 'Warning! Possible false positive for file match. File extension reported as'     
        print extension+' and is longer than 5 characters. Likely not a file; skipping.'
        continue
    time.sleep(7)
    file_to_write_name = str(qq+1)+'.'+extension
    if x[0:4] == 'http':
        try:
            newpage = urllib2.urlopen(x.replace(' ','%20'))
        except urllib2.HTTPError, e:
            print e 
            print 'File '+name+' not found. Continuing to next file.'
            continue
    else:
        try:
            newpage = urllib2.urlopen(baseurl+x.replace(' ','%20'))
        except urllib2.HTTPError, e:
            print e 
            print 'File '+name+' not found. Continuing to next file.'
            continue

    try:
        name = x[::-1][:x[::-1].find('/')][::-1]
        localFile = open(name,'w')
        # Macintosh users will need to write to '/home/<user>   
        localFile.write(newpage.read())
        localFile.close()
        newpage.close()
    except IOError:
       print 'Warning! File '+str(qq+1)+" failed to write; possibly link is to a web page. \nTried to write to "+file_to_write_name+'.'


