Spaces:
Runtime error
Runtime error
File size: 2,863 Bytes
37274e8 dda581f d27126c dda581f d27126c 3fb9503 d27126c dda581f d27126c dda581f d27126c dda581f b8f1ae4 3fb9503 b8f1ae4 dda581f 3fb9503 dda581f d27126c 98f430e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | import os
from flask import Flask, jsonify, send_from_directory, request, render_template_string
from werkzeug.utils import secure_filename
from database import db
from models import Video
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///metavideo.db"
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
UPLOAD_FOLDER = "uploads"
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
db.init_app(app)
# -------------------
# HTML САЙТ (как YouTube простая версия)
# -------------------
HTML = """
<!DOCTYPE html>
<html>
<head>
<title>Meta Video</title>
</head>
<body>
<h1>🎥 Meta Video</h1>
<h2>📤 Upload</h2>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="text" name="title" placeholder="Title"><br><br>
<input type="file" name="file"><br><br>
<button type="submit">Upload</button>
</form>
<h2>📺 Videos</h2>
<div id="videos"></div>
<script>
fetch("/api/feed")
.then(r => r.json())
.then(data => {
let html = "";
data.forEach(v => {
html += `
<div style="margin-bottom:20px;">
<h3>${v.title}</h3>
<video width="300" controls>
<source src="${v.url}" type="video/mp4">
</video>
</div>
`;
});
document.getElementById("videos").innerHTML = html;
});
</script>
</body>
</html>
"""
# -------------------
# ГЛАВНАЯ СТРАНИЦА
# -------------------
@app.route("/")
def home():
return render_template_string(HTML)
# -------------------
# ЗАГРУЗКА ВИДЕО
# -------------------
@app.route("/upload", methods=["POST"])
def upload():
file = request.files["file"]
title = request.form.get("title", "Untitled")
filename = secure_filename(file.filename)
path = os.path.join(UPLOAD_FOLDER, filename)
file.save(path)
video = Video(title=title, filename=filename)
db.session.add(video)
db.session.commit()
return "Uploaded! Go back"
# -------------------
# FEED (список видео)
# -------------------
@app.route("/api/feed")
def feed():
videos = Video.query.all()
return jsonify([
{
"title": v.title,
"url": f"/video/{v.filename}"
}
for v in videos
])
# -------------------
# СТРИМ ВИДЕО
# -------------------
@app.route("/video/<filename>")
def video(filename):
return send_from_directory(UPLOAD_FOLDER, filename)
# -------------------
# START
# -------------------
if __name__ == "__main__":
with app.app_context():
db.create_all()
port = int(os.environ.get("PORT", 7860))
app.run(host="0.0.0.0", port=port) |