def remote(request, url, encoding=None, markup=None, highlight=None, strip_html=True, **kwargs): """ include a remote file into a page. Arguments, see DocString of lucidTag() """ # Get preferences from DB and overwrite them pref_form = PreferencesForm() preferences = pref_form.get_preferences(request, lucidtag_kwargs=kwargs) cache_key = "include_remote_%s" % url context = cache.get(cache_key) if context: from_cache = True else: from_cache = False # Get the current page url, for referer context = request.PYLUCID.context page_absolute_url = context["page_absolute_url"] current_language = request.PYLUCID.current_language current_language_code = current_language.code socket_timeout = preferences["socket_timeout"] user_agent = preferences["user_agent"] start_time = time.time() try: # Request the url content in unicode r = HttpRequest(url, timeout=socket_timeout, threadunsafe_workaround=True) r.request.add_header("User-agent", user_agent) r.request.add_header("Referer", page_absolute_url) r.request.add_header("Accept-Language", current_language_code) response = r.get_response() raw_content = r.get_unicode() except Exception, err: return _error(request, "Include error.", "Can't get %r: %s" % (url, err)) duration = time.time() - start_time # get request/response information request_header = response.request_header response_info = response.info() context = { "raw_content": raw_content, "request_header": request_header, "response_info": response_info, "duration": duration, } cache.set(cache_key, context , preferences["cache_timeout"])
def get_json_remote(request, url, cache_time, timeout, encoding=None): """ include a remote file into a page. Arguments, see DocString of lucidTag() """ cache_key = "get_json_remote_%s" % url context = cache.get(cache_key) if context: from_cache = True else: from_cache = False # Get the current page url, for referer context = request.PYLUCID.context page_absolute_url = context["page_absolute_url"] current_language = request.PYLUCID.current_language current_language_code = current_language.code user_agent = "PyLucid CMS v%s" % VERSION_STRING start_time = time.time() try: # Request the url content in unicode r = HttpRequest(url, timeout=timeout, threadunsafe_workaround=True) r.request.add_header("User-agent", user_agent) r.request.add_header("Referer", page_absolute_url) r.request.add_header("Accept-Language", current_language_code) response = r.get_response() raw_content = r.get_unicode() except Exception, err: return _error(request, "Remote error.", "Can't get %r: %s" % (url, err)) duration = time.time() - start_time # get request/response information request_header = response.request_header response_info = response.info() raw_json = raw_content[len("jsonFlickrFeed("):-1] try: data = json.loads(raw_json) except Exception, err: _error(request, "Remote error.", "Can't load json %r: %s" % (url, err)) data = None