def download_srt_from_3rd_party(*args, **kwargs): """Download subtitles specified by command line args""" lang_code = kwargs.get("lang_code", None) # if language specified, do those, if not do all if lang_code: srt_list_path = get_lang_map_filepath(lang_code) try: videos = json.loads(open(srt_list_path).read()) except: raise LanguageCodeDoesNotExist(lang_code) download_if_criteria_met(videos, *args, **kwargs) else: for filename in get_all_download_status_files(): try: videos = json.loads(open(filename).read()) except Exception as e: logging.error(e) raise CommandError("Unable to open %s. The file might be corrupted. Please re-run the generate_subtitle_map command to regenerate it." % filename) try: kwargs["lang_code"] = os.path.basename(filename).split("_")[0] download_if_criteria_met(videos, *args, **kwargs) except Exception as e: logging.error(e) raise CommandError("Error while downloading language srts: %s" % e)
def update_json(youtube_id, lang_code, downloaded, api_response, time_of_attempt): """Update language_srt_map to reflect download status""" # Open JSON file filepath = get_lang_map_filepath(lang_code) try: language_srt_map = json.loads(open(filepath).read()) except Exception as e: logging.error("Something went wrong while trying to open the json file (%s): %s" % (filepath, e)) return False # create updated entry entry = language_srt_map[youtube_id] entry["downloaded"] = downloaded entry["api_response"] = api_response entry["last_attempt"] = time_of_attempt if api_response == "success": entry["last_success"] = time_of_attempt # update full-size JSON with new information language_srt_map[youtube_id].update(entry) # write it to file logging.info("File updated.") json_file = open(filepath, "wb") json_file.write(json.dumps(language_srt_map)) json_file.close() return True
def update_json(youtube_id, lang_code, downloaded, api_response, time_of_attempt): """Update language_srt_map to reflect download status""" # Open JSON file filepath = get_lang_map_filepath(lang_code) try: language_srt_map = json.loads(open(filepath).read()) except Exception as e: logging.error( "Something went wrong while trying to open the json file (%s): %s" % (filepath, e)) return False # create updated entry entry = language_srt_map[youtube_id] entry["downloaded"] = downloaded entry["api_response"] = api_response entry["last_attempt"] = time_of_attempt if api_response == "success": entry["last_success"] = time_of_attempt # update full-size JSON with new information language_srt_map[youtube_id].update(entry) # write it to file logging.info("File updated.") json_file = open(filepath, "wb") json_file.write(json.dumps(language_srt_map)) json_file.close() return True
def download_srt_from_3rd_party(*args, **kwargs): """Download subtitles specified by command line args""" lang_code = kwargs.get("lang_code", None) # if language specified, do those, if not do all if lang_code: srt_list_path = get_lang_map_filepath(lang_code) try: videos = json.loads(open(srt_list_path).read()) except: raise LanguageCodeDoesNotExist(lang_code) download_if_criteria_met(videos, *args, **kwargs) else: base_path = settings.SUBTITLES_DATA_ROOT + "languages/" for filename in get_all_download_status_files(): try: videos = json.loads(open(base_path + filename).read()) except: raise CommandError( "Unable to open %s. The file might be corrupted. Please re-run the generate_subtitle_map command to regenerate it." % filename) kwargs["lang_code"] = filename.split("_")[0] download_if_criteria_met(videos, *args, **kwargs)
def download_srt_from_3rd_party(*args, **kwargs): """Download subtitles specified by command line args""" lang_code = kwargs.get("lang_code", None) # if language specified, do those, if not do all if lang_code: srt_list_path = get_lang_map_filepath(lang_code) try: videos = json.loads(open(srt_list_path).read()) except: raise LanguageCodeDoesNotExist(lang_code) download_if_criteria_met(videos, *args, **kwargs) else: for filename in get_all_download_status_files(): try: videos = json.loads(open(filename).read()) except Exception as e: logging.error(e) raise CommandError( "Unable to open %s. The file might be corrupted. Please re-run the generate_subtitle_map command to regenerate it." % filename) try: kwargs["lang_code"] = os.path.basename(filename).split("_")[0] download_if_criteria_met(videos, *args, **kwargs) except Exception as e: logging.error(e) raise CommandError( "Error while downloading language srts: %s" % e)
def clear_subtitles_cache(language_codes=None, download_path=DOWNLOAD_PATH): language_codes = language_codes or os.listdir(download_path) for language_code in language_codes: # Clear the status file lm_file = get_lang_map_filepath(language_code) with open(lm_file, "r") as fp: download_status = json.load(fp) for key in download_status: download_status[key] = {u'downloaded': False, u'last_success': u'', u'last_attempt': u'', u'api_response': u''} with open(lm_file, "w") as fp: json.dump(download_status, fp) # Delete all srt files srt_path = get_srt_path(language_code) if os.path.exists(srt_path): shutil.rmtree(srt_path)
def download_srt_from_3rd_party(*args, **kwargs): """Download subtitles specified by command line args""" lang_code = kwargs.get("lang_code", None) # if language specified, do those, if not do all if not lang_code: raise CommandError("You must specify a language code or 'all' with -l") elif lang_code == "all": bad_languages = {} for filename in get_all_download_status_files(): try: videos = json.loads(open(filename).read()) except Exception as e: logging.error(e) raise CommandError( "Unable to open %s. The file might be corrupted. Please re-run the generate_subtitle_map command to regenerate it." % filename) try: lang_code = os.path.basename(filename).split("_")[0] kwargs["lang_code"] = lang_code download_if_criteria_met(videos, *args, **kwargs) except Exception as e: logging.error("Error downloading subtitles for %s: %s" % (lang_code, e)) bad_languages[lang_code] = e continue # now report final results if bad_languages: raise CommandError( "Failed to download subtitles for the following languages: %s" % bad_languages.keys()) else: srt_list_path = get_lang_map_filepath( convert_language_code_format(lang_code)) try: videos = json.loads(open(srt_list_path).read()) except: logging.warning( "No subtitles available for download for language code %s. Skipping." % lang_code) else: download_if_criteria_met(videos, *args, **kwargs)
def clear_subtitles_cache(language_codes=None, locale_root=LOCALE_ROOT): language_codes = language_codes or os.listdir(locale_root) for language_code in language_codes: # Clear the status file lm_file = get_lang_map_filepath(language_code) with open(lm_file, "r") as fp: download_status = json.load(fp) for key in download_status: download_status[key] = { u'downloaded': False, u'last_success': u'', u'last_attempt': u'', u'api_response': u'' } with open(lm_file, "w") as fp: json.dump(download_status, fp) # Delete all srt files srt_path = get_srt_path(language_code) if os.path.exists(srt_path): shutil.rmtree(srt_path)
def download_srt_from_3rd_party(*args, **kwargs): """Download subtitles specified by command line args""" lang_code = kwargs.get("lang_code", None) # if language specified, do those, if not do all if not lang_code: raise CommandError("You must specify a language code or 'all' with -l") elif lang_code == "all": bad_languages = {} for filename in get_all_download_status_files(): try: videos = json.loads(open(filename).read()) except Exception as e: logging.error(e) raise CommandError("Unable to open %s. The file might be corrupted. Please re-run the generate_subtitle_map command to regenerate it." % filename) try: lang_code = os.path.basename(filename).split("_")[0] kwargs["lang_code"] = lang_code download_if_criteria_met(videos, *args, **kwargs) except Exception as e: logging.error("Error downloading subtitles for %s: %s" % (lang_code, e)) bad_languages[lang_code] = e continue # now report final results if bad_languages: raise CommandError("Failed to download subtitles for the following languages: %s" % bad_languages.keys()) else: srt_list_path = get_lang_map_filepath(convert_language_code_format(lang_code)) try: videos = json.loads(open(srt_list_path).read()) except: logging.warning("No subtitles available for download for language code %s. Skipping." % lang_code) else: download_if_criteria_met(videos, *args, **kwargs)
def download_srt_from_3rd_party(*args, **kwargs): """Download subtitles specified by command line args""" lang_code = kwargs.get("lang_code", None) # if language specified, do those, if not do all if lang_code: srt_list_path = get_lang_map_filepath(lang_code) try: videos = json.loads(open(srt_list_path).read()) except: raise LanguageCodeDoesNotExist(lang_code) download_if_criteria_met(videos, *args, **kwargs) else: base_path = settings.SUBTITLES_DATA_ROOT + "languages/" for filename in get_all_download_status_files(): try: videos = json.loads(open(base_path + filename).read()) except: raise CommandError("Unable to open %s. The file might be corrupted. Please re-run the generate_subtitle_map command to regenerate it." % filename) kwargs["lang_code"] = filename.split("_")[0] download_if_criteria_met(videos, *args, **kwargs)