# L-34 MCS 275 Wed 5 Apr 2017 : copywebpage.py
"""
Script to make a local copy of a web page,
at 80 characters copy after copy.
"""
def copypage(page, file):
    """
    Given the URL for the web page,
    a copy of its contents is written to file.
    Both url and file are strings.
    """
    copyfile = open(file, 'w')
    while True:
        try:
            data = page.read(80).decode()
        except:
            print('Could not decode data.')
            break
        if data == '':
            break
        copyfile.write(data)
    page.close()
    copyfile.close()

def main():
    """
    Prompts the user for a web page,
    a file name, and then starts copying.
    """
    from urllib.request import urlopen
    print('making a local copy of a web page')
    url = input('Give URL : ')
    try:
        page = urlopen(url)
    except:
        print('Could not open the page.')
        return
    name = input('Give file name : ')
    copypage(page, name)

if __name__ == "__main__":
    main()
