# 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 ''


from cachedfetch import cachedFetch
from keys import crossref_id
import urllib
import xml.dom.minidom

def getText(startnode):
    rc = ""
    for node in startnode.childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc


def doiFetchInfo(doi):

    querystring = urllib.urlencode([('id', 'doi:'+doi)])
    url = "http://www.crossref.org/openurl/?pid=" + crossref_id + "&noredirect=true&" + querystring

# 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: doifetch.py
from cachedfetch import cachedFetch
from keys import crossref_id
import urllib
import xml.dom.minidom


def getText(startnode):
    rc = ""
    for node in startnode.childNodes:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc


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)