@@ -907,25 +907,52 @@ def load_commented_json(path: Path) -> Dict:
907907 return data
908908
909909
910- def interpolate_ytdlp_config (data : dict [str , Any ]) -> None :
910+ def interpolate_ytdlp_config (data : dict [str , Any ], po_token : str ) -> None :
911911 """
912912 Interpolate the ytdlp config file with values from the environment variables.
913913 """
914914 # currently this is only used to replace the default PO token value
915915 if not (extractor_args := data .get ("extractor-args" )):
916916 return
917- if "<PO_TOKEN_VALUE>" not in extractor_args :
918- return
917+ if type (extractor_args ) is str :
918+ if "<PO_TOKEN_VALUE>" not in extractor_args :
919+ return
920+ data ["extractor-args" ] = extractor_args .replace (
921+ "<PO_TOKEN_VALUE>" , po_token
922+ )
923+ # Looks like passing a list to ytdlp works as if passing multiple --extractor-args flags
924+ elif type (extractor_args ) is list :
925+ for i , val in enumerate (extractor_args ):
926+ if type (val ) is str and "<PO_TOKEN_VALUE>" in val :
927+ extractor_args [i ] = val .replace ("<PO_TOKEN_VALUE>" , po_token )
928+ else :
929+ log .warning (
930+ "extractor-args in ytdlp config is neither a string, list or dict."
931+ "Cannot interpolate PO token."
932+ )
933+
919934
935+ def load_po_token (config_dir : Path ) -> str :
936+ """
937+ Load the PO token either from environment variable or from file (env variable
938+ takes precedence).
939+ Raise ValueError if none was found.
940+ """
920941 token = environ .get ("PO_TOKEN" , None )
942+ if token and len (token ) > 0 :
943+ return token .strip ()
944+
945+ with open (config_dir / "po_token.txt" , "r" ) as f :
946+ token = f .read ().strip ()
947+
921948 if not token :
922949 raise ValueError (
923- "PO_TOKEN environment variable not set. "
924- "Please set the value with export PO_TOKEN=<your_token_value>"
950+ "Failed to load PO token from environment variable or po_token file."
951+ "Please set the value with export PO_TOKEN=<your_token_value> "
952+ "or create a file named 'po_token' in the config directory."
925953 )
926- data ["extractor-args" ] = extractor_args .replace (
927- "<PO_TOKEN_VALUE>" , token
928- )
954+ return token
955+
929956
930957
931958def load_env_file (path : Path ) -> None :
@@ -987,7 +1014,7 @@ def main():
9871014 ytdlp_conf_file = fallback
9881015
9891016 loaded_ytdlp_conf = load_commented_json (ytdlp_conf_file )
990- interpolate_ytdlp_config (loaded_ytdlp_conf )
1017+ interpolate_ytdlp_config (loaded_ytdlp_conf , po_token = load_po_token ( config_dir ) )
9911018 args ["ytdlp_config" ] = loaded_ytdlp_conf
9921019
9931020 logfile_path = Path ("" ) # cwd by default
0 commit comments