# 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 django.utils import simplejson as json
from keys import nyt_apikey
import urllib
import re
import sys


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

# 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: nytimesfetch.py
from cachedfetch import cachedFetch
from django.utils import simplejson as json
from keys import nyt_apikey
import urllib
import re
import sys



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)