Permalink
Browse files

Added jsonp support through a new callback request parameter

  • Loading branch information...
1 parent 5e80980 commit de273cb067e43b50ec6628d2db1774b32e43394d WebFlint committed Sep 3, 2012
View
@@ -1,148 +1,158 @@
import APIUtils
import DataCache
-from UserString import MutableString
import logging
import AppConfig
+
#get cached content
-def getCache(pageId,format):
- logging.debug('getCache: %s' % pageId)
- try:
- dbData = DataCache.getData(pageId,format)
- if (dbData):
- if (DataCache.hasExpired(dbData)):
- #data has expired, remove it
- try:
- dbData[0].delete()
- return None
- except:
- logging.error('getCache: unable to remove cache')
- return None
- else:
- logging.debug('getCache: got cached data for id %s' % id)
- return dbData[0].rec_xml
- except:
- logging.error('getCache: unable to get/retrieve cache')
- return None
+def getCache(pageId, format):
+ logging.debug('getCache: %s' % pageId)
+ try:
+ dbData = DataCache.getData(pageId, format)
+ if (dbData):
+ if (DataCache.hasExpired(dbData)):
+ #data has expired, remove it
+ try:
+ dbData[0].delete()
+ return None
+ except:
+ logging.error('getCache: unable to remove cache')
+ return None
+ else:
+ logging.debug('getCache: got cached data for id %s' % id)
+ return dbData[0].rec_xml
+ except:
+ logging.error('getCache: unable to get/retrieve cache')
+ return None
+
#parse HN's submissions by user
def getHackerNewsSubmittedContent(user, format='json', url='', referer='', remote_addr=''):
- #only cache homepage data
- apiURL = "%s/submitted?id=%s" % (AppConfig.hackerNewsURL, user)
- apiURLBackup = "%s/submitted?id=%s" % (AppConfig.hackerNewsURLBackup, user)
- id = '/submitted/%s' % (user)
- cachedData = None
- cachedData = getCache(id,format)
- if (cachedData):
- return cachedData
- else:
- hnData = APIUtils.parsePageContent(apiURL,apiURLBackup, '/submitted', None,format)
- if (hnData):
- logging.debug('getHackerNewsSubmittedContent: storing cached value for id %s' % id)
- DataCache.putData(id, format,APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
- return hnData
- else:
- logging.warning('getHackerNewsSubmittedContent: unable to retrieve data for id %s' % id)
- return ''
+ #only cache homepage data
+ apiURL = "%s/submitted?id=%s" % (AppConfig.hackerNewsURL, user)
+ apiURLBackup = "%s/submitted?id=%s" % (AppConfig.hackerNewsURLBackup, user)
+ id = '/submitted/%s' % (user)
+ cachedData = None
+ cachedData = getCache(id, format)
+ if (cachedData):
+ return cachedData
+ else:
+ hnData = APIUtils.parsePageContent(apiURL, apiURLBackup, '/submitted', None, format)
+ if (hnData):
+ logging.debug('getHackerNewsSubmittedContent: storing cached value for id %s' % id)
+ DataCache.putData(id, format, APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
+ return hnData
+ else:
+ logging.warning('getHackerNewsSubmittedContent: unable to retrieve data for id %s' % id)
+ return ''
+
#parse HN's comments by story id
def getHackerNewsComments(articleId, format='json', url='', referer='', remote_addr=''):
- #only cache homepage data
- apiURL = "%s/item?id=%s" % (AppConfig.hackerNewsURL, articleId)
- apiURLBackup = "%s/item?id=%s" % (AppConfig.hackerNewsURLBackup, articleId)
- id = '/comments/%s' % (articleId)
- cachedData = getCache(id,format)
- if (cachedData):
- return cachedData
- else:
- hnData = APIUtils.parseCommentsContent(apiURL, apiURLBackup, '/comments', None,format)
- if (hnData):
- logging.debug('getHackerNewsComments: storing cached value for id %s' % id)
- DataCache.putData(id, format,APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
- return hnData
- else:
- logging.warning('getHackerNewsComments: unable to retrieve data for id %s' % id)
- return ''
+ #only cache homepage data
+ apiURL = "%s/item?id=%s" % (AppConfig.hackerNewsURL, articleId)
+ apiURLBackup = "%s/item?id=%s" % (AppConfig.hackerNewsURLBackup, articleId)
+ id = '/comments/%s' % (articleId)
+ cachedData = getCache(id, format)
+ if (cachedData):
+ return cachedData
+ else:
+ hnData = APIUtils.parseCommentsContent(apiURL, apiURLBackup, '/comments', None, format)
+ if (hnData):
+ logging.debug('getHackerNewsComments: storing cached value for id %s' % id)
+ DataCache.putData(id, format, APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
+ return hnData
+ else:
+ logging.warning('getHackerNewsComments: unable to retrieve data for id %s' % id)
+ return ''
+
#parse HN's comments by story id
def getHackerNewsNestedComments(articleId, format='json', url='', referer='', remote_addr=''):
- #only cache homepage data
- apiURL = "%s/item?id=%s" % (AppConfig.hackerNewsURL, articleId)
- apiURLBackup = "%s/item?id=%s" % (AppConfig.hackerNewsURLBackup, articleId)
- id = '/nestedcomments/%s' % (articleId)
- #cache data
- cachedData = getCache(id,format)
- if (cachedData):
- return cachedData
- else:
- try:
- hnData = APIUtils.parseNestedCommentsContent(apiURL, apiURLBackup, '/nestedcomments', None,format)
- if (hnData):
- logging.debug('getHackerNewsComments: storing cached value for id %s' % id)
- DataCache.putData(id, format,APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
- return hnData
- else:
- logging.warning('getHackerNewsComments: unable to retrieve data for id %s' % id)
- return ''
- except:
- logging.warning('getHackerNewsComments: error(s) getting comments %s' % id)
- return ''
+ #only cache homepage data
+ apiURL = "%s/item?id=%s" % (AppConfig.hackerNewsURL, articleId)
+ apiURLBackup = "%s/item?id=%s" % (AppConfig.hackerNewsURLBackup, articleId)
+ id = '/nestedcomments/%s' % (articleId)
+ #cache data
+ cachedData = getCache(id, format)
+ if (cachedData):
+ return cachedData
+ else:
+ try:
+ hnData = APIUtils.parseNestedCommentsContent(apiURL, apiURLBackup, '/nestedcomments', None, format)
+ if (hnData):
+ logging.debug('getHackerNewsComments: storing cached value for id %s' % id)
+ DataCache.putData(id, format, APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
+ return hnData
+ else:
+ logging.warning('getHackerNewsComments: unable to retrieve data for id %s' % id)
+ return ''
+ except:
+ logging.warning('getHackerNewsComments: error(s) getting comments %s' % id)
+ return ''
+
def getHackerNewsSimpleContent(fetcherURL, fetcherBackupURL, id, page='', format='json', url='', referer='', remote_addr=''):
- #don't cache paginated content
- if (page):
- return APIUtils.parsePageContent(fetcherURL, fetcherBackupURL, id, page,format)
- else:
- cachedData = getCache(id,format)
- if (cachedData):
- return cachedData
- else:
- hnData = APIUtils.parsePageContent(fetcherURL, fetcherBackupURL, id, page,format)
- if (hnData):
- logging.debug('getHackerNewsSimpleContent: storing cached value for id %s' % id)
- DataCache.putData(id, format,APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
- return hnData
- else:
- logging.warning('getHackerNewsSimpleContent: unable to retrieve data for id %s' % id)
- return ''
+ #don't cache paginated content
+ if (page):
+ return APIUtils.parsePageContent(fetcherURL, fetcherBackupURL, id, page, format)
+ else:
+ cachedData = getCache(id, format)
+ if (cachedData):
+ return cachedData
+ else:
+ hnData = APIUtils.parsePageContent(fetcherURL, fetcherBackupURL, id, page, format)
+ if (hnData):
+ logging.debug('getHackerNewsSimpleContent: storing cached value for id %s' % id)
+ DataCache.putData(id, format, APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
+ return hnData
+ else:
+ logging.warning('getHackerNewsSimpleContent: unable to retrieve data for id %s' % id)
+ return ''
+
#parse HN's ask content
def getHackerNewsAskContent(page='', format='json', url='', referer='', remote_addr=''):
- return getHackerNewsSimpleContent(AppConfig.hackerNewsAskURL, AppConfig.hackerNewsAskURLBackup, '/ask', page, format, url, referer, remote_addr)
+ return getHackerNewsSimpleContent(AppConfig.hackerNewsAskURL, AppConfig.hackerNewsAskURLBackup, '/ask', page, format, url, referer, remote_addr)
+
#parse HN's best content
def getHackerNewsBestContent(page='', format='json', url='', referer='', remote_addr=''):
- return getHackerNewsSimpleContent(AppConfig.hackerNewsBestURL, AppConfig.hackerNewsBestURLBackup, '/best', page, format, url, referer, remote_addr)
+ return getHackerNewsSimpleContent(AppConfig.hackerNewsBestURL, AppConfig.hackerNewsBestURLBackup, '/best', page, format, url, referer, remote_addr)
+
#parse HN's newest content
def getHackerNewsNewestContent(page='', format='json', url='', referer='', remote_addr=''):
- return getHackerNewsSimpleContent(AppConfig.hackerNewsNewestURL, AppConfig.hackerNewsNewestURLBackup, '/newest', page, format, url, referer, remote_addr)
+ return getHackerNewsSimpleContent(AppConfig.hackerNewsNewestURL, AppConfig.hackerNewsNewestURLBackup, '/newest', page, format, url, referer, remote_addr)
+
#get homepage second page stories
def getHackerNewsSecondPageContent(page='', format='json', url='', referer='', remote_addr=''):
- return getHackerNewsSimpleContent(AppConfig.hackerNewsPage2URL, AppConfig.hackerNewsPage2URLBackup, '/news2', page, format, url, referer, remote_addr)
+ return getHackerNewsSimpleContent(AppConfig.hackerNewsPage2URL, AppConfig.hackerNewsPage2URLBackup, '/news2', page, format, url, referer, remote_addr)
+
#get homepage first page stories
def getHackerNewsPageContent(page='', format='json', url='', referer='', remote_addr=''):
- return getHackerNewsSimpleContent(AppConfig.hackerNewsURL, AppConfig.hackerNewsURLBackup, '/news', page, format, url, referer, remote_addr)
-
+ return getHackerNewsSimpleContent(AppConfig.hackerNewsURL, AppConfig.hackerNewsURLBackup, '/news', page, format, url, referer, remote_addr)
+
+
#get latest homepage stories
def getHackerNewsLatestContent(page='', format='json', url='', referer='', remote_addr='', limit=1):
- #only cache homepage data
- limit = int(limit)
- if (page):
- return APIUtils.parsePageContent(AppConfig.hackerNewsURL, AppConfig.hackerNewsURLBackup, '/latest', page,format,limit)
- else:
- id = '/latest/%s' % limit
- cachedData = getCache(id,format)
- if (cachedData):
- return cachedData
- else:
- hnData = APIUtils.parsePageContent(AppConfig.hackerNewsURL,AppConfig.hackerNewsURLBackup, '/latest', page,format,limit)
- if (hnData):
- logging.debug('getHackerNewsLatestContent: storing cached value for id %s' % id)
- DataCache.putData(id, format,APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
- return hnData
- else:
- logging.warning('getHackerNewsLatestContent: unable to retrieve data for id %s' % id)
- return ''
+ #only cache homepage data
+ limit = int(limit)
+ if (page):
+ return APIUtils.parsePageContent(AppConfig.hackerNewsURL, AppConfig.hackerNewsURLBackup, '/latest', page, format, limit)
+ else:
+ id = '/latest/%s' % limit
+ cachedData = getCache(id, format)
+ if (cachedData):
+ return cachedData
+ else:
+ hnData = APIUtils.parsePageContent(AppConfig.hackerNewsURL, AppConfig.hackerNewsURLBackup, '/latest', page, format, limit)
+ if (hnData):
+ logging.debug('getHackerNewsLatestContent: storing cached value for id %s' % id)
+ DataCache.putData(id, format, APIUtils.removeNonAscii(hnData), url, referer, remote_addr)
+ return hnData
+ else:
+ logging.warning('getHackerNewsLatestContent: unable to retrieve data for id %s' % id)
+ return ''
View
@@ -1,36 +1,46 @@
+
+
#output the error
def error(format, msg):
- if (format == 'json'):
- return '{"status":"error","message":"%s"}' % msg
- else:
- return '<?xml version="1.0"?><root><status>error</status><message>%s</message></root>' % msg
-
-def dataWrapper(format, returnData):
- if (format == 'json'):
- return '{"items":[%s]}' % returnData.lstrip('[').rstrip('],')
- else:
- if (not returnData.startswith('<root>')):
- returnData = '<root>' + returnData
- if (not returnData.endswith('</root>')):
- returnData += '</root>'
- return '<?xml version="1.0"?>%s' % returnData
+ if (format == 'json'):
+ return '{"status":"error","message":"%s"}' % msg
+ else:
+ return '<?xml version="1.0"?><root><status>error</status><message>%s</message></root>' % msg
+
+
+def dataWrapper(format, returnData, callback):
+ if (format == 'json'):
+ returnData = '{"items":[%s]}' % returnData.lstrip('[').rstrip('],')
+ if (callback):
+ return '%s(%s);' % (callback, returnData)
+ else:
+ return returnData
+ else:
+ if (not returnData.startswith('<root>')):
+ returnData = '<root>' + returnData
+ if (not returnData.endswith('</root>')):
+ returnData += '</root>'
+ return '<?xml version="1.0"?>%s' % returnData
+
def contentType(format):
- if (format == 'json'):
- return 'application/json; charset=utf-8'
- else:
- return 'application/xml; charset=utf-8'
+ if (format == 'json'):
+ return 'application/json; charset=utf-8'
+ else:
+ return 'application/xml; charset=utf-8'
+
#output simple string in json|xml format
def data(format, elm, msg):
- if (format == 'json'):
- return '"%s":"%s",' % (elm, msg)
- else:
- return '<%s>%s</%s>' % (elm, msg, elm)
-
+ if (format == 'json'):
+ return '"%s":"%s",' % (elm, msg)
+ else:
+ return '<%s>%s</%s>' % (elm, msg, elm)
+
+
#output complex types in json|xml format
def dataComplex(format, elm, msg):
- if (format == 'json'):
- return '{"%s":[%s]},' % (elm, msg)
- else:
- return '<%s>%s</%s>' % (elm, msg, elm)
+ if (format == 'json'):
+ return '{"%s":[%s]},' % (elm, msg)
+ else:
+ return '<%s>%s</%s>' % (elm, msg, elm)
Oops, something went wrong. Retry.

0 comments on commit de273cb

Please sign in to comment.