rairo commited on
Commit
0185b10
·
verified ·
1 Parent(s): 94074b0

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +33 -14
main.py CHANGED
@@ -615,7 +615,6 @@ def view_videos():
615
 
616
 
617
  #download archives endpoint
618
-
619
  @app.route('/api/download/story_archive/<story_id>', methods=['GET'])
620
  def download_story_archive(story_id):
621
  try:
@@ -686,31 +685,38 @@ def download_story_archive(story_id):
686
  # If there's an image URL, download & embed in PDF
687
  if image_url:
688
  try:
689
- # Initialize Firebase Storage bucket based on URL type
 
 
 
690
  if image_url.startswith('gs://'):
 
691
  path_parts = image_url[5:].split('/', 1)
692
  if len(path_parts) != 2:
693
  continue
694
  bucket_name, file_path = path_parts
695
  bucket = storage.bucket(bucket_name)
696
  else:
 
697
  if '/o/' not in image_url or '?' not in image_url:
698
  continue
699
  file_path = image_url.split('/o/')[1].split('?')[0]
700
  file_path = urllib.parse.unquote(file_path)
701
- bucket = storage.bucket()
702
-
703
- blob = bucket.blob(file_path)
704
- # Create a temporary file to hold the image
705
  with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
706
  temp_img_path = temp_file.name
707
- # Download the image to temp file
 
 
708
  blob.download_to_filename(temp_img_path)
 
709
  # Calculate appropriate image width and place it on the page
710
- page_width = pdf.w - 2*pdf.l_margin
711
- image_width = min(page_width - 20, 160)
712
- pdf.image(temp_img_path, x=(pdf.w - image_width)/2, w=image_width)
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)}")
@@ -741,6 +747,10 @@ def download_story_archive(story_id):
741
  image_url = section_obj.get("image_url", "")
742
  if image_url:
743
  try:
 
 
 
 
744
  if image_url.startswith('gs://'):
745
  path_parts = image_url[5:].split('/', 1)
746
  if len(path_parts) != 2:
@@ -752,10 +762,12 @@ def download_story_archive(story_id):
752
  continue
753
  file_path = image_url.split('/o/')[1].split('?')[0]
754
  file_path = urllib.parse.unquote(file_path)
755
- bucket = storage.bucket()
 
756
  blob = bucket.blob(file_path)
757
  image_data = blob.download_as_bytes()
758
- # Get file extension from blob name
 
759
  ext = os.path.splitext(blob.name)[1].lower() or '.jpg'
760
  zipf.writestr(f"image_section_{idx + 1}{ext}", image_data)
761
  except Exception as img_error:
@@ -765,6 +777,10 @@ def download_story_archive(story_id):
765
  audio_url = section_obj.get("audio_url", "")
766
  if audio_url:
767
  try:
 
 
 
 
768
  if audio_url.startswith('gs://'):
769
  path_parts = audio_url[5:].split('/', 1)
770
  if len(path_parts) != 2:
@@ -776,9 +792,12 @@ def download_story_archive(story_id):
776
  continue
777
  file_path = audio_url.split('/o/')[1].split('?')[0]
778
  file_path = urllib.parse.unquote(file_path)
779
- bucket = storage.bucket()
 
780
  blob = bucket.blob(file_path)
781
  audio_data = blob.download_as_bytes()
 
 
782
  ext = os.path.splitext(blob.name)[1].lower() or '.mp3'
783
  zipf.writestr(f"audio_section_{idx + 1}{ext}", audio_data)
784
  except Exception as audio_error:
 
615
 
616
 
617
  #download archives endpoint
 
618
  @app.route('/api/download/story_archive/<story_id>', methods=['GET'])
619
  def download_story_archive(story_id):
620
  try:
 
685
  # If there's an image URL, download & embed in PDF
686
  if image_url:
687
  try:
688
+ # Initialize Firebase Storage bucket
689
+ bucket = storage.bucket()
690
+
691
+ # Handle both gs:// and HTTPS URLs
692
  if image_url.startswith('gs://'):
693
+ # Extract bucket name and file path from gs:// URL
694
  path_parts = image_url[5:].split('/', 1)
695
  if len(path_parts) != 2:
696
  continue
697
  bucket_name, file_path = path_parts
698
  bucket = storage.bucket(bucket_name)
699
  else:
700
+ # Extract file path from HTTPS URL
701
  if '/o/' not in image_url or '?' not in image_url:
702
  continue
703
  file_path = image_url.split('/o/')[1].split('?')[0]
704
  file_path = urllib.parse.unquote(file_path)
705
+
706
+ # Download the image to a temporary file
 
 
707
  with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as temp_file:
708
  temp_img_path = temp_file.name
709
+
710
+ # Download the image
711
+ blob = bucket.blob(file_path)
712
  blob.download_to_filename(temp_img_path)
713
+
714
  # Calculate appropriate image width and place it on the page
715
+ page_width = pdf.w - 2 * pdf.l_margin
716
+ image_width = min(page_width - 20, 160) # Keep some margin
717
+ pdf.image(temp_img_path, x=(pdf.w - image_width) / 2, w=image_width)
718
+
719
+ # Clean up temporary file
720
  os.unlink(temp_img_path)
721
  except Exception as img_error:
722
  print(f"PDF image error: {str(img_error)}")
 
747
  image_url = section_obj.get("image_url", "")
748
  if image_url:
749
  try:
750
+ # Initialize Firebase Storage bucket
751
+ bucket = storage.bucket()
752
+
753
+ # Handle both gs:// and HTTPS URLs
754
  if image_url.startswith('gs://'):
755
  path_parts = image_url[5:].split('/', 1)
756
  if len(path_parts) != 2:
 
762
  continue
763
  file_path = image_url.split('/o/')[1].split('?')[0]
764
  file_path = urllib.parse.unquote(file_path)
765
+
766
+ # Download the image
767
  blob = bucket.blob(file_path)
768
  image_data = blob.download_as_bytes()
769
+
770
+ # Determine file extension
771
  ext = os.path.splitext(blob.name)[1].lower() or '.jpg'
772
  zipf.writestr(f"image_section_{idx + 1}{ext}", image_data)
773
  except Exception as img_error:
 
777
  audio_url = section_obj.get("audio_url", "")
778
  if audio_url:
779
  try:
780
+ # Initialize Firebase Storage bucket
781
+ bucket = storage.bucket()
782
+
783
+ # Handle both gs:// and HTTPS URLs
784
  if audio_url.startswith('gs://'):
785
  path_parts = audio_url[5:].split('/', 1)
786
  if len(path_parts) != 2:
 
792
  continue
793
  file_path = audio_url.split('/o/')[1].split('?')[0]
794
  file_path = urllib.parse.unquote(file_path)
795
+
796
+ # Download the audio
797
  blob = bucket.blob(file_path)
798
  audio_data = blob.download_as_bytes()
799
+
800
+ # Determine file extension
801
  ext = os.path.splitext(blob.name)[1].lower() or '.mp3'
802
  zipf.writestr(f"audio_section_{idx + 1}{ext}", audio_data)
803
  except Exception as audio_error: