Spaces:
Running
Running
File size: 4,436 Bytes
f02fba9 e5343b2 f02fba9 e5343b2 f02fba9 e5343b2 f02fba9 e5343b2 f02fba9 e5343b2 f02fba9 |
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
import os, shutil, zipfile, threading, time
from flask import Flask, request, render_template_string
import gdown
from huggingface_hub import HfApi, login, upload_folder
# Environment variables (set them in HF Spaces settings)
FOLDER_URL = os.getenv("FOLDER_URL")
REPO_ID = os.getenv("REPO_ID")
TOKEN = os.getenv("HF_TOKEN")
DOWNLOAD_DIR = "/tmp/backups"
EXTRACT_DIR = "/tmp/extracted_backups"
# Global state
last_backup_time = "Never"
schedule_interval = 0
app = Flask(__name__)
# Backup logic
def run_backup():
global last_backup_time
try:
shutil.rmtree(DOWNLOAD_DIR, ignore_errors=True)
shutil.rmtree(EXTRACT_DIR, ignore_errors=True)
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
os.makedirs(EXTRACT_DIR, exist_ok=True)
gdown.download_folder(
url=FOLDER_URL,
output=DOWNLOAD_DIR,
use_cookies=False,
quiet=False
)
for root, _, files in os.walk(DOWNLOAD_DIR):
for f in files:
if f.endswith(".zip"):
zp = os.path.join(root, f)
with zipfile.ZipFile(zp) as z:
z.extractall(EXTRACT_DIR)
bad = os.path.join(EXTRACT_DIR, "world_nither")
good = os.path.join(EXTRACT_DIR, "world_nether")
if os.path.exists(bad) and not os.path.exists(good):
os.rename(bad, good)
login(token=TOKEN)
api = HfApi()
try:
api.delete_repo(repo_id=REPO_ID, repo_type="dataset")
except Exception as err:
print("delete skipped:", err)
api.create_repo(repo_id=REPO_ID, repo_type="dataset", private=False, exist_ok=True)
subfolders = {
"world": os.path.join(EXTRACT_DIR, "world"),
"world_nether": os.path.join(EXTRACT_DIR, "world_nether"),
"world_the_end": os.path.join(EXTRACT_DIR, "world_the_end"),
"plugins": os.path.join(EXTRACT_DIR, "plugins")
}
for name, path in subfolders.items():
if os.path.exists(path):
upload_folder(
repo_id=REPO_ID,
folder_path=path,
repo_type="dataset",
token=TOKEN,
path_in_repo=name,
commit_message="add " + name
)
last_backup_time = time.ctime()
return "Backup completed"
except Exception as e:
return f"Error: {str(e)}"
# Background timer
def schedule_loop():
while True:
if schedule_interval > 0:
run_backup()
time.sleep(schedule_interval * 60)
else:
time.sleep(10)
# Start scheduler thread
threading.Thread(target=schedule_loop, daemon=True).start()
# HTML template
HTML = """
<!DOCTYPE html>
<html>
<head>
<title>Minecraft Backup Panel</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: sans-serif; padding: 20px; max-width: 600px; margin: auto; }
h2 { font-size: 24px; }
input, button { width: 100%; padding: 10px; margin: 8px 0; font-size: 16px; }
.box { background: #f5f5f5; padding: 15px; border-radius: 8px; margin-top: 20px; }
</style>
</head>
<body>
<h2>Minecraft Backup Controller</h2>
<form method="post">
<label>Set interval (minutes)</label>
<input type="number" name="interval" value="{{ interval }}" min="1">
<button type="submit">Set Timer</button>
</form>
<form method="post">
<input type="hidden" name="manual_run" value="1">
<button type="submit">Run Backup Now</button>
</form>
<div class="box">
<p><strong>Last Backup:</strong> {{ last_run }}</p>
<p><strong>Status:</strong> {{ status }}</p>
</div>
</body>
</html>
"""
@app.route("/", methods=["GET", "POST"])
def index():
global schedule_interval
status = ""
if request.method == "POST":
if "manual_run" in request.form:
status = run_backup()
else:
try:
schedule_interval = int(request.form.get("interval", "0"))
status = f"Timer set for every {schedule_interval} minutes"
except:
status = "Invalid interval"
return render_template_string(HTML, last_run=last_backup_time, interval=schedule_interval, status=status)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860)
|