from flask import Flask, render_template, send_from_directory, jsonify, request
import os

app = Flask(__name__)
media_dir = 'storysaver'
MEDIA_PER_PAGE = 20

def get_media_files(start, count):
    media_files = []
    for root, dirs, files in os.walk(media_dir):
        for filename in files:
            if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.mp4', '.mkv', '.mov')):
                file_path = os.path.relpath(os.path.join(root, filename), media_dir)
                media_files.append(file_path)
    return media_files[start:start + count]

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/media/<path:filename>')
def media(filename):
    return send_from_directory(media_dir, filename)

@app.route('/load-more')
def load_more():
    page = int(request.args.get('page', 0))
    media_files = get_media_files(page * MEDIA_PER_PAGE, MEDIA_PER_PAGE)
    return jsonify(media_files)

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)
