#!/usr/bin/env python3
#=====================================================================================
# Retrieve a URL from the web 
#=====================================================================================
import cgi                                    #import cgi module
import urllib.request                         #urllib.request module allows fetching of urls 
import cgitb
cgitb.enable()

print("Content-Type: text/html \n")           #required http response header (w/ extra line)

elements = cgi.FieldStorage()                 #obtain the http for elements

url = elements.getvalue('url') or "https://"  #entry field             
 
msg = ''                                      #global variable

#-------------------------------------------------------------------------------------
# Function to validate entry fields
#-------------------------------------------------------------------------------------
def validate():
    global msg                                #needed to modify global variable
                                              #no need if access to global is read only
    if url=='':
        msg = '*** Please enter URL ***'

#-------------------------------------------------------------------------------------
# Function to retrieve a URL from the web
#-------------------------------------------------------------------------------------
def getURL(url):
    global msg                                #needed to modify global variable

    url2 = url.replace(' ','%20')             #replace space with %20 

    try:
        resource = urllib.request.urlopen(url2)     #send the request
        page     = resource.read()                  #read the returned data as a bytes object
    except:
        msg  = "Could not retrieve URL"
        page = ""
       
    try:                                            #try
        page = page.decode()                        #decode the bytes object into a string
    except:                                         #if unsuccessful                                            
        pass                                        #do nothing                     
    
    return(page)
        
#-------------------------------------------------------------------------------------
# Function to display the page
#-------------------------------------------------------------------------------------
def display(page):
    global msg                             #needed to modify global variable
                                           #no need if access to blobal is read only
    print('''
        <!DOCTYPE html>
        <html>
        <head>
        <title>Get URL</title>
        <style type="text/css">
            #Layer3 {position:relative; left:60px; top:10px; width:780px; height:110px;}
            .style4 {font-family: Arial, Helvetica, sans-serif; font-size:16px; font-weight: bold;}
            span    {font-weight:bold; cursor:pointer}
        </style>
        </head>
        <body bgcolor='lightyellow'>
        <div id='Layer3'>

        <form METHOD=post ACTION=getURL.py>                
        <fieldset style='width:750'>
        <legend>Get a URL</legend> 

        <table border='0'>
        <tr>
        <td> Location </td>
    ''')
    print("<td><input type='text' id='url' name='url' size='75' value='" + url + "'>") 
    print(" include http(s)://")

    print(''' 
        </tr>
        <tr>
        <td></td>
        <td><input type='submit' value='  Retrieve  '>
    ''')
    print("<font color='red'>" + msg + "</font></td>")
    print('''
        </tr>
        </table>       
        </fieldset>
        </form>
        </div>
        <i>Example:</i> 
        <span onclick=document.getElementById('url').value=this.innerHTML>https://google.com</span>
        <br>
        <i>Example:</i> 
        <span onclick=document.getElementById('url').value=this.innerHTML>https://workshop.sps.nyu.edu/~sultans/python/ </span>
        <br>
        <i>Example:</i> 
        <span onclick=document.getElementById('url').value=this.innerHTML>https://workshop.sps.nyu.edu/~sultans/util/rest/REST.py?user=demo&pswd=demo&db=demo&sql=show tables </span>
        <hr>
    ''')

    print(page)          #print the retrieved URL

#-------------------------------------------------------------------------------------
# Main code
#-------------------------------------------------------------------------------------

page = ''

if elements:                                    #if data was entered in the form 
    validate()                                  #validate entered data                            
    page = getURL(url)                          #retrieve the url

display(page)                                   #call display function




#=== link to see the python code ================================================
import os
import sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#================================================================================