diff --git a/__pycache__/config.cpython-312.pyc b/__pycache__/config.cpython-312.pyc new file mode 100644 index 0000000..e86d509 Binary files /dev/null and b/__pycache__/config.cpython-312.pyc differ diff --git a/config.py b/config.py new file mode 100644 index 0000000..c1c59ee --- /dev/null +++ b/config.py @@ -0,0 +1,25 @@ +import os + +class Config: + # Configuration générale + SECRET_KEY = os.environ.get('SECRET_KEY', 'dev_key') + DEBUG = False + TESTING = False + REDIS_URL = os.environ.get("REDIS_URL", "redis://localhost:6379") + +class DevelopmentConfig(Config): + DEBUG = True + # Configuration pour le développement local + REDIS_URL = "redis://localhost:6379" + +class ProductionConfig(Config): + # Configuration pour la production (Render, etc.) + REDIS_URL = os.environ.get("REDIS_URL") + # Peut désactiver le debug mode pour la prod + DEBUG = False + +config = { + 'development': DevelopmentConfig, + 'production': ProductionConfig, + 'default': DevelopmentConfig +} diff --git a/data/diary.csv b/data/diary.csv deleted file mode 100644 index 496fac2..0000000 --- a/data/diary.csv +++ /dev/null @@ -1,66 +0,0 @@ -Watched Date,Name,Year,Letterboxd URI,Rating,Rewatch,Tags,Watched Date -2023-12-15,Ghost in the Shell,1995,https://boxd.it/5k8vT3,3.5,,,2023-12-13 -2024-01-03,Baby Driver,2017,https://boxd.it/5tDbaP,4.0,,,2024-01-02 -2024-01-04,The Shawshank Redemption,1994,https://boxd.it/5ugTgP,4.5,,,2024-01-03 -2024-01-05,Léon: The Professional,1994,https://boxd.it/5uRr6B,4.0,,,2024-01-04 -2024-01-06,Brothers,2009,https://boxd.it/5vuRQN,3.5,,,2024-01-05 -2024-01-07,"Crazy, Stupid, Love.",2011,https://boxd.it/5wgeFn,4.0,,,2024-01-06 -2024-01-08,There Will Be Blood,2007,https://boxd.it/5x4B37,4.5,,,2024-01-07 -2024-01-09,(500) Days of Summer,2009,https://boxd.it/5xDHe1,4.0,,,2024-01-08 -2024-01-20,Dallas Buyers Club,2013,https://boxd.it/5DrLPb,4.0,,,2024-01-19 -2024-01-22,Boogie Nights,1997,https://boxd.it/5EEjCN,4.0,,,2024-01-21 -2024-01-24,Saltburn,2023,https://boxd.it/5FxIYT,4.0,,,2024-01-23 -2024-01-25,Under the Skin,2013,https://boxd.it/5G0SG9,2.5,,,2024-01-24 -2024-01-26,Anatomy of a Fall,2023,https://boxd.it/5GuroR,4.0,,,2024-01-25 -2024-01-31,Poor Things,2023,https://boxd.it/5J4AI7,4.5,,,2024-01-30 -2024-02-01,In the Mood for Love,2000,https://boxd.it/5JweK7,4.5,,,2024-01-31 -2024-02-07,Black Swan,2010,https://boxd.it/5MhygD,4.0,,,2024-02-06 -2024-02-13,Rain Man,1988,https://boxd.it/5OXE5j,4.0,,,2024-02-12 -2024-02-20,The 40 Year Old Virgin,2005,https://boxd.it/5Sgvxj,3.5,,,2024-02-19 -2024-02-21,Captain Fantastic,2016,https://boxd.it/5SHgol,4.0,,,2024-02-20 -2024-02-23,The Prestige,2006,https://boxd.it/5TwaTp,4.5,,,2024-02-22 -2024-02-29,The Shining,1980,https://boxd.it/5WgsV9,4.0,,,2024-02-28 -2024-03-02,Dune: Part Two,2024,https://boxd.it/5X4Ep5,5.0,,,2024-03-01 -2024-03-12,Fantastic Mr. Fox,2009,https://boxd.it/622Xk5,4.5,,,2024-03-11 -2024-03-13,Past Lives,2023,https://boxd.it/62uP1F,4.5,,,2024-03-12 -2024-03-19,Little Women,2019,https://boxd.it/65jkNX,3.0,,,2024-03-18 -2024-03-21,The Perks of Being a Wallflower,2012,https://boxd.it/668ca1,4.0,,,2024-03-20 -2024-03-28,Amélie,2001,https://boxd.it/69lEoN,4.5,,,2024-03-27 -2024-03-29,Lost in Translation,2003,https://boxd.it/69Kn1n,4.0,,,2024-03-28 -2024-04-03,No Country for Old Men,2007,https://boxd.it/6clZZd,4.0,,,2024-04-02 -2024-04-05,Fargo,1996,https://boxd.it/6dadIv,3.5,,,2024-04-04 -2024-04-09,No Hard Feelings,2023,https://boxd.it/6f0cwL,2.5,,,2024-04-08 -2024-04-17,"OSS 117: Cairo, Nest of Spies",2006,https://boxd.it/6i7AOl,4.0,,,2024-04-16 -2024-04-20,The Neon Demon,2016,https://boxd.it/6j9IF5,3.0,,,2024-04-19 -2024-04-23,Taxi Driver,1976,https://boxd.it/6ktjeR,3.5,,,2024-04-22 -2024-04-30,The Theory of Everything,2014,https://boxd.it/6nlzHL,4.5,,,2024-04-29 -2024-05-09,Civil War,2024,https://boxd.it/6rbidl,4.5,,,2024-05-08 -2024-05-10,Scott Pilgrim vs. the World,2010,https://boxd.it/6rxEAl,4.0,,,2024-05-09 -2024-05-15,Asteroid City,2023,https://boxd.it/6txkqD,3.5,,,2024-05-14 -2024-05-22,Battle of the Sexes,2017,https://boxd.it/6wiCsp,3.5,,,2024-05-22 -2024-05-26,War Dogs,2016,https://boxd.it/6xS8YB,3.5,,,2024-05-25 -2024-05-28,The Creator,2023,https://boxd.it/6yPVwT,4.5,,,2024-05-27 -2024-05-30,Full Metal Jacket,1987,https://boxd.it/6zEwVV,4.0,,,2024-05-29 -2024-06-04,Dead Poets Society,1989,https://boxd.it/6BNjNv,3.5,,,2024-06-03 -2024-06-05,Perfect Days,2023,https://boxd.it/6C9zSb,4.0,,,2024-06-04 -2024-06-07,Burn After Reading,2008,https://boxd.it/6CQVG3,3.0,,,2024-06-06 -2024-06-12,Before Sunrise,1995,https://boxd.it/6EWJMD,4.0,,,2024-06-11 -2024-06-20,OSS 117: Lost in Rio,2009,https://boxd.it/6I1d3j,3.5,,,2024-06-19 -2024-06-23,Inside Out 2,2024,https://boxd.it/6J9mU7,4.0,,,2024-06-22 -2024-06-26,May December,2023,https://boxd.it/6KqGP5,2.5,,,2024-06-25 -2024-06-29,Ferrari,2023,https://boxd.it/6Lv8pV,3.0,,,2024-06-28 -2024-06-30,Sicario,2015,https://boxd.it/6LUq2b,4.0,,,2024-06-29 -2024-07-21,Gladiator,2000,https://boxd.it/6Vcoij,4.5,,,2024-07-20 -2024-07-22,Perfect Blue,1997,https://boxd.it/6VIhi1,4.0,,,2024-07-21 -2024-07-23,Paprika,2006,https://boxd.it/6WcVWb,4.5,,,2024-07-22 -2024-07-24,Dream Scenario,2023,https://boxd.it/6WCG0t,4.0,,,2024-07-23 -2024-07-29,Yes Man,2008,https://boxd.it/6ZbFWJ,3.0,,,2024-07-29 -2024-07-30,Deadpool & Wolverine,2024,https://boxd.it/6Zo8Sb,3.5,,,2024-07-29 -2024-07-31,Source Code,2011,https://boxd.it/6ZPH0H,3.5,,,2024-07-30 -2024-08-04,Almost Famous,2000,https://boxd.it/71w9tb,4.0,,,2024-08-03 -2024-08-05,The Master,2012,https://boxd.it/72e6pN,3.0,,,2024-08-05 -2024-08-10,Millennium Actress,2001,https://boxd.it/747e7V,4.0,,,2024-08-10 -2024-08-14,The King of Comedy,1982,https://boxd.it/75VeGl,4.5,,,2024-08-13 -2024-08-15,American Fiction,2023,https://boxd.it/76lZ2J,4.0,,,2024-08-14 -2024-08-18,The Diary of a Teenage Girl,2015,https://boxd.it/77HSDB,3.5,,,2024-08-17 -2024-08-20,Punch-Drunk Love,2002,https://boxd.it/78Mk2P,4.0,,,2024-08-19 diff --git a/main.py b/main.py index 1992ea9..cee0556 100644 --- a/main.py +++ b/main.py @@ -3,12 +3,15 @@ from flask import Flask from flask_cors import CORS from src.api.routes import api_bp from flask_sse import sse +from config import config def create_app(): app = Flask(__name__) CORS(app) - app.config["REDIS_URL"] = "redis://localhost:6379" + env = os.environ.get('FLASK_ENV', 'default') + app.config.from_object(config[env]) + app.register_blueprint(sse, url_prefix='/stream') app.register_blueprint(api_bp, url_prefix='/api') diff --git a/src/api/__pycache__/routes.cpython-312.pyc b/src/api/__pycache__/routes.cpython-312.pyc index a7e92a8..33f28c8 100644 Binary files a/src/api/__pycache__/routes.cpython-312.pyc and b/src/api/__pycache__/routes.cpython-312.pyc differ diff --git a/src/api/routes.py b/src/api/routes.py index 795a0bf..36b8497 100644 --- a/src/api/routes.py +++ b/src/api/routes.py @@ -1,6 +1,7 @@ +import io import math import os -from flask import Blueprint, jsonify, request +from flask import Blueprint, jsonify, request, session from werkzeug.utils import secure_filename from src.services.tmdb_service import TMDBService from src.services.image_service import ImageService @@ -12,10 +13,9 @@ api_bp = Blueprint('api', __name__) tmdb_service = TMDBService() image_service = ImageService() -UPLOAD_FOLDER = './data' ALLOWED_EXTENSIONS = {'csv'} -csv_file = os.path.join(UPLOAD_FOLDER, 'diary.csv') +user_csvs = {} @lru_cache(maxsize=1000) def get_poster_url(movie_name, movie_year): @@ -28,27 +28,36 @@ def get_poster_url(movie_name, movie_year): @api_bp.route('/movies', methods=['GET']) def get_movies(): + user_id = session.get('user_id') # Récupérer l'ID de l'utilisateur de la session + + if user_id not in user_csvs: + return jsonify({'movies': [], 'message': 'No movies found. Please upload a CSV file.'}), 200 + + # Lire le DataFrame à partir du cache + csv_content = user_csvs[user_id] + try: - if not os.path.exists(csv_file): - return jsonify({'movies': [], 'message': 'No movies found. Please upload a CSV file.'}), 200 + # Si csv_content est une chaîne, il n'est pas nécessaire de décoder + 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)) + # Reste de votre code pour la pagination et le traitement... page = int(request.args.get('page', 1)) limit = int(request.args.get('limit', 10)) - - df = pd.read_csv(csv_file) - + df = df.iloc[::-1].reset_index(drop=True) - total_movies = len(df) total_pages = math.ceil(total_movies / limit) - + start_index = (page - 1) * limit end_index = start_index + limit - + df_page = df.iloc[start_index:end_index] - + movies = df_page.to_dict('records') - + movies_with_posters = [ { 'id': start_index + i + 1, @@ -59,7 +68,7 @@ def get_movies(): } for i, movie in enumerate(movies) ] - + return jsonify({ 'movies': movies_with_posters, 'total': total_movies, @@ -88,25 +97,26 @@ def allowed_file(filename): @api_bp.route('/upload-csv', methods=['POST']) def upload_csv(): + user_id = session.get('user_id') + if 'file' not in request.files: return jsonify({'error': 'No file part'}), 400 file = request.files['file'] if file.filename == '': return jsonify({'error': 'No selected file'}), 400 if file and allowed_file(file.filename): - filename = secure_filename(file.filename) - file_path = os.path.join(UPLOAD_FOLDER, filename) - file.save(file_path) + content = file.read() + user_csvs[user_id] = content try: - df = pd.read_csv(file_path) + df = pd.read_csv(io.StringIO(content.decode('utf-8'))) df = df.rename(columns={ 'Date': 'Watched Date', 'Name': 'Name', 'Year': 'Year' }) - df.to_csv(csv_file, index=False) + user_csvs[user_id] = df.to_csv(index=False) return jsonify({'success': True, 'message': 'File uploaded and processed successfully'}), 200 except Exception as e: @@ -115,23 +125,27 @@ def upload_csv(): @api_bp.route('/check-csv', methods=['GET']) def check_csv(): + user_id = session.get('user_id') + try: - if os.path.exists(csv_file): + if user_id in user_csvs: return jsonify({'fileExists': True}), 200 else: return jsonify({'fileExists': False}), 200 except Exception as e: print(f"Error in check_csv: {str(e)}") return jsonify({'error': 'An error occurred while checking the CSV file'}), 500 - + @api_bp.route('/delete-csv', methods=['DELETE']) def delete_csv(): + user_id = session.get('user_id') + try: - if os.path.exists(csv_file): - os.remove(csv_file) + if user_id in user_csvs: + del user_csvs[user_id] return jsonify({'success': True, 'message': 'CSV file deleted successfully'}), 200 else: return jsonify({'error': 'CSV file not found'}), 404 except Exception as e: print(f"Error in delete_csv: {str(e)}") - return jsonify({'error': 'An error occurred while deleting the CSV file'}), 500 \ No newline at end of file + return jsonify({'error': 'An error occurred while deleting the CSV file'}), 500