# Path: doifetch.py
# def doiFetchInfo(doi):
# 
#     querystring = urllib.urlencode([('id', 'doi:'+doi)])
#     url = "http://www.crossref.org/openurl/?pid=" + crossref_id + "&noredirect=true&" + querystring
#     xmldata = cachedFetch(url, 3600)
#     if xmldata == '':
#         error('Bad data from CrossRef')
#     
#     xmldoc = xml.dom.minidom.parseString(xmldata)
#     citedata = {}
#     
#     simplefields = {'doi': 'doi',
#     'issn': 'issn',
#     'journal': 'journal_title',
#     'volume': 'volume',
#     'issue': 'issue',
#     'year': 'year',
#     'title': 'article_title',
#     'isbn': 'isbn',
#     }
#     
#     for key, cr_tagname in simplefields.iteritems():
#         cr_node = xmldoc.getElementsByTagName(cr_tagname)
#         if cr_node:
#             citedata[key] = getText(cr_node[0])
# 
#     cr_contributors = xmldoc.getElementsByTagName('contributor')
#     authors = []
#     for cr_contributor in cr_contributors:
#         author = {}
#         cr_givenname = cr_contributor.getElementsByTagName('given_name')
#         if cr_givenname:
#             author['first'] = getText(cr_givenname[0])
#         cr_surname = cr_contributor.getElementsByTagName('surname')
#         if cr_surname:
#             author['last'] = getText(cr_surname[0])
#         if author['first'].isupper() and author['last'].isupper():
#             author['first'] = author['first'].title()
#             author['last'] = author['last'].title()
#         authors.append(author)
#     if authors:
#         citedata['authors'] = authors
#     
#     cr_first_page = xmldoc.getElementsByTagName('first_page')
#     cr_last_page = xmldoc.getElementsByTagName('last_page')
#     if cr_first_page:
#         citedata['pages'] = getText(cr_first_page[0])
#     if cr_last_page:
#         citedata['pages'] += u"\u2013" + getText(cr_last_page[0])
#         
# 
#     return citedata


from doifetch import doiFetchInfo
import cgi
    print 'Content-Type: text/html; charset=utf-8'
    print ''
    print """<!DOCTYPE html><html><head>
             <title>DOI Wikipedia reference generator</title>

        <link rel="shortcut icon" href="/favicon.ico" />

        </head><body style="font-family: sans-serif; font-size:0.79375em">
        <h1><a href="/doiweb.py" style="text-decoration: none; color: black">{<font color="red">T</font>} DOI Wikipedia reference generator</a></h1>
        """


    form = cgi.FieldStorage()
    doi = ''
    if form.has_key("doi"):
        doi = form["doi"].value

    print """<hr><form action="" method="get">
        <label for="doi">DOI:</label>
        <input type="text" size="80" name="doi" id="doi" tabindex=1 value="%s">
        <input type="submit" value="Load" tabindex=1>
        </form>""" % (cgi.escape(doi, 1))

    if not form.has_key("doi"):
        print """<hr><font color="DarkOliveGreen">Example DOI (copy and paste above):</font> 10.1111/j.1600-0404.1986.tb04634.x
        <p>Try also: <a href="/">Wikipedia citation tool for Google Books</a> or <a href="/nytweb.py">The New York Times</a></p>"""
        printFooter()
        return

    doi = doi.strip()    #Trim whitespace

# Path: doifetch.py
# def doiFetchInfo(doi):
# 
#     querystring = urllib.urlencode([('id', 'doi:'+doi)])
#     url = "http://www.crossref.org/openurl/?pid=" + crossref_id + "&noredirect=true&" + querystring
#     xmldata = cachedFetch(url, 3600)
#     if xmldata == '':
#         error('Bad data from CrossRef')
#     
#     xmldoc = xml.dom.minidom.parseString(xmldata)
#     citedata = {}
#     
#     simplefields = {'doi': 'doi',
#     'issn': 'issn',
#     'journal': 'journal_title',
#     'volume': 'volume',
#     'issue': 'issue',
#     'year': 'year',
#     'title': 'article_title',
#     'isbn': 'isbn',
#     }
#     
#     for key, cr_tagname in simplefields.iteritems():
#         cr_node = xmldoc.getElementsByTagName(cr_tagname)
#         if cr_node:
#             citedata[key] = getText(cr_node[0])
# 
#     cr_contributors = xmldoc.getElementsByTagName('contributor')
#     authors = []
#     for cr_contributor in cr_contributors:
#         author = {}
#         cr_givenname = cr_contributor.getElementsByTagName('given_name')
#         if cr_givenname:
#             author['first'] = getText(cr_givenname[0])
#         cr_surname = cr_contributor.getElementsByTagName('surname')
#         if cr_surname:
#             author['last'] = getText(cr_surname[0])
#         if author['first'].isupper() and author['last'].isupper():
#             author['first'] = author['first'].title()
#             author['last'] = author['last'].title()
#         authors.append(author)
#     if authors:
#         citedata['authors'] = authors
#     
#     cr_first_page = xmldoc.getElementsByTagName('first_page')
#     cr_last_page = xmldoc.getElementsByTagName('last_page')
#     if cr_first_page:
#         citedata['pages'] = getText(cr_first_page[0])
#     if cr_last_page:
#         citedata['pages'] += u"\u2013" + getText(cr_last_page[0])
#         
# 
#     return citedata


# Path: doiweb.py
from doifetch import doiFetchInfo
import cgi

    print 'Content-Type: text/html; charset=utf-8'
    print ''
    print """<!DOCTYPE html><html><head>
             <title>DOI Wikipedia reference generator</title>

        <link rel="shortcut icon" href="/favicon.ico" />

        </head><body style="font-family: sans-serif; font-size:0.79375em">
        <h1><a href="/doiweb.py" style="text-decoration: none; color: black">{<font color="red">T</font>} DOI Wikipedia reference generator</a></h1>
        """


    form = cgi.FieldStorage()
    doi = ''
    if form.has_key("doi"):
        doi = form["doi"].value

    print """<hr><form action="" method="get">
        <label for="doi">DOI:</label>
        <input type="text" size="80" name="doi" id="doi" tabindex=1 value="%s">
        <input type="submit" value="Load" tabindex=1>
        </form>""" % (cgi.escape(doi, 1))

    if not form.has_key("doi"):
        print """<hr><font color="DarkOliveGreen">Example DOI (copy and paste above):</font> 10.1111/j.1600-0404.1986.tb04634.x
        <p>Try also: <a href="/">Wikipedia citation tool for Google Books</a> or <a href="/nytweb.py">The New York Times</a></p>"""
        printFooter()
        return

    doi = doi.strip()    #Trim whitespace

    citedata = doiFetchInfo(doi)