@layer_cache.cache(layer=layer_cache.Layers.Memcache | layer_cache.Layers.Datastore, expiration=86400) def getSmartHistoryContent(): request = urllib2.Request("http://smarthistory.org/khan-home.html") try: response = urllib2.urlopen(request) smart_history = response.read() smart_history = re.search(re.compile("<body>(.*)</body>", re.S), smart_history).group(1).decode("utf-8") smart_history.replace("script", "") except Exception, e: logging.exception("Failed fetching smarthistory playlist") smart_history = None pass return smart_history @layer_cache.cache_with_key_fxn( lambda *args, **kwargs: "library_content_html_%s" % Setting.cached_library_content_date() ) def library_content_html(): # No cache found -- regenerate HTML smart_history = getSmartHistoryContent() all_playlists = [] dict_videos = {} dict_videos_counted = {} dict_playlists = {} dict_playlists_by_title = {} dict_video_playlists = {} async_queries = [ Video.all(),
def library_content_html(): # No cache found -- regenerate HTML smart_history = getSmartHistoryContent() all_playlists = [] dict_videos = {} dict_videos_counted = {} dict_playlists = {} dict_playlists_by_title = {} dict_video_playlists = {} async_queries = [ Video.all(), Playlist.all(), VideoPlaylist.all().filter('live_association = ', True).order('video_position'), ] results = util.async_queries(async_queries) for video in results[0].get_result(): dict_videos[video.key()] = video for playlist in results[1].get_result(): dict_playlists[playlist.key()] = playlist if playlist.title in topics_list: dict_playlists_by_title[playlist.title] = playlist for video_playlist in results[2].get_result(): playlist_key = VideoPlaylist.playlist.get_value_for_datastore(video_playlist) video_key = VideoPlaylist.video.get_value_for_datastore(video_playlist) if dict_videos.has_key(video_key) and dict_playlists.has_key(playlist_key): video = dict_videos[video_key] playlist = dict_playlists[playlist_key] fast_video_playlist_dict = {"video":video, "playlist":playlist} if dict_video_playlists.has_key(playlist_key): dict_video_playlists[playlist_key].append(fast_video_playlist_dict) else: dict_video_playlists[playlist_key] = [fast_video_playlist_dict] if dict_playlists_by_title.has_key(playlist.title): # Only count videos in topics_list dict_videos_counted[video.youtube_id] = True # Update count of all distinct videos associated w/ a live playlist Setting.count_videos(len(dict_videos_counted.keys())) for topic in topics_list: if topic in dict_playlists_by_title: playlist = dict_playlists_by_title[topic] playlist_key = playlist.key() playlist_videos = dict_video_playlists.get(playlist_key) or [] if not playlist_videos: logging.error('Playlist %s has no videos!', playlist.title) playlist_data = { 'title': topic, 'topic': topic, 'playlist': playlist, 'videos': playlist_videos, 'next': None } all_playlists.append(playlist_data) playlist_data_prev = None for playlist_data in all_playlists: if playlist_data_prev: playlist_data_prev['next'] = playlist_data playlist_data_prev = playlist_data # Separating out the columns because the formatting is a little different on each column template_values = { 'App' : App, 'all_playlists': all_playlists, 'smart_history': smart_history, } html = shared_jinja.get().render_template("library_content_template.html", **template_values) # Set shared date of last generated content Setting.cached_library_content_date(str(datetime.datetime.now())) return html
def get(self): self.response.out.write( json.dumps(Setting.cached_library_content_date(), indent=4))
user_data = UserData.get_for_current_user() logout_url = users.create_logout_url(self.request.uri) template_values = qa.add_template_values({'App': App, 'points': user_data.points, 'username': user and user.nickname() or "", 'login_url': util.create_login_url(self.request.uri), 'student_email' : self.request.get('student_email'), 'logout_url': logout_url}, self.request) path = os.path.join(os.path.dirname(__file__), 'import.html') self.response.out.write(template.render(path, template_values)) @layer_cache.cache_with_key("playlists_%s" % Setting.cached_library_content_date()) def get_playlists(): playlists = [] for playlist_title in all_topics_list: query = Playlist.all() query.filter('title =', playlist_title) playlist = query.get() playlist_dict = {'youtube_id': playlist.youtube_id, 'youtube_url': playlist.url, 'title': playlist.title, 'description': playlist.description, 'api_url': "http://www.khanacademy.org/api/playlistvideos?playlist=%s" % (urllib.quote_plus(playlist_title)), } playlists.append(playlist_dict) return json.dumps(playlists, indent=4)
def video_library_last_updated(): return Setting.cached_library_content_date()
playlist.youtube_id, 'youtube_url': playlist.url, 'title': playlist.title, 'description': playlist.description, 'videos': [], 'api_url': "http://www.khanacademy.org/api/playlistvideos?playlist=%s" % (urllib.quote_plus(playlist.title)), } @layer_cache.cache_with_key("json_playlists_%s" % Setting.cached_library_content_date()) def get_playlists_json(): return json.dumps(get_playlist_api_dicts(), indent=4) @layer_cache.cache_with_key_fxn( lambda playlist_title: "json_playlistvideos_%s_%s" % (playlist_title, Setting.cached_library_content_date())) def get_playlist_videos_json(playlist_title): query = Playlist.all() query.filter('title =', playlist_title) playlist = query.get() video_query = Video.all() video_query.filter('playlists = ', playlist_title) video_key_dict = Video.get_dict(video_query, lambda video: video.key())
@route("/api/videolibrary", methods=["GET"]) @jsonp @jsonify def video_library(): return zlib.decompress(get_video_library_json_compressed()) @route("/api/videolibrarylastupdated", methods=["GET"]) @jsonp @jsonify def video_library_last_updated(): return Setting.cached_library_content_date() @layer_cache.cache_with_key_fxn( lambda: "json_playlists_%s" % Setting.cached_library_content_date(), layer=layer_cache.Layers.Memcache) def get_playlists_json(): return json.dumps(get_playlist_api_dicts(), indent=4) @layer_cache.cache_with_key_fxn( lambda playlist_title: "json_playlistvideos_%s_%s" % (playlist_title, Setting.cached_library_content_date()), layer=layer_cache.Layers.Memcache) def get_playlist_videos_json(playlist_title): query = Playlist.all() query.filter('title =', playlist_title) playlist = query.get() video_query = Video.all()
def library_content_html(): # No cache found -- regenerate HTML smart_history = getSmartHistoryContent() all_playlists = [] dict_videos = {} dict_videos_counted = {} dict_playlists = {} dict_playlists_by_title = {} dict_video_playlists = {} async_queries = [ Video.all(), Playlist.all(), VideoPlaylist.all().filter('live_association = ', True).order('video_position'), ] results = util.async_queries(async_queries) for video in results[0].get_result(): dict_videos[video.key()] = video for playlist in results[1].get_result(): dict_playlists[playlist.key()] = playlist if playlist.title in topics_list: dict_playlists_by_title[playlist.title] = playlist for video_playlist in results[2].get_result(): playlist_key = VideoPlaylist.playlist.get_value_for_datastore( video_playlist) video_key = VideoPlaylist.video.get_value_for_datastore(video_playlist) if dict_videos.has_key(video_key) and dict_playlists.has_key( playlist_key): video = dict_videos[video_key] playlist = dict_playlists[playlist_key] fast_video_playlist_dict = {"video": video, "playlist": playlist} if dict_video_playlists.has_key(playlist_key): dict_video_playlists[playlist_key].append( fast_video_playlist_dict) else: dict_video_playlists[playlist_key] = [fast_video_playlist_dict] if dict_playlists_by_title.has_key(playlist.title): # Only count videos in topics_list dict_videos_counted[video.youtube_id] = True # Update count of all distinct videos associated w/ a live playlist Setting.count_videos(len(dict_videos_counted.keys())) for topic in topics_list: if topic in dict_playlists_by_title: playlist = dict_playlists_by_title[topic] playlist_key = playlist.key() playlist_videos = dict_video_playlists.get(playlist_key) or [] if not playlist_videos: logging.error('Playlist %s has no videos!', playlist.title) playlist_data = { 'title': topic, 'topic': topic, 'playlist': playlist, 'videos': playlist_videos, 'next': None } all_playlists.append(playlist_data) playlist_data_prev = None for playlist_data in all_playlists: if playlist_data_prev: playlist_data_prev['next'] = playlist_data playlist_data_prev = playlist_data # Separating out the columns because the formatting is a little different on each column template_values = { 'App': App, 'all_playlists': all_playlists, 'smart_history': smart_history, } html = shared_jinja.get().render_template("library_content_template.html", **template_values) # Set shared date of last generated content Setting.cached_library_content_date(str(datetime.datetime.now())) return html
def get(self): self.response.out.write(json.dumps(Setting.cached_library_content_date(), indent=4))
return get_playlist_videos_json(playlist_title) @route("/api/videolibrary", methods=["GET"]) @jsonp @jsonify def video_library(): return zlib.decompress(get_video_library_json_compressed()) @route("/api/videolibrarylastupdated", methods=["GET"]) @jsonp @jsonify def video_library_last_updated(): return Setting.cached_library_content_date() @layer_cache.cache_with_key_fxn( lambda: "json_playlists_%s" % Setting.cached_library_content_date(), layer=layer_cache.Layers.Memcache) def get_playlists_json(): return json.dumps(get_playlist_api_dicts(), indent=4) @layer_cache.cache_with_key_fxn( lambda playlist_title: "json_playlistvideos_%s_%s" % (playlist_title, Setting.cached_library_content_date()), layer=layer_cache.Layers.Memcache) def get_playlist_videos_json(playlist_title): query = Playlist.all() query.filter('title =', playlist_title) playlist = query.get() video_query = Video.all() video_query.filter('playlists = ', playlist_title) video_key_dict = Video.get_dict(video_query, lambda video: video.key())