Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -637,8 +637,8 @@ def download_story_archive(story_id):
|
|
| 637 |
|
| 638 |
# Prepare data
|
| 639 |
full_text = story_record.get("full_story", "")
|
| 640 |
-
sections = story_record.get("sections",)
|
| 641 |
-
|
| 642 |
# Derive a title from the first sentence
|
| 643 |
split_sentences = full_text.split('.', 1)
|
| 644 |
title = split_sentences[0].strip() if split_sentences and split_sentences[0].strip() else "Untitled"
|
|
@@ -655,24 +655,14 @@ def download_story_archive(story_id):
|
|
| 655 |
pdf = FPDF()
|
| 656 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 657 |
pdf.add_page()
|
| 658 |
-
|
| 659 |
-
# --- Register and set Unicode fonts ---
|
| 660 |
-
# Assuming you have TTF files named 'DejaVuSans.ttf' (regular) and 'DejaVuSans-Bold.ttf' (bold)
|
| 661 |
-
# in the same directory
|
| 662 |
-
try:
|
| 663 |
-
pdf.add_font('DejaVu', '', 'DejaVuSans.ttf', uni=True)
|
| 664 |
-
pdf.add_font('DejaVu', 'B', 'dejaVu-sans-bold.ttf', uni=True)
|
| 665 |
-
pdf.set_font('DejaVu', '', 12) # Set default font to regular
|
| 666 |
-
except Exception as e:
|
| 667 |
-
print(f"Error loading Unicode font: {e}")
|
| 668 |
-
pdf.set_font("Arial", size=12) # Fallback to Arial if DejaVu fails
|
| 669 |
|
| 670 |
# Basic info
|
| 671 |
pdf.cell(0, 10, f"Story ID: {story_id}")
|
| 672 |
pdf.ln()
|
| 673 |
pdf.cell(0, 10, f"Title: {title}")
|
| 674 |
pdf.ln(15)
|
| 675 |
-
|
| 676 |
# Add the full story text with proper line breaks
|
| 677 |
pdf.multi_cell(0, 10, "Full Story:")
|
| 678 |
pdf.ln(5)
|
|
@@ -686,52 +676,46 @@ def download_story_archive(story_id):
|
|
| 686 |
image_url = section_obj.get("image_url", "")
|
| 687 |
|
| 688 |
pdf.add_page()
|
| 689 |
-
pdf.set_font(
|
| 690 |
pdf.cell(0, 10, f"Section {idx + 1}")
|
| 691 |
pdf.ln(15)
|
| 692 |
-
pdf.set_font(
|
| 693 |
pdf.multi_cell(0, 10, section_text)
|
| 694 |
pdf.ln(10)
|
| 695 |
|
| 696 |
# If there's an image URL, download & embed in PDF
|
| 697 |
if image_url:
|
| 698 |
try:
|
| 699 |
-
# Initialize Firebase Storage bucket
|
| 700 |
bucket = storage.bucket()
|
| 701 |
-
|
| 702 |
# Get the image file path from the URL
|
|
|
|
|
|
|
| 703 |
file_path = ""
|
| 704 |
if '/o/' in image_url and '?' in image_url:
|
| 705 |
file_path = image_url.split('/o/')[1].split('?')[0]
|
| 706 |
file_path = urllib.parse.unquote(file_path)
|
| 707 |
-
|
| 708 |
-
|
| 709 |
-
|
| 710 |
-
|
| 711 |
-
|
| 712 |
-
|
| 713 |
-
|
| 714 |
-
|
| 715 |
-
|
| 716 |
-
|
| 717 |
-
|
| 718 |
-
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
os.unlink(temp_img_path)
|
| 724 |
-
else:
|
| 725 |
-
print(f"PDF image error: Could not parse image URL for section {idx + 1}: {image_url}")
|
| 726 |
-
pdf.ln(5)
|
| 727 |
-
pdf.multi_cell(0, 10, f"[Image URL invalid]")
|
| 728 |
-
|
| 729 |
except Exception as img_error:
|
| 730 |
-
print(f"PDF image error
|
| 731 |
-
import traceback
|
| 732 |
-
traceback.print_exc()
|
| 733 |
pdf.ln(5)
|
| 734 |
-
pdf.multi_cell(0, 10, f"[Image could not be included
|
| 735 |
|
| 736 |
# Save PDF to memory
|
| 737 |
pdf_buffer = io.BytesIO()
|
|
@@ -762,20 +746,14 @@ def download_story_archive(story_id):
|
|
| 762 |
if '/o/' in image_url and '?' in image_url:
|
| 763 |
file_path = image_url.split('/o/')[1].split('?')[0]
|
| 764 |
file_path = urllib.parse.unquote(file_path)
|
| 765 |
-
|
| 766 |
-
|
| 767 |
-
|
| 768 |
-
|
| 769 |
-
|
| 770 |
-
|
| 771 |
-
print(f"Added image for section {idx + 1}")
|
| 772 |
-
else:
|
| 773 |
-
print(f"Error downloading image for section {idx + 1}: Could not parse URL: {image_url}")
|
| 774 |
-
|
| 775 |
except Exception as img_error:
|
| 776 |
print(f"Error downloading image for section {idx + 1}: {str(img_error)}")
|
| 777 |
-
import traceback
|
| 778 |
-
traceback.print_exc()
|
| 779 |
|
| 780 |
# ======================
|
| 781 |
# 4) Download each section's audio file to the ZIP
|
|
@@ -789,29 +767,23 @@ def download_story_archive(story_id):
|
|
| 789 |
if '/o/' in audio_url and '?' in audio_url:
|
| 790 |
file_path = audio_url.split('/o/')[1].split('?')[0]
|
| 791 |
file_path = urllib.parse.unquote(file_path)
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
print(f"Added audio for section {idx + 1}")
|
| 799 |
-
else:
|
| 800 |
-
print(f"Error downloading audio for section {idx + 1}: Could not parse URL: {audio_url}")
|
| 801 |
-
|
| 802 |
except Exception as audio_error:
|
| 803 |
print(f"Error downloading audio for section {idx + 1}: {str(audio_error)}")
|
| 804 |
-
import traceback
|
| 805 |
-
traceback.print_exc()
|
| 806 |
|
| 807 |
# ------------------------------------------------------------------
|
| 808 |
# Serve ZIP File
|
| 809 |
# ------------------------------------------------------------------
|
| 810 |
zip_buffer.seek(0)
|
| 811 |
return send_file(
|
| 812 |
-
zip_buffer,
|
| 813 |
-
mimetype='application/zip',
|
| 814 |
-
as_attachment=True,
|
| 815 |
download_name=f"story_{story_id}.zip"
|
| 816 |
)
|
| 817 |
|
|
|
|
| 637 |
|
| 638 |
# Prepare data
|
| 639 |
full_text = story_record.get("full_story", "")
|
| 640 |
+
sections = story_record.get("sections", [])
|
| 641 |
+
|
| 642 |
# Derive a title from the first sentence
|
| 643 |
split_sentences = full_text.split('.', 1)
|
| 644 |
title = split_sentences[0].strip() if split_sentences and split_sentences[0].strip() else "Untitled"
|
|
|
|
| 655 |
pdf = FPDF()
|
| 656 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 657 |
pdf.add_page()
|
| 658 |
+
pdf.set_font("Arial", size=12)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
|
| 660 |
# Basic info
|
| 661 |
pdf.cell(0, 10, f"Story ID: {story_id}")
|
| 662 |
pdf.ln()
|
| 663 |
pdf.cell(0, 10, f"Title: {title}")
|
| 664 |
pdf.ln(15)
|
| 665 |
+
|
| 666 |
# Add the full story text with proper line breaks
|
| 667 |
pdf.multi_cell(0, 10, "Full Story:")
|
| 668 |
pdf.ln(5)
|
|
|
|
| 676 |
image_url = section_obj.get("image_url", "")
|
| 677 |
|
| 678 |
pdf.add_page()
|
| 679 |
+
pdf.set_font("Arial", style='B', size=14)
|
| 680 |
pdf.cell(0, 10, f"Section {idx + 1}")
|
| 681 |
pdf.ln(15)
|
| 682 |
+
pdf.set_font("Arial", size=12)
|
| 683 |
pdf.multi_cell(0, 10, section_text)
|
| 684 |
pdf.ln(10)
|
| 685 |
|
| 686 |
# If there's an image URL, download & embed in PDF
|
| 687 |
if image_url:
|
| 688 |
try:
|
| 689 |
+
# Initialize Firebase Storage bucket
|
| 690 |
bucket = storage.bucket()
|
| 691 |
+
|
| 692 |
# Get the image file path from the URL
|
| 693 |
+
# Firebase Storage URLs are in the format:
|
| 694 |
+
# https://firebasestorage.googleapis.com/v0/b/[bucket]/o/[file_path]?[params]
|
| 695 |
file_path = ""
|
| 696 |
if '/o/' in image_url and '?' in image_url:
|
| 697 |
file_path = image_url.split('/o/')[1].split('?')[0]
|
| 698 |
file_path = urllib.parse.unquote(file_path)
|
| 699 |
+
|
| 700 |
+
# Create a temporary file to hold the image
|
| 701 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
|
| 702 |
+
temp_img_path = temp_file.name
|
| 703 |
+
|
| 704 |
+
# Download the image to temp file
|
| 705 |
+
blob = bucket.blob(file_path)
|
| 706 |
+
blob.download_to_filename(temp_img_path)
|
| 707 |
+
|
| 708 |
+
# Calculate appropriate image width and place it on the page
|
| 709 |
+
page_width = pdf.w - 2*pdf.l_margin
|
| 710 |
+
image_width = min(page_width - 20, 160) # Keep some margin
|
| 711 |
+
pdf.image(temp_img_path, x=(pdf.w - image_width)/2, w=image_width)
|
| 712 |
+
|
| 713 |
+
# Clean up temp file
|
| 714 |
+
os.unlink(temp_img_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 715 |
except Exception as img_error:
|
| 716 |
+
print(f"PDF image error: {str(img_error)}")
|
|
|
|
|
|
|
| 717 |
pdf.ln(5)
|
| 718 |
+
pdf.multi_cell(0, 10, f"[Image could not be included]")
|
| 719 |
|
| 720 |
# Save PDF to memory
|
| 721 |
pdf_buffer = io.BytesIO()
|
|
|
|
| 746 |
if '/o/' in image_url and '?' in image_url:
|
| 747 |
file_path = image_url.split('/o/')[1].split('?')[0]
|
| 748 |
file_path = urllib.parse.unquote(file_path)
|
| 749 |
+
|
| 750 |
+
# Get the blob and download as bytes
|
| 751 |
+
blob = bucket.blob(file_path)
|
| 752 |
+
image_data = blob.download_as_bytes()
|
| 753 |
+
zipf.writestr(f"image_section_{idx + 1}.jpg", image_data)
|
| 754 |
+
print(f"Added image for section {idx + 1}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
except Exception as img_error:
|
| 756 |
print(f"Error downloading image for section {idx + 1}: {str(img_error)}")
|
|
|
|
|
|
|
| 757 |
|
| 758 |
# ======================
|
| 759 |
# 4) Download each section's audio file to the ZIP
|
|
|
|
| 767 |
if '/o/' in audio_url and '?' in audio_url:
|
| 768 |
file_path = audio_url.split('/o/')[1].split('?')[0]
|
| 769 |
file_path = urllib.parse.unquote(file_path)
|
| 770 |
+
|
| 771 |
+
# Get the blob and download as bytes
|
| 772 |
+
blob = bucket.blob(file_path)
|
| 773 |
+
audio_data = blob.download_as_bytes()
|
| 774 |
+
zipf.writestr(f"audio_section_{idx + 1}.mp3", audio_data)
|
| 775 |
+
print(f"Added audio for section {idx + 1}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 776 |
except Exception as audio_error:
|
| 777 |
print(f"Error downloading audio for section {idx + 1}: {str(audio_error)}")
|
|
|
|
|
|
|
| 778 |
|
| 779 |
# ------------------------------------------------------------------
|
| 780 |
# Serve ZIP File
|
| 781 |
# ------------------------------------------------------------------
|
| 782 |
zip_buffer.seek(0)
|
| 783 |
return send_file(
|
| 784 |
+
zip_buffer,
|
| 785 |
+
mimetype='application/zip',
|
| 786 |
+
as_attachment=True,
|
| 787 |
download_name=f"story_{story_id}.zip"
|
| 788 |
)
|
| 789 |
|