# Path: cachedfetch.py
# def cachedFetch(url, expiretime=3600, maxlength=100000):    # fetch url, with memcache
#     # Enable this to effectively clear memcache on new version deployment
#     #key = os.environ['CURRENT_VERSION_ID'] + '/' + url
#     key = url
#     data = memcache.get(key)
#     if data is not None:
#         #print >> sys.stderr, "From memcache: " + url 
#         return data
#     else:
#         #print >> sys.stderr, "From fetch: " + url
#         try:
#             result = urllib2.urlopen(url)
#             data = result.read(maxlength)
#             result.close()
#             memcache.add(key, data, expiretime)
#             return data
#         except urllib2.URLError, e:
#             return ''
#         except DownloadError:
#             return ''


import cgi
import cgitb; cgitb.enable()
import urllib
import re
from cachedfetch import cachedFetch
from django.utils import simplejson as json
#import urllib2        # Now called inside cachedFetch

def error(message):
    print 'Content-Type: text/plain'
    print ''
    print message
    exit()

def main():
    form = cgi.FieldStorage()
    if (not form.has_key("isbn")):
      error('No isbn!')
    isbn = form["isbn"].value
    isbn = re.sub('[^0-9]', '', isbn)

    if (not form.has_key("callback")):
      error('No callback.')
    callback = form["callback"].value


    url = "http://diberri.dyndns.org/cgi-bin/templatefiller/index.cgi?type=isbn&vertical=1&dont_use_etal=1&format=xml&id=" + isbn


# Path: cachedfetch.py
# def cachedFetch(url, expiretime=3600, maxlength=100000):    # fetch url, with memcache
#     # Enable this to effectively clear memcache on new version deployment
#     #key = os.environ['CURRENT_VERSION_ID'] + '/' + url
#     key = url
#     data = memcache.get(key)
#     if data is not None:
#         #print >> sys.stderr, "From memcache: " + url 
#         return data
#     else:
#         #print >> sys.stderr, "From fetch: " + url
#         try:
#             result = urllib2.urlopen(url)
#             data = result.read(maxlength)
#             result.close()
#             memcache.add(key, data, expiretime)
#             return data
#         except urllib2.URLError, e:
#             return ''
#         except DownloadError:
#             return ''


# Path: getdiberri.py
import cgi
import cgitb; cgitb.enable()
import urllib
import re
from cachedfetch import cachedFetch
from django.utils import simplejson as json

#import urllib2        # Now called inside cachedFetch

def error(message):
    print 'Content-Type: text/plain'
    print ''
    print message
    exit()

def main():
    form = cgi.FieldStorage()
    if (not form.has_key("isbn")):
      error('No isbn!')
    isbn = form["isbn"].value
    isbn = re.sub('[^0-9]', '', isbn)

    if (not form.has_key("callback")):
      error('No callback.')
    callback = form["callback"].value


    url = "http://diberri.dyndns.org/cgi-bin/templatefiller/index.cgi?type=isbn&vertical=1&dont_use_etal=1&format=xml&id=" + isbn


    xmldata = cachedFetch(url, 3600)