|
12 | 12 | from summarize import handle_summarization_request |
13 | 13 | from duration import format_duration |
14 | 14 | from analytics import track_event |
15 | | -from utils import sanitize_filename, get_original_language, normalize_language_code, create_emoji_friendly_pdf_with_weasyprint |
| 15 | +from utils import sanitize_filename, get_original_language, normalize_language_code, create_emoji_friendly_pdf_with_weasyprint, create_emoji_friendly_pdf_with_weasyprint_async |
16 | 16 | from languages import languages |
17 | 17 | import openai |
18 | 18 | import aiofiles |
@@ -403,11 +403,24 @@ async def handle_youtube_link(update: Update, context: CallbackContext): |
403 | 403 | filename = f"{base_filename}_{transcript.get('normalized_language_code')}.txt" |
404 | 404 |
|
405 | 405 | logger.info(f"Sending transcript for language: {language_code}") |
406 | | - # Use BytesIO for binary mode (recommended for Telegram) |
407 | | - file_obj = io.BytesIO(formatted_transcript.encode('utf-8')) |
408 | | - file_obj.name = filename # Telegram uses this as the filename |
409 | | - logger.info(f"Sending transcript file: {filename} to user {user_id}.") |
410 | | - msg = await update.message.reply_document(document=InputFile(file_obj), caption=f"{filename}") |
| 406 | + |
| 407 | + try: |
| 408 | + pdf_file = await create_emoji_friendly_pdf_with_weasyprint_async(f"{video_info}\n\n{formatted_transcript}") |
| 409 | + pdf_filename = filename[:-3] + "pdf" # Set the filename for the PDF |
| 410 | + logger.info(f"Sending PDF transcript file: {pdf_filename} to user {user_id}.") |
| 411 | + |
| 412 | + msg = await context.bot.send_document( |
| 413 | + chat_id=update.message.chat_id, |
| 414 | + document=InputFile(pdf_file, filename=pdf_filename), |
| 415 | + caption=f"{pdf_filename}" |
| 416 | + ) |
| 417 | + except Exception as e: |
| 418 | + # Use BytesIO for binary mode (recommended for Telegram) |
| 419 | + file_obj = io.BytesIO(formatted_transcript.encode('utf-8')) |
| 420 | + file_obj.name = filename # Telegram uses this as the filename |
| 421 | + logger.error(f"Failed to send PDF transcript file {pdf_filename}: {e}") |
| 422 | + logger.info(f"Sending TXT transcript file: {filename} to user {user_id}.") |
| 423 | + msg = await update.message.reply_document(document=InputFile(file_obj), caption=f"{filename}") |
411 | 424 |
|
412 | 425 | track_event( |
413 | 426 | user_id=user.id, |
@@ -1009,7 +1022,7 @@ async def handle_show_full_video_description_button(update: Update, context: Cal |
1009 | 1022 | video_info_short = video_info_full_description.split("<b>📝 Description</b>:")[0] |
1010 | 1023 |
|
1011 | 1024 | try: |
1012 | | - pdf_file = create_emoji_friendly_pdf_with_weasyprint(video_info_full_description) |
| 1025 | + pdf_file = await create_emoji_friendly_pdf_with_weasyprint_async(video_info_full_description) |
1013 | 1026 | # pdf_file = convert_to_pdf_xhtml2pdf("Example text for testing") |
1014 | 1027 | if video_details: |
1015 | 1028 | logger.info(f"Video details for video {video_id} and PDF file name: {str(video_details)[:300]}.") |
|
0 commit comments