Skip to content

Commit

Permalink
Check empty environment variables & Reformat .env.example (FujiwaraCh…
Browse files Browse the repository at this point in the history
…oki#198)

* reformat .env.example: removed inline comments due to parsing error in load_dotenv

* Add empty environment variable checks
  • Loading branch information
Gudauu authored Feb 12, 2024
1 parent 4768a88 commit 10ecdac
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
35 changes: 30 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
# See EnvironmentVariables.md for more information.
ASSEMBLY_AI_API_KEY="" # Optional

# Necessary API Keys
# -------------------

# TikTok Session ID
# Obtain your session ID by logging into TikTok and copying the sessionid cookie.
TIKTOK_SESSION_ID=""
IMAGEMAGICK_BINARY="" # Download from https://imagemagick.org/script/download.php
PEXELS_API_KEY="" # Get from https://www.pexels.com/api/
OPENAI_API_KEY="" # Optional
GOOGLE_API_KEY="" # Get from https://makersuite.google.com/app/apikey

# ImageMagick Binary Path
# Download ImageMagick from https://imagemagick.org/script/download.php
IMAGEMAGICK_BINARY=""

# Pexels API Key
# Register at https://www.pexels.com/api/ to get your API key.
PEXELS_API_KEY=""

# Optional API Keys
# -----------------

# OpenAI API Key
# Visit https://openai.com/api/ for details on obtaining an API key.
OPENAI_API_KEY=""

# AssemblyAI API Key
# Sign up at https://www.assemblyai.com/ to receive an API key.
ASSEMBLY_AI_API_KEY=""

# Google API Key
# Generate your API key through https://makersuite.google.com/app/apikey
GOOGLE_API_KEY=""

12 changes: 8 additions & 4 deletions Backend/main.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
import os
from utils import *
from dotenv import load_dotenv

# Load environment variables
load_dotenv("../.env")
# Check if all required environment variables are set
# This must happen before importing video which uses API keys without checking
check_env_vars()

from gpt import *
from video import *
from utils import *
from search import *
from uuid import uuid4
from tiktokvoice import *
from flask_cors import CORS
from termcolor import colored
from dotenv import load_dotenv
from youtube import upload_video
from apiclient.errors import HttpError
from flask import Flask, request, jsonify
from moviepy.config import change_settings


# Load environment variables
load_dotenv("../.env")

# Set environment variables
SESSION_ID = os.getenv("TIKTOK_SESSION_ID")
Expand Down
26 changes: 26 additions & 0 deletions Backend/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import sys
import json
import random
import logging
Expand Down Expand Up @@ -90,3 +91,28 @@ def choose_random_song() -> str:
return f"../Songs/{song}"
except Exception as e:
logger.error(colored(f"Error occurred while choosing random song: {str(e)}", "red"))


def check_env_vars() -> None:
"""
Checks if the necessary environment variables are set.
Returns:
None
Raises:
SystemExit: If any required environment variables are missing.
"""
try:
required_vars = ["PEXELS_API_KEY", "TIKTOK_SESSION_ID", "IMAGEMAGICK_BINARY"]
missing_vars = [var + os.getenv(var) for var in required_vars if os.getenv(var) is None or (len(os.getenv(var)) == 0)]

if missing_vars:
missing_vars_str = ", ".join(missing_vars)
logger.error(colored(f"The following environment variables are missing: {missing_vars_str}", "red"))
logger.error(colored("Please consult 'EnvironmentVariables.md' for instructions on how to set them.", "yellow"))
sys.exit(1) # Aborts the program
except Exception as e:
logger.error(f"Error occurred while checking environment variables: {str(e)}")
sys.exit(1) # Aborts the program if an unexpected error occurs

0 comments on commit 10ecdac

Please sign in to comment.