Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -570,7 +570,7 @@ def view_projects():
|
|
| 570 |
except Exception as e:
|
| 571 |
return jsonify({'error': str(e)}), 500
|
| 572 |
|
| 573 |
-
|
| 574 |
@app.route('/api/view/videos', methods=['GET'])
|
| 575 |
def view_videos():
|
| 576 |
"""
|
|
@@ -613,8 +613,8 @@ def view_videos():
|
|
| 613 |
return jsonify({'error': str(e)}), 500
|
| 614 |
|
| 615 |
|
|
|
|
| 616 |
|
| 617 |
-
@app.route('/api/download/story_archive/<story_id>', methods=['GET'])
|
| 618 |
def download_story_archive(story_id):
|
| 619 |
try:
|
| 620 |
# --- Authentication ---
|
|
@@ -629,75 +629,118 @@ def download_story_archive(story_id):
|
|
| 629 |
# --- Fetch the Story ---
|
| 630 |
stories_ref = db.reference('stories')
|
| 631 |
story_record = stories_ref.child(story_id).get()
|
| 632 |
-
|
| 633 |
if not story_record:
|
| 634 |
return jsonify({'error': 'Story not found'}), 404
|
| 635 |
if story_record.get('uid') != uid:
|
| 636 |
return jsonify({'error': 'Unauthorized'}), 403
|
| 637 |
|
| 638 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 639 |
zip_buffer = io.BytesIO()
|
| 640 |
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 641 |
|
| 642 |
-
#
|
|
|
|
|
|
|
| 643 |
pdf = FPDF()
|
| 644 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 645 |
pdf.add_page()
|
| 646 |
pdf.set_font("Arial", size=12)
|
| 647 |
-
pdf.multi_cell(0, 10, f"Story ID: {story_id}\n\n")
|
| 648 |
-
pdf.multi_cell(0, 10, f"Title: {story_record.get('title', 'Untitled')}\n\n")
|
| 649 |
-
pdf.multi_cell(0, 10, f"Full Story:\n\n{story_record.get('full_story', '')}\n\n")
|
| 650 |
|
| 651 |
-
#
|
| 652 |
-
|
| 653 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 654 |
pdf.add_page()
|
| 655 |
pdf.set_font("Arial", style='B', size=14)
|
| 656 |
-
pdf.multi_cell(0, 10, f"Section {idx + 1}
|
| 657 |
pdf.set_font("Arial", size=12)
|
| 658 |
-
pdf.multi_cell(0, 10,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 659 |
|
| 660 |
-
# Save PDF to
|
| 661 |
pdf_buffer = io.BytesIO()
|
| 662 |
pdf.output(pdf_buffer)
|
| 663 |
pdf_buffer.seek(0)
|
|
|
|
| 664 |
zipf.writestr(f"{story_id}.pdf", pdf_buffer.read())
|
| 665 |
|
| 666 |
-
#
|
| 667 |
-
|
| 668 |
-
|
| 669 |
-
txt_content
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
txt_content += f"\n\nSection {idx + 1}:\n{section}"
|
| 674 |
-
|
| 675 |
-
# Add TXT file to ZIP
|
| 676 |
zipf.writestr(f"{story_id}.txt", txt_content)
|
| 677 |
|
| 678 |
-
#
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
|
|
|
|
|
|
|
|
|
| 682 |
bucket = storage.bucket()
|
| 683 |
-
blob_path =
|
| 684 |
blob = bucket.blob(blob_path)
|
| 685 |
image_data = blob.download_as_bytes()
|
| 686 |
-
zipf.writestr(f"
|
| 687 |
-
|
| 688 |
-
#
|
| 689 |
-
|
| 690 |
-
|
| 691 |
-
|
|
|
|
|
|
|
| 692 |
bucket = storage.bucket()
|
| 693 |
blob_path = audio_url.split('/o/')[-1].split('?')[0]
|
| 694 |
blob = bucket.blob(blob_path)
|
| 695 |
audio_data = blob.download_as_bytes()
|
| 696 |
-
zipf.writestr(f"
|
| 697 |
|
| 698 |
-
#
|
|
|
|
|
|
|
| 699 |
zip_buffer.seek(0)
|
| 700 |
-
return send_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 701 |
|
| 702 |
except Exception as e:
|
| 703 |
return jsonify({'error': str(e)}), 500
|
|
|
|
| 570 |
except Exception as e:
|
| 571 |
return jsonify({'error': str(e)}), 500
|
| 572 |
|
| 573 |
+
# view videos endpoint
|
| 574 |
@app.route('/api/view/videos', methods=['GET'])
|
| 575 |
def view_videos():
|
| 576 |
"""
|
|
|
|
| 613 |
return jsonify({'error': str(e)}), 500
|
| 614 |
|
| 615 |
|
| 616 |
+
#download archives endpoint
|
| 617 |
|
|
|
|
| 618 |
def download_story_archive(story_id):
|
| 619 |
try:
|
| 620 |
# --- Authentication ---
|
|
|
|
| 629 |
# --- Fetch the Story ---
|
| 630 |
stories_ref = db.reference('stories')
|
| 631 |
story_record = stories_ref.child(story_id).get()
|
|
|
|
| 632 |
if not story_record:
|
| 633 |
return jsonify({'error': 'Story not found'}), 404
|
| 634 |
if story_record.get('uid') != uid:
|
| 635 |
return jsonify({'error': 'Unauthorized'}), 403
|
| 636 |
|
| 637 |
+
# Prepare data
|
| 638 |
+
full_text = story_record.get("full_story", "")
|
| 639 |
+
sections = story_record.get("sections", [])
|
| 640 |
+
|
| 641 |
+
# Derive a title from the first sentence
|
| 642 |
+
split_sentences = full_text.split('.', 1)
|
| 643 |
+
title = split_sentences[0].strip() if split_sentences and split_sentences[0].strip() else "Untitled"
|
| 644 |
+
|
| 645 |
+
# ------------------------------------------------------------------
|
| 646 |
+
# Create an in-memory ZIP
|
| 647 |
+
# ------------------------------------------------------------------
|
| 648 |
zip_buffer = io.BytesIO()
|
| 649 |
with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zipf:
|
| 650 |
|
| 651 |
+
# ======================
|
| 652 |
+
# 1) Generate PDF
|
| 653 |
+
# ======================
|
| 654 |
pdf = FPDF()
|
| 655 |
pdf.set_auto_page_break(auto=True, margin=15)
|
| 656 |
pdf.add_page()
|
| 657 |
pdf.set_font("Arial", size=12)
|
|
|
|
|
|
|
|
|
|
| 658 |
|
| 659 |
+
# Basic info
|
| 660 |
+
pdf.multi_cell(0, 10, f"Story ID: {story_id}\n")
|
| 661 |
+
pdf.multi_cell(0, 10, f"Title: {title}\n")
|
| 662 |
+
pdf.multi_cell(0, 10, f"Full Story:\n\n{full_text}\n\n")
|
| 663 |
+
|
| 664 |
+
# For each section, create a new page
|
| 665 |
+
for idx, section_obj in enumerate(sections):
|
| 666 |
+
# Each section is a dict with "section_text" and "image_url"
|
| 667 |
+
section_text = section_obj.get("section_text", "")
|
| 668 |
+
image_url = section_obj.get("image_url", "")
|
| 669 |
+
|
| 670 |
pdf.add_page()
|
| 671 |
pdf.set_font("Arial", style='B', size=14)
|
| 672 |
+
pdf.multi_cell(0, 10, f"Section {idx + 1}")
|
| 673 |
pdf.set_font("Arial", size=12)
|
| 674 |
+
pdf.multi_cell(0, 10, section_text)
|
| 675 |
+
|
| 676 |
+
# If there's an image URL, download & embed in PDF
|
| 677 |
+
if image_url and "firebasestorage.googleapis.com" in image_url:
|
| 678 |
+
bucket = storage.bucket()
|
| 679 |
+
blob_path = image_url.split('/o/')[-1].split('?')[0]
|
| 680 |
+
blob = bucket.blob(blob_path)
|
| 681 |
+
image_data = blob.download_as_bytes()
|
| 682 |
+
|
| 683 |
+
# Save the image to a temp file so FPDF can read it
|
| 684 |
+
temp_img_path = f"/tmp/{uuid.uuid4().hex}.jpg"
|
| 685 |
+
with open(temp_img_path, "wb") as f:
|
| 686 |
+
f.write(image_data)
|
| 687 |
+
|
| 688 |
+
# Insert the image at (x=10, y=None => next line), width=100
|
| 689 |
+
pdf.image(temp_img_path, x=10, y=None, w=100)
|
| 690 |
+
# Clean up temp file
|
| 691 |
+
os.remove(temp_img_path)
|
| 692 |
|
| 693 |
+
# Save PDF to memory
|
| 694 |
pdf_buffer = io.BytesIO()
|
| 695 |
pdf.output(pdf_buffer)
|
| 696 |
pdf_buffer.seek(0)
|
| 697 |
+
# Add PDF to the ZIP
|
| 698 |
zipf.writestr(f"{story_id}.pdf", pdf_buffer.read())
|
| 699 |
|
| 700 |
+
# ======================
|
| 701 |
+
# 2) Generate a TXT file
|
| 702 |
+
# ======================
|
| 703 |
+
txt_content = f"Story ID: {story_id}\nTitle: {title}\n\nFull Story:\n\n{full_text}\n\n"
|
| 704 |
+
for idx, section_obj in enumerate(sections):
|
| 705 |
+
section_text = section_obj.get("section_text", "")
|
| 706 |
+
txt_content += f"\n\nSection {idx + 1}:\n{section_text}"
|
|
|
|
|
|
|
|
|
|
| 707 |
zipf.writestr(f"{story_id}.txt", txt_content)
|
| 708 |
|
| 709 |
+
# ======================
|
| 710 |
+
# 3) Download each section's image file to the ZIP
|
| 711 |
+
# ======================
|
| 712 |
+
# If you want each image in a separate folder, you can do "images/section_{idx+1}.jpg"
|
| 713 |
+
for idx, section_obj in enumerate(sections):
|
| 714 |
+
image_url = section_obj.get("image_url", "")
|
| 715 |
+
if image_url and "firebasestorage.googleapis.com" in image_url:
|
| 716 |
bucket = storage.bucket()
|
| 717 |
+
blob_path = image_url.split('/o/')[-1].split('?')[0]
|
| 718 |
blob = bucket.blob(blob_path)
|
| 719 |
image_data = blob.download_as_bytes()
|
| 720 |
+
zipf.writestr(f"image_section_{idx + 1}.jpg", image_data)
|
| 721 |
+
|
| 722 |
+
# ======================
|
| 723 |
+
# 4) Download each section's audio file to the ZIP
|
| 724 |
+
# ======================
|
| 725 |
+
for idx, section_obj in enumerate(sections):
|
| 726 |
+
audio_url = section_obj.get("audio_url", "")
|
| 727 |
+
if audio_url and "firebasestorage.googleapis.com" in audio_url:
|
| 728 |
bucket = storage.bucket()
|
| 729 |
blob_path = audio_url.split('/o/')[-1].split('?')[0]
|
| 730 |
blob = bucket.blob(blob_path)
|
| 731 |
audio_data = blob.download_as_bytes()
|
| 732 |
+
zipf.writestr(f"audio_section_{idx + 1}.mp3", audio_data)
|
| 733 |
|
| 734 |
+
# ------------------------------------------------------------------
|
| 735 |
+
# Serve ZIP File
|
| 736 |
+
# ------------------------------------------------------------------
|
| 737 |
zip_buffer.seek(0)
|
| 738 |
+
return send_file(
|
| 739 |
+
zip_buffer,
|
| 740 |
+
mimetype='application/zip',
|
| 741 |
+
as_attachment=True,
|
| 742 |
+
download_name=f"story_{story_id}.zip"
|
| 743 |
+
)
|
| 744 |
|
| 745 |
except Exception as e:
|
| 746 |
return jsonify({'error': str(e)}), 500
|