diff --git a/.env.template b/.env.template index 173bacf..c94fa6a 100644 --- a/.env.template +++ b/.env.template @@ -36,4 +36,4 @@ VOICE="Matthew" # e.g. en_us_002 TTsChoice="polly" # todo add docs # IN-PROGRESS - not yet implemented -STORYMODE="False" +STORYMODE="False" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 2701ca5..1c28a20 100644 --- a/.gitignore +++ b/.gitignore @@ -169,4 +169,6 @@ results/* reddit-bot-351418-5560ebc49cac.json /.idea *.pyc -/video_creation/data/videos.json \ No newline at end of file +/video_creation/data/videos.json +utils/envUpdate.py.old +envVars.txt \ No newline at end of file diff --git a/main.py b/main.py index b4e1123..bfb3ba3 100755 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from os import getenv, name from reddit.subreddit import get_subreddit_threads from utils.cleanup import cleanup from utils.console import print_markdown, print_step +from utils.envUpdate import envUpdate from video_creation.background import download_background, chop_background_video from video_creation.final_video import make_final_video from video_creation.screenshot_downloader import download_screenshots_of_reddit_posts @@ -37,6 +38,7 @@ reddit2fa = getenv("REDDIT_2FA") def main(): + envUpdate() cleanup() def get_obj(): diff --git a/utils/envUpdate.py b/utils/envUpdate.py new file mode 100644 index 0000000..f225419 --- /dev/null +++ b/utils/envUpdate.py @@ -0,0 +1,48 @@ +import os +import subprocess +import tempfile +import logging + +from os import path +from sys import platform, stderr + +log = logging.getLogger(__name__) + +def envUpdate(): + if path.exists(".env.template"): + if platform == "win32" or platform == "cygwin": + runPS('utils\envUpdateWin.ps1') + f = open("envVars.txt", "rb") + envTemplate = f.read() + elif platform == "darwin" or platform == "linux": + envTemplate = subprocess.check_output( + "awk -F '=' 'NF {print $1}' .env.template | grep --regexp=^[a-zA-Z]", + shell=True, + ) + return + tempEnv = tempfile.TemporaryFile() + tempEnv.write(envTemplate) + tempEnv.seek(0) + envVars = tempEnv.readlines() + + missing = [] + isMissingEnvs = False + for env in envVars: + try: + env = env.decode("utf-8").strip() + except AttributeError: + env = env.strip() + + if env not in os.environ: + isMissingEnvs = True + missing.append(env) + + if isMissingEnvs: + log.error( + f"[ERROR] The following environment variables are missing: {missing}.)" + ) + exit(-1) + +def runPS(cmd): + completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True) + return completed \ No newline at end of file diff --git a/utils/envUpdateWin.ps1 b/utils/envUpdateWin.ps1 new file mode 100644 index 0000000..1eb8f64 --- /dev/null +++ b/utils/envUpdateWin.ps1 @@ -0,0 +1,9 @@ +$envFile = Get-Content ".\.env.template" + +$envFile -split "=" | Where-Object {$_ -notmatch '\"'} | Set-Content ".\envVarsbefSpl.txt" +Get-Content ".\envVarsbefSpl.txt" | Where-Object {$_ -notmatch '\#'} | Set-Content ".\envVarsN.txt" +Get-Content ".\envVarsN.txt" | Where-Object {$_ -ne ''} | Set-Content ".\envVars.txt" +Remove-Item ".\envVarsbefSpl.txt" +Remove-Item ".\envVarsN.txt" + +Write-Host $nowSplit \ No newline at end of file