def stamp_availability_on_video(video, format="mp4", force=False, stamp_urls=True, videos_path=None): """ Stamp all relevant urls and availability onto a video object (if necessary), including: * whether the video is available (on disk or online) """ videos_path = videos_path or settings.CONTENT_ROOT def compute_video_availability(youtube_id, format, videos_path=videos_path): return {"on_disk": is_video_on_disk(youtube_id, format, videos_path=videos_path)} def compute_video_metadata(youtube_id, format): return {"stream_type": "video/%s" % format} def compute_video_urls(youtube_id, format, lang_code, on_disk=None, thumb_formats=["png", "jpg"], videos_path=videos_path): if on_disk is None: on_disk = is_video_on_disk(youtube_id, format, videos_path=videos_path) if on_disk: video_base_url = settings.CONTENT_URL + youtube_id stream_url = video_base_url + ".%s" % format thumbnail_url = None # default to None now, so we know when no thumbnail is available. for thumb_format in thumb_formats: # find the thumbnail on disk thumb_filename = '%s.%s' % (youtube_id, thumb_format) thumb_filepath = os.path.join(videos_path, thumb_filename) if os.path.exists(thumb_filepath): thumbnail_url = video_base_url + "." + thumb_format # default break elif settings.BACKUP_VIDEO_SOURCE and lang_code == "en": dict_vals = {"youtube_id": youtube_id, "video_format": format, "thumb_format": thumb_formats[0] } stream_url = settings.BACKUP_VIDEO_SOURCE % dict_vals thumbnail_url = settings.BACKUP_THUMBNAIL_SOURCE % dict_vals if settings.BACKUP_THUMBNAIL_SOURCE else None else: return {} # no URLs return {"stream": stream_url, "thumbnail": thumbnail_url} video_availability = video.get("availability", {}) if not force else {} en_youtube_id = get_youtube_id(video["id"], lang_code=None) # get base ID video_map = get_id2oklang_map(video["id"]) or {} if not "on_disk" in video_availability: for lang_code in video_map.keys(): youtube_id = video_map[lang_code].encode('utf-8') video_availability[lang_code] = compute_video_availability(youtube_id, format=format, videos_path=videos_path) video_availability["en"] = video_availability.get("en", {"on_disk": False}) # en should always be defined # Summarize status any_on_disk = any([lang_avail["on_disk"] for lang_avail in video_availability.values()]) any_available = any_on_disk or bool(settings.BACKUP_VIDEO_SOURCE) if stamp_urls: # Loop over all known dubbed videos for lang_code, youtube_id in video_map.iteritems(): urls = compute_video_urls(youtube_id, format, lang_code, on_disk=video_availability[lang_code]["on_disk"], videos_path=videos_path) if urls: # Only add properties if anything is available. video_availability[lang_code].update(urls) video_availability[lang_code].update(compute_video_metadata(youtube_id, format)) # Get the (english) subtitle urls subtitle_lang_codes = get_langs_with_subtitle(en_youtube_id) subtitles_tuple = [(lc, get_srt_url(en_youtube_id, lc)) for lc in subtitle_lang_codes if os.path.exists(get_srt_path(lc, en_youtube_id))] subtitles_urls = dict(subtitles_tuple) video_availability["en"]["subtitles"] = subtitles_urls # now scrub any values that don't actually exist for lang_code in video_availability.keys(): if not video_availability[lang_code]["on_disk"] and len(video_availability[lang_code]) == 1: del video_availability[lang_code] # Now summarize some availability onto the video itself video["availability"] = video_availability video["on_disk"] = any_on_disk video["available"] = any_available return video
def get_content_cache(force=False, annotate=False, language=None): if not language: language = django_settings.LANGUAGE_CODE global CONTENT if CONTENT is None: CONTENT = {} if CONTENT.get(language) is None: content = None if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force: content = softload_sqlite_cache(settings.CONTENT_CACHE_FILEPATH) if content: CONTENT[language] = content return CONTENT[language] else: if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP: call_command("create_content_db") content = softload_sqlite_cache(settings.CONTENT_CACHE_FILEPATH) else: content = softload_json(settings.CONTENT_FILEPATH, logger=logging.debug, raises=False) CONTENT[language] = content annotate = True if annotate: # Loop through all content items and put thumbnail urls, content urls, # and subtitle urls on the content dictionary, and list all languages # that the content is available in. try: contents_folder = os.listdir(django_settings.CONTENT_ROOT) except OSError: contents_folder = [] subtitle_langs = {} if os.path.exists(i18n.get_srt_path()): for (dirpath, dirnames, filenames) in os.walk(i18n.get_srt_path()): # Only both looking at files that are inside a 'subtitles' directory if os.path.basename(dirpath) == "subtitles": lc = os.path.basename(os.path.dirname(dirpath)) for filename in filenames: if filename in subtitle_langs: subtitle_langs[filename].append(lc) else: subtitle_langs[filename] = [lc] for key, content in CONTENT[language].iteritems(): default_thumbnail = create_thumbnail_url(content.get("id")) dubmap = i18n.get_id2oklang_map(content.get("id")) if dubmap: content_lang = i18n.select_best_available_language(language, available_codes=dubmap.keys()) or "" if content_lang: dubbed_id = dubmap.get(content_lang) format = content.get("format", "") if (dubbed_id + "." + format) in contents_folder: content["available"] = True thumbnail = create_thumbnail_url(dubbed_id) or default_thumbnail content["content_urls"] = { "stream": django_settings.CONTENT_URL + dubmap.get(content_lang) + "." + format, "stream_type": "{kind}/{format}".format(kind=content.get("kind", "").lower(), format=format), "thumbnail": thumbnail, } elif django_settings.BACKUP_VIDEO_SOURCE: content["available"] = True content["content_urls"] = { "stream": django_settings.BACKUP_VIDEO_SOURCE.format(youtube_id=dubbed_id, video_format=format), "stream_type": "{kind}/{format}".format(kind=content.get("kind", "").lower(), format=format), "thumbnail": django_settings.BACKUP_VIDEO_SOURCE.format(youtube_id=dubbed_id, video_format="png"), } else: content["available"] = False else: content["available"] = False else: content["available"] = False # Get list of subtitle language codes currently available subtitle_lang_codes = subtitle_langs.get("{id}.srt".format(id=content.get("id")), []) # Generate subtitle URLs for any subtitles that do exist for this content item subtitle_urls = [{ "code": lc, "url": django_settings.STATIC_URL + "srt/{code}/subtitles/{id}.srt".format(code=lc, id=content.get("id")), "name": i18n.get_language_name(lc) } for lc in subtitle_lang_codes] # Sort all subtitle URLs by language code content["subtitle_urls"] = sorted(subtitle_urls, key=lambda x: x.get("code", "")) with i18n.translate_block(language): content["selected_language"] = content_lang content["title"] = _(content["title"]) content["description"] = _(content.get("description")) if content.get("description") else "" CONTENT[language][key] = content if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP: try: CONTENT[language].commit() except IOError as e: logging.warn("Annotated content cache file failed in saving with error {e}".format(e=e)) return CONTENT[language]
def get_content_cache(force=False, annotate=False, language=settings.LANGUAGE_CODE): global CONTENT, CONTENT_FILEPATH if CONTENT is None: CONTENT = {} if CONTENT.get(language) is None: CONTENT[language] = softload_json(CONTENT_FILEPATH, logger=logging.debug, raises=False) annotate = True if annotate: if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force: content = softload_json(CONTENT_FILEPATH + "_" + language + ".cache", logger=logging.debug, raises=False) if content: CONTENT[language] = content return CONTENT[language] # Loop through all content items and put thumbnail urls, content urls, # and subtitle urls on the content dictionary, and list all languages # that the content is available in. for content in CONTENT[language].values(): default_thumbnail = create_thumbnail_url(content.get("id")) dubmap = i18n.get_id2oklang_map(content.get("id")) if dubmap: content_lang = i18n.select_best_available_language(language, available_codes=dubmap.keys()) or "" if content_lang: dubbed_id = dubmap.get(content_lang) format = content.get("format", "") if is_content_on_disk(dubbed_id, format): content["available"] = True thumbnail = create_thumbnail_url(dubbed_id) or default_thumbnail content["content_urls"] = { "stream": settings.CONTENT_URL + dubmap.get(content_lang) + "." + format, "stream_type": "{kind}/{format}".format(kind=content.get("kind", "").lower(), format=format), "thumbnail": thumbnail, } else: content["available"] = False else: content["available"] = False else: content["available"] = False # Get list of subtitle language codes currently available subtitle_lang_codes = [] if not os.path.exists(i18n.get_srt_path()) else [lc for lc in os.listdir(i18n.get_srt_path()) if os.path.exists(i18n.get_srt_path(lc, content.get("id")))] # Generate subtitle URLs for any subtitles that do exist for this content item subtitle_urls = [{ "code": lc, "url": settings.STATIC_URL + "srt/{code}/subtitles/{id}.srt".format(code=lc, id=content.get("id")), "name": i18n.get_language_name(lc) } for lc in subtitle_lang_codes if os.path.exists(i18n.get_srt_path(lc, content.get("id")))] # Sort all subtitle URLs by language code content["subtitle_urls"] = sorted(subtitle_urls, key=lambda x: x.get("code", "")) with i18n.translate_block(content_lang): content["selected_language"] = content_lang content["title"] = _(content["title"]) content["description"] = _(content.get("description", "")) if content.get("description") else "" if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP: try: with open(CONTENT_FILEPATH + "_" + language + ".cache", "w") as f: json.dump(CONTENT[language], f) except IOError as e: logging.warn("Annotated content cache file failed in saving with error {e}".format(e=e)) return CONTENT[language]
def get_content_cache(force=False, annotate=False, language=settings.LANGUAGE_CODE): global CONTENT, CONTENT_FILEPATH if CONTENT is None: CONTENT = {} if CONTENT.get(language) is None: CONTENT[language] = softload_json(CONTENT_FILEPATH, logger=logging.debug, raises=False) annotate = True if annotate: if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force: content = softload_json(CONTENT_FILEPATH + "_" + language + ".cache", logger=logging.debug, raises=False) if content: CONTENT[language] = content return CONTENT[language] # Loop through all content items and put thumbnail urls, content urls, # and subtitle urls on the content dictionary, and list all languages # that the content is available in. for content in CONTENT[language].values(): default_thumbnail = create_thumbnail_url(content.get("id")) dubmap = i18n.get_id2oklang_map(content.get("id")) if dubmap: content_lang = i18n.select_best_available_language(language, available_codes=dubmap.keys()) or "" if content_lang: dubbed_id = dubmap.get(content_lang) format = content.get("format", "") if is_content_on_disk(dubbed_id, format): content["available"] = True thumbnail = create_thumbnail_url(dubbed_id) or default_thumbnail content["content_urls"] = { "stream": settings.CONTENT_URL + dubmap.get(content_lang) + "." + format, "stream_type": "{kind}/{format}".format(kind=content.get("kind", "").lower(), format=format), "thumbnail": thumbnail, } elif settings.BACKUP_VIDEO_SOURCE: content["available"] = True content["content_urls"] = { "stream": settings.BACKUP_VIDEO_SOURCE.format(youtube_id=dubbed_id, video_format=format), "stream_type": "{kind}/{format}".format(kind=content.get("kind", "").lower(), format=format), "thumbnail": settings.BACKUP_VIDEO_SOURCE.format(youtube_id=dubbed_id, video_format="png"), } else: content["available"] = False else: content["available"] = False else: content["available"] = False # Get list of subtitle language codes currently available subtitle_lang_codes = [] if not os.path.exists(i18n.get_srt_path()) else [lc for lc in os.listdir(i18n.get_srt_path()) if os.path.exists(i18n.get_srt_path(lc, content.get("id")))] # Generate subtitle URLs for any subtitles that do exist for this content item subtitle_urls = [{ "code": lc, "url": settings.STATIC_URL + "srt/{code}/subtitles/{id}.srt".format(code=lc, id=content.get("id")), "name": i18n.get_language_name(lc) } for lc in subtitle_lang_codes if os.path.exists(i18n.get_srt_path(lc, content.get("id")))] # Sort all subtitle URLs by language code content["subtitle_urls"] = sorted(subtitle_urls, key=lambda x: x.get("code", "")) with i18n.translate_block(content_lang): content["selected_language"] = content_lang content["title"] = _(content["title"]) content["description"] = _(content.get("description", "")) if content.get("description") else "" if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP: try: with open(CONTENT_FILEPATH + "_" + language + ".cache", "w") as f: json.dump(CONTENT[language], f) except IOError as e: logging.warn("Annotated content cache file failed in saving with error {e}".format(e=e)) return CONTENT[language]
def get_content_cache(force=False, annotate=False): global CONTENT, CONTENT_FILEPATH if CONTENT is None: CONTENT = softload_json(CONTENT_FILEPATH, logger=logging.debug, raises=False) annotate = True if annotate: if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP and not force: content = softload_json(CONTENT_FILEPATH + ".cache", logger=logging.debug, raises=False) if content: CONTENT = content return CONTENT # Loop through all content items and put thumbnail urls, content urls, # and subtitle urls on the content dictionary, and list all languages # that the content is available in. for content in CONTENT.values(): languages = [] default_thumbnail = create_thumbnail_url(content.get("id")) dubmap = i18n.get_id2oklang_map(content.get("id")) for lang, dubbed_id in dubmap.items(): format = content.get("format", "") if is_content_on_disk(dubbed_id, format): languages.append(lang) thumbnail = create_thumbnail_url( dubbed_id) or default_thumbnail content["lang_data_" + lang] = { "stream": settings.CONTENT_URL + dubmap.get(lang) + "." + format, "stream_type": "{kind}/{format}".format(kind=content.get("kind", "").lower(), format=format), "thumbnail": thumbnail, } content["languages"] = languages # Get list of subtitle language codes currently available subtitle_lang_codes = [] if not os.path.exists( i18n.get_srt_path()) else [ lc for lc in os.listdir(i18n.get_srt_path()) if os.path.exists(i18n.get_srt_path(lc, content.get("id"))) ] # Generate subtitle URLs for any subtitles that do exist for this content item subtitle_urls = [ { "code": lc, "url": settings.STATIC_URL + "srt/{code}/subtitles/{id}.srt".format( code=lc, id=content.get("id")), "name": i18n.get_language_name(lc) } for lc in subtitle_lang_codes if os.path.exists(i18n.get_srt_path(lc, content.get("id"))) ] # Sort all subtitle URLs by language code content["subtitle_urls"] = sorted(subtitle_urls, key=lambda x: x.get("code", "")) if settings.DO_NOT_RELOAD_CONTENT_CACHE_AT_STARTUP: try: with open(CONTENT_FILEPATH + ".cache", "w") as f: json.dump(CONTENT, f) except IOError as e: logging.warn( "Annotated content cache file failed in saving with error {e}" .format(e=e)) return CONTENT