Change Cart disposition
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import io
|
||||
import math
|
||||
import os
|
||||
from flask import Blueprint, jsonify, request, session
|
||||
import zipfile
|
||||
|
||||
import requests
|
||||
from flask import Blueprint, jsonify, request, session, send_file
|
||||
from werkzeug.utils import secure_filename
|
||||
from src.services.tmdb_service import TMDBService
|
||||
from src.services.image_service import ImageService
|
||||
@@ -149,3 +152,48 @@ def delete_csv():
|
||||
except Exception as e:
|
||||
print(f"Error in delete_csv: {str(e)}")
|
||||
return jsonify({'error': 'An error occurred while deleting the CSV file'}), 500
|
||||
|
||||
|
||||
@api_bp.route('/download-posters', methods=['POST'])
|
||||
def download_posters():
|
||||
user_id = session.get('user_id')
|
||||
|
||||
if user_id not in user_csvs:
|
||||
return jsonify({'error': 'No CSV file found. Please upload a CSV file.'}), 404
|
||||
|
||||
csv_content = user_csvs[user_id]
|
||||
|
||||
try:
|
||||
zip_buffer = io.BytesIO()
|
||||
|
||||
with zipfile.ZipFile(zip_buffer, 'w', zipfile.ZIP_DEFLATED) as zip_file:
|
||||
if isinstance(csv_content, bytes):
|
||||
df = pd.read_csv(io.StringIO(csv_content.decode('utf-8')))
|
||||
else:
|
||||
df = pd.read_csv(io.StringIO(csv_content))
|
||||
|
||||
movies = df.to_dict('records')
|
||||
|
||||
for movie in movies:
|
||||
poster_url = get_poster_url(movie['Name'], movie['Year'])
|
||||
if poster_url:
|
||||
try:
|
||||
response = requests.get(poster_url)
|
||||
if response.status_code == 200:
|
||||
zip_file.writestr(f"{movie['Name']}_{movie['Year']}.jpg", response.content)
|
||||
except Exception as e:
|
||||
print(f"Error downloading poster for {movie['Name']}: {str(e)}")
|
||||
continue
|
||||
|
||||
zip_buffer.seek(0)
|
||||
|
||||
return send_file(
|
||||
zip_buffer,
|
||||
mimetype='application/zip',
|
||||
as_attachment=True,
|
||||
download_name='posters.zip'
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
print(f"Error in download_posters: {str(e)}")
|
||||
return jsonify({'error': 'An error occurred while fetching movie posters'}), 500
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
import re
|
||||
import os
|
||||
|
||||
|
||||
def clean_filename(filename):
|
||||
return re.sub(r'[\\/*?:"<>|]', '_', filename)
|
||||
|
||||
|
||||
def ensure_directory(directory):
|
||||
os.makedirs(directory, exist_ok=True)
|
||||
os.makedirs(directory, exist_ok=True)
|
||||
|
||||
@@ -2,6 +2,7 @@ import requests
|
||||
from PIL import Image
|
||||
import io
|
||||
|
||||
|
||||
class ImageService:
|
||||
def download_image(self, image_url, save_path=None):
|
||||
response = requests.get(image_url, stream=True)
|
||||
@@ -10,4 +11,4 @@ class ImageService:
|
||||
if save_path:
|
||||
image.save(save_path)
|
||||
return image
|
||||
return None
|
||||
return None
|
||||
|
||||
Reference in New Issue
Block a user