Upload main.py
Browse files
main.py
CHANGED
@@ -204,18 +204,26 @@ async def fetch_trending_videos(reddit, processed_videos, subreddit_name):
|
|
204 |
|
205 |
|
206 |
async def download_video(url, filename):
|
207 |
-
"""
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
return
|
217 |
-
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
return None
|
220 |
|
221 |
|
@@ -500,39 +508,32 @@ async def process_video_task(video_info: dict):
|
|
500 |
try:
|
501 |
api_key = os.environ.get("GOOGLE_API_KEY")
|
502 |
if not api_key:
|
503 |
-
processing_results[video_id] = {
|
504 |
-
"status": "error",
|
505 |
-
"error": "Missing GOOGLE_API_KEY",
|
506 |
-
}
|
507 |
return
|
508 |
|
509 |
reddit = await initialize_reddit()
|
510 |
submission = await reddit.submission(id=video_id)
|
511 |
comments = await get_n_comments(submission, 5)
|
512 |
comments_string = "\n\n".join(comments) if comments else "No comments found."
|
|
|
513 |
temp_video_filename = f"{video_id}.mp4"
|
514 |
-
downloaded_video = await download_video(
|
515 |
-
video_info.get("url"), temp_video_filename
|
516 |
-
)
|
517 |
if not downloaded_video:
|
518 |
-
processing_results[video_id] = {
|
519 |
-
"status": "error",
|
520 |
-
"error": "Failed to download video",
|
521 |
-
}
|
522 |
await reddit.close()
|
523 |
return
|
524 |
|
525 |
-
#
|
526 |
loop = asyncio.get_running_loop()
|
527 |
result = await loop.run_in_executor(
|
528 |
process_executor,
|
529 |
-
generate_video, # blocking function
|
530 |
downloaded_video,
|
531 |
video_info.get("title"),
|
532 |
comments_string,
|
533 |
api_key,
|
534 |
f"{video_id}",
|
535 |
-
"@damnsointeresting"
|
536 |
)
|
537 |
|
538 |
if result and result[0]:
|
@@ -542,13 +543,10 @@ async def process_video_task(video_info: dict):
|
|
542 |
"status": "completed",
|
543 |
"video_url": video_url,
|
544 |
"generated_title": generated_title,
|
545 |
-
"generated_caption": generated_caption
|
546 |
}
|
547 |
else:
|
548 |
-
processing_results[video_id] = {
|
549 |
-
"status": "error",
|
550 |
-
"error": "Video processing failed",
|
551 |
-
}
|
552 |
|
553 |
if os.path.exists(temp_video_filename):
|
554 |
os.remove(temp_video_filename)
|
|
|
204 |
|
205 |
|
206 |
async def download_video(url, filename):
|
207 |
+
"""
|
208 |
+
Offload the synchronous RedDownloader.Download call to a thread.
|
209 |
+
"""
|
210 |
+
loop = asyncio.get_running_loop()
|
211 |
+
|
212 |
+
def blocking_download():
|
213 |
+
try:
|
214 |
+
# This call is blocking.
|
215 |
+
RedDownloader.Download(url, quality=720, output=filename.split('.')[0])
|
216 |
+
return os.path.exists(filename)
|
217 |
+
except Exception as e:
|
218 |
+
logging.error(f"Error downloading video: {e}")
|
219 |
+
return False
|
220 |
+
|
221 |
+
exists = await loop.run_in_executor(None, blocking_download)
|
222 |
+
if exists:
|
223 |
+
logging.info(f"Video downloaded to {filename}")
|
224 |
+
return filename
|
225 |
+
else:
|
226 |
+
logging.error(f"Video file not found after download: {filename}")
|
227 |
return None
|
228 |
|
229 |
|
|
|
508 |
try:
|
509 |
api_key = os.environ.get("GOOGLE_API_KEY")
|
510 |
if not api_key:
|
511 |
+
processing_results[video_id] = {"status": "error", "error": "Missing GOOGLE_API_KEY"}
|
|
|
|
|
|
|
512 |
return
|
513 |
|
514 |
reddit = await initialize_reddit()
|
515 |
submission = await reddit.submission(id=video_id)
|
516 |
comments = await get_n_comments(submission, 5)
|
517 |
comments_string = "\n\n".join(comments) if comments else "No comments found."
|
518 |
+
|
519 |
temp_video_filename = f"{video_id}.mp4"
|
520 |
+
downloaded_video = await download_video(video_info.get("url"), temp_video_filename)
|
|
|
|
|
521 |
if not downloaded_video:
|
522 |
+
processing_results[video_id] = {"status": "error", "error": "Failed to download video"}
|
|
|
|
|
|
|
523 |
await reddit.close()
|
524 |
return
|
525 |
|
526 |
+
# Offload heavy MoviePy processing using the process pool.
|
527 |
loop = asyncio.get_running_loop()
|
528 |
result = await loop.run_in_executor(
|
529 |
process_executor,
|
530 |
+
generate_video, # blocking function using MoviePy
|
531 |
downloaded_video,
|
532 |
video_info.get("title"),
|
533 |
comments_string,
|
534 |
api_key,
|
535 |
f"{video_id}",
|
536 |
+
"@damnsointeresting"
|
537 |
)
|
538 |
|
539 |
if result and result[0]:
|
|
|
543 |
"status": "completed",
|
544 |
"video_url": video_url,
|
545 |
"generated_title": generated_title,
|
546 |
+
"generated_caption": generated_caption
|
547 |
}
|
548 |
else:
|
549 |
+
processing_results[video_id] = {"status": "error", "error": "Video processing failed"}
|
|
|
|
|
|
|
550 |
|
551 |
if os.path.exists(temp_video_filename):
|
552 |
os.remove(temp_video_filename)
|