Replace prints with proper logs.

This commit is contained in:
Cristiano Ferreira
2023-12-12 22:20:22 +00:00
parent 9f4aed52ae
commit 2b91cfe26d
3 changed files with 38 additions and 25 deletions

View File

@@ -1,3 +1,5 @@
import logging
from google.cloud import storage
import os
import uuid
@@ -13,7 +15,7 @@ def download_firebase_file(bucket_name, source_blob_name, destination_file_name)
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(source_blob_name)
blob.download_to_filename(destination_file_name)
print(f"File downloaded to {destination_file_name}")
logging.info(f"File uploaded to {destination_file_name}")
return destination_file_name
def upload_file_firebase(bucket_name, destination_blob_name, source_file_name):
@@ -23,10 +25,10 @@ def upload_file_firebase(bucket_name, destination_blob_name, source_file_name):
try:
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(f"File uploaded to {destination_blob_name}")
logging.info(f"File uploaded to {destination_blob_name}")
return True
except Exception as e:
print("Error uploading file to Google Cloud Storage:", e)
logging.error("Error uploading file to Google Cloud Storage: " + str(e))
return False
def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file_name):
@@ -36,7 +38,7 @@ def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file
try:
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print(f"File uploaded to {destination_blob_name}")
logging.info(f"File uploaded to {destination_blob_name}")
# Make the file public
blob.make_public()
@@ -45,7 +47,7 @@ def upload_file_firebase_get_url(bucket_name, destination_blob_name, source_file
url = blob.public_url
return url
except Exception as e:
print("Error uploading file to Google Cloud Storage:", e)
logging.error("Error uploading file to Google Cloud Storage: " + str(e))
return None
def save_to_db(collection: str, item):
@@ -53,7 +55,7 @@ def save_to_db(collection: str, item):
collection_ref = db.collection(collection)
(update_time, document_ref) = collection_ref.add(item)
if document_ref:
print(f"Document added with ID: {document_ref.id}")
logging.info(f"Document added with ID: {document_ref.id}")
return (True, document_ref.id)
else:
return (False, None)
@@ -67,7 +69,7 @@ def save_to_db_with_id(collection: str, item, id: str):
# Set the data to the document
document_ref.set(item)
if document_ref:
print(f"Document added with ID: {document_ref.id}")
logging.info(f"Document added with ID: {document_ref.id}")
return (True, document_ref.id)
else:
return (False, None)

View File

@@ -1,3 +1,4 @@
import logging
import os
import random
@@ -5,6 +6,8 @@ import requests
import time
from dotenv import load_dotenv
import app
from helper.constants import *
from helper.firebase_helper import upload_file_firebase_get_url, save_to_db_with_id
from heygen.AvatarEnum import AvatarEnum
@@ -35,6 +38,7 @@ TYLER_CHRISTOPHER = "03c796f8ed274bb38f19e893bcbc6121"
def create_videos_and_save_to_db(exercises, template, id):
# Speaking 1
logging.info('Creating video for speaking part 1')
sp1_result = create_video(exercises[0]["question"], random.choice(list(AvatarEnum)))
if sp1_result is not None:
sound_file_path = VIDEO_FILES_PATH + sp1_result
@@ -47,9 +51,10 @@ def create_videos_and_save_to_db(exercises, template, id):
template["exercises"][0]["video_url"] = sp1_video_url
template["exercises"][0]["video_path"] = sp1_video_path
else:
print("Failed to create video for part 1 question: " + exercises[0]["question"])
logging.error("Failed to create video for part 1 question: " + exercises[0]["question"])
# Speaking 2
logging.info('Creating video for speaking part 2')
sp2_result = create_video(exercises[1]["question"], random.choice(list(AvatarEnum)))
if sp2_result is not None:
sound_file_path = VIDEO_FILES_PATH + sp2_result
@@ -63,11 +68,12 @@ def create_videos_and_save_to_db(exercises, template, id):
template["exercises"][1]["video_url"] = sp2_video_url
template["exercises"][1]["video_path"] = sp2_video_path
else:
print("Failed to create video for part 2 question: " + exercises[1]["question"])
logging.error("Failed to create video for part 2 question: " + exercises[1]["question"])
# Speaking 3
sp3_questions = []
avatar = random.choice(list(AvatarEnum))
logging.info('Creating videos for speaking part 3')
for question in exercises[2]["questions"]:
result = create_video(question, avatar)
if result is not None:
@@ -81,7 +87,7 @@ def create_videos_and_save_to_db(exercises, template, id):
}
sp3_questions.append(video)
else:
print("Failed to create video for part 3 question: " + question)
logging.error("Failed to create video for part 3 question: " + question)
template["exercises"][2]["prompts"] = sp3_questions
template["exercises"][2]["title"] = exercises[2]["topic"]
@@ -103,8 +109,8 @@ def create_video(text, avatar: AvatarEnum):
]
}
response = requests.post(CREATE_VIDEO_URL, headers=POST_HEADER, json=data)
print(response.status_code)
print(response.json())
logging.info(response.status_code)
logging.info(response.json())
# GET TO CHECK STATUS AND GET VIDEO WHEN READY
video_id = response.json()["data"]["video_id"]
@@ -123,11 +129,11 @@ def create_video(text, avatar: AvatarEnum):
error = response_data["data"]["error"]
if status != "completed" and error is None:
print(f"Status: {status}")
logging.info(f"Status: {status}")
time.sleep(5) # Wait for 5 second before the next request
print(response.status_code)
print(response.json())
logging.info(response.status_code)
logging.info(response.json())
# DOWNLOAD VIDEO
download_url = response.json()['data']['video_url']
@@ -141,8 +147,8 @@ def create_video(text, avatar: AvatarEnum):
output_path = os.path.join(output_directory, output_filename)
with open(output_path, 'wb') as f:
f.write(response.content)
print(f"File '{output_filename}' downloaded successfully.")
logging.info(f"File '{output_filename}' downloaded successfully.")
return output_filename
else:
print(f"Failed to download file. Status code: {response.status_code}")
logging.error(f"Failed to download file. Status code: {response.status_code}")
return None