Added download history
This commit is contained in:
+31
-2
@@ -1,8 +1,9 @@
|
|||||||
from fastapi import FastAPI, Request, Form
|
from fastapi import FastAPI, Request, Form
|
||||||
from fastapi.responses import HTMLResponse, FileResponse
|
from fastapi.responses import HTMLResponse, FileResponse, JSONResponse
|
||||||
from fastapi.templating import Jinja2Templates
|
from fastapi.templating import Jinja2Templates
|
||||||
import os
|
import os
|
||||||
import uuid
|
import json
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from app.downloader import download_video
|
from app.downloader import download_video
|
||||||
|
|
||||||
@@ -11,7 +12,14 @@ app = FastAPI()
|
|||||||
templates = Jinja2Templates(directory="app/templates")
|
templates = Jinja2Templates(directory="app/templates")
|
||||||
|
|
||||||
DOWNLOAD_DIR = "downloads"
|
DOWNLOAD_DIR = "downloads"
|
||||||
|
HISTORY_FILE = "history.json"
|
||||||
|
|
||||||
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
||||||
|
os.makedirs("data", exist_ok=True)
|
||||||
|
|
||||||
|
if not os.path.exists(HISTORY_FILE):
|
||||||
|
with open(HISTORY_FILE, "w") as f:
|
||||||
|
json.dump([], f)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/", response_class=HTMLResponse)
|
@app.get("/", response_class=HTMLResponse)
|
||||||
@@ -22,9 +30,30 @@ async def home(request: Request):
|
|||||||
@app.post("/download")
|
@app.post("/download")
|
||||||
async def download(url: str = Form(...), mode: str = Form(...)):
|
async def download(url: str = Form(...), mode: str = Form(...)):
|
||||||
path = download_video(url, mode)
|
path = download_video(url, mode)
|
||||||
|
|
||||||
|
# salva nella cronologia
|
||||||
|
with open(HISTORY_FILE, "r") as f:
|
||||||
|
history = json.load(f)
|
||||||
|
|
||||||
|
history.append({
|
||||||
|
"url": url,
|
||||||
|
"mode": mode,
|
||||||
|
"path": path,
|
||||||
|
"time": datetime.now().strftime("%Y-%m-%d %H:%M")
|
||||||
|
})
|
||||||
|
|
||||||
|
with open(HISTORY_FILE, "w") as f:
|
||||||
|
json.dump(history, f, indent=2)
|
||||||
|
|
||||||
return {"path": path}
|
return {"path": path}
|
||||||
|
|
||||||
|
|
||||||
|
@app.get("/history")
|
||||||
|
async def history():
|
||||||
|
with open(HISTORY_FILE) as f:
|
||||||
|
return JSONResponse(json.load(f))
|
||||||
|
|
||||||
|
|
||||||
@app.get("/file")
|
@app.get("/file")
|
||||||
async def file(path: str):
|
async def file(path: str):
|
||||||
return FileResponse(path)
|
return FileResponse(path)
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ Download
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="mt-6">
|
||||||
|
<h2 class="text-lg font-semibold mb-2">History</h2>
|
||||||
|
<div id="history" class="text-sm space-y-1"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
||||||
document.getElementById("downloadBtn").addEventListener("click", async () => {
|
document.getElementById("downloadBtn").addEventListener("click", async () => {
|
||||||
@@ -78,6 +83,31 @@ status.innerHTML = "Error: check logs"
|
|||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
|
async function loadHistory(){
|
||||||
|
|
||||||
|
const res = await fetch("/history")
|
||||||
|
const data = await res.json()
|
||||||
|
|
||||||
|
const box = document.getElementById("history")
|
||||||
|
box.innerHTML = ""
|
||||||
|
|
||||||
|
data.reverse().forEach(item => {
|
||||||
|
|
||||||
|
box.innerHTML += `
|
||||||
|
<div class="bg-gray-700 p-2 rounded">
|
||||||
|
${item.time} - ${item.mode}<br>
|
||||||
|
<a href="/file?path=${item.path}" class="text-green-400 underline">
|
||||||
|
Download
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
`
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
loadHistory()
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
Reference in New Issue
Block a user