# Path: nytimesfetch.py
# def nytimesFetchInfo(url):
#     #print >> sys.stderr, 'url: ' + url
#     #http://api.nytimes.com/svc/search/v1/article?format=json&query=url%3Ahttp%3A%2F%2Fwww.nytimes.com%2F2009%2F03%2F26%2Fgarden%2F26slow.html&fields=title%2C+nytd_title%2C+date%2C+byline%2C+page_facet%2C+section_page_facet%2C+source_facet&api-key=#### 
#     urlfixed = re.sub('\?.*$', '', url)
#     #print >> sys.stderr, urlfixed
#     querystring = urllib.urlencode({'format': 'json',
#                                     'query': 'url:'+urlfixed,
#                                     'fields': 'title, date, byline, page_facet, section_page_facet, source_facet, column_facet',
#                                     'api-key': nyt_apikey})
#     nyturl = "http://api.nytimes.com/svc/search/v1/article?" + querystring
#     jsonstring = cachedFetch(nyturl, 3600)
#     if jsonstring == '':
#         error('Bad data from NYTimes')
#     
#     nytdata = json.loads(jsonstring)
#     #print >> sys.stderr, nytdata
#     if len(nytdata['results']) == 0:
#         sys.stderr.write("No data from " + nyturl + "\n")
#         return {}
#     
#     result = nytdata['results'][0]
#     citedata = {'work': 'The New York Times'}
#     citedata['title'] = result['title']
#     if result.has_key('page_facet'):
#         citedata['page'] = result['page_facet']
#     if result.has_key('date'):
#         m = re.search('(\d\d\d\d)(\d\d)(\d\d)', result['date'])
#         if m:
#             citedata['date'] = m.group(1) + '-' + m.group(2) + '-' + m.group(3)
#     if result.has_key('byline'):
#         byline = result['byline']
#         byline = re.sub('(?i)^By\s+', '', byline)
#         byline = re.sub('(?i) And ', ', ', byline)
#         byline = byline.title()
#         authors = re.split('\s*,\s*', byline)
#         citedata['authors'] = authors
# 
#     
#     return citedata


from nytimesfetch import nytimesFetchInfo
import cgi
import re
import time
import sys

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

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


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

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

    if not form.has_key("url"):
        print """<hr><font color="DarkOliveGreen">Example URL (copy and paste above):</font> http://www.nytimes.com/2007/12/25/world/africa/25kenya.html
        <p>Try also: <a href="/">Wikipedia citation tool for Google Books</a> or <a href="/doiweb.py">DOI</a></p>"""
        printFooter()
        return

    url = url.strip()    #Trim whitespace
    url = re.sub('\?.*$', '', url)    #Remove from the question mark
    if not re.search('^http:\/\/[^\/]*nytimes\.com\/', url, re.I):
        error('Not a New York Times URL.')
        return

# Path: nytimesfetch.py
# def nytimesFetchInfo(url):
#     #print >> sys.stderr, 'url: ' + url
#     #http://api.nytimes.com/svc/search/v1/article?format=json&query=url%3Ahttp%3A%2F%2Fwww.nytimes.com%2F2009%2F03%2F26%2Fgarden%2F26slow.html&fields=title%2C+nytd_title%2C+date%2C+byline%2C+page_facet%2C+section_page_facet%2C+source_facet&api-key=#### 
#     urlfixed = re.sub('\?.*$', '', url)
#     #print >> sys.stderr, urlfixed
#     querystring = urllib.urlencode({'format': 'json',
#                                     'query': 'url:'+urlfixed,
#                                     'fields': 'title, date, byline, page_facet, section_page_facet, source_facet, column_facet',
#                                     'api-key': nyt_apikey})
#     nyturl = "http://api.nytimes.com/svc/search/v1/article?" + querystring
#     jsonstring = cachedFetch(nyturl, 3600)
#     if jsonstring == '':
#         error('Bad data from NYTimes')
#     
#     nytdata = json.loads(jsonstring)
#     #print >> sys.stderr, nytdata
#     if len(nytdata['results']) == 0:
#         sys.stderr.write("No data from " + nyturl + "\n")
#         return {}
#     
#     result = nytdata['results'][0]
#     citedata = {'work': 'The New York Times'}
#     citedata['title'] = result['title']
#     if result.has_key('page_facet'):
#         citedata['page'] = result['page_facet']
#     if result.has_key('date'):
#         m = re.search('(\d\d\d\d)(\d\d)(\d\d)', result['date'])
#         if m:
#             citedata['date'] = m.group(1) + '-' + m.group(2) + '-' + m.group(3)
#     if result.has_key('byline'):
#         byline = result['byline']
#         byline = re.sub('(?i)^By\s+', '', byline)
#         byline = re.sub('(?i) And ', ', ', byline)
#         byline = byline.title()
#         authors = re.split('\s*,\s*', byline)
#         citedata['authors'] = authors
# 
#     
#     return citedata


# Path: nytweb.py
from nytimesfetch import nytimesFetchInfo
import cgi
import re
import time
import sys


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

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


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

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

    if not form.has_key("url"):
        print """<hr><font color="DarkOliveGreen">Example URL (copy and paste above):</font> http://www.nytimes.com/2007/12/25/world/africa/25kenya.html
        <p>Try also: <a href="/">Wikipedia citation tool for Google Books</a> or <a href="/doiweb.py">DOI</a></p>"""
        printFooter()
        return

    url = url.strip()    #Trim whitespace
    url = re.sub('\?.*$', '', url)    #Remove from the question mark
    if not re.search('^http:\/\/[^\/]*nytimes\.com\/', url, re.I):
        error('Not a New York Times URL.')
        return

    citedata = nytimesFetchInfo(url)