Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,308 @@
|
|
| 1 |
-
import requests
|
| 2 |
from fastapi import FastAPI
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# Initialize FastAPI app
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
async def root():
|
| 13 |
-
return {"message": "FastAPI is running!"}
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
# 🚀 RUN
|
| 38 |
# =========================
|
| 39 |
if __name__ == "__main__":
|
|
|
|
| 40 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import StreamingResponse, FileResponse
|
| 3 |
+
from fastapi.responses import PlainTextResponse
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from discord_webhook import DiscordWebhook
|
| 8 |
+
import httpx
|
| 9 |
+
import urllib3
|
| 10 |
+
import json
|
| 11 |
+
import base64
|
| 12 |
+
import secrets
|
| 13 |
+
from fastapi.responses import PlainTextResponse, RedirectResponse
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
urllib3.disable_warnings(
|
| 25 |
+
urllib3.exceptions.InsecureRequestWarning
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
|
|
|
|
| 33 |
app = FastAPI()
|
| 34 |
|
| 35 |
+
# =========================
|
| 36 |
+
# DISCORD WEBHOOK
|
| 37 |
+
# =========================
|
| 38 |
+
|
| 39 |
+
BOT_TOKEN = "MTUwMTk1OTI1Njk4MTc2NjE4NA.Gcshyg.YHzk_9k39AVEgLq2IHXtKeLHYMl6AT7GbnUpwE"
|
| 40 |
+
|
| 41 |
+
CHANNEL_ID = "1470082028518113342"
|
| 42 |
+
DISCORD_WEBHOOK = "https://discord.com/api/webhooks/1511459066063618203/XhA1ctRlD-iRNukVQoXVWJ6wU315BAdvJng8-gfpJ2Yx14nxoGGE1X8Ca6bqo7MkGk2d"
|
| 43 |
+
MINESKIN_TOKEN = "msk_SBLac6yF_ApAeLAIAUGjlX9FQkiZ2itwwSmuzQ3sBM8KryyVHcD1HrW4KqPCcRGgEfIFPAVSY"
|
| 44 |
+
short_images = {}
|
| 45 |
+
# =========================
|
| 46 |
+
# DOWNLOAD SKIN
|
| 47 |
+
# =========================
|
| 48 |
|
| 49 |
+
def download_skin(url: str):
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
headers = {
|
| 52 |
+
"User-Agent": (
|
| 53 |
+
"Mozilla/5.0 "
|
| 54 |
+
"(Windows NT 10.0; Win64; x64) "
|
| 55 |
+
"AppleWebKit/537.36 "
|
| 56 |
+
"(KHTML, like Gecko) "
|
| 57 |
+
"Chrome/122.0 Safari/537.36"
|
| 58 |
+
)
|
| 59 |
}
|
| 60 |
+
|
| 61 |
+
response = requests.get(
|
| 62 |
+
url,
|
| 63 |
+
headers=headers
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
response.raise_for_status()
|
| 67 |
+
|
| 68 |
+
return Image.open(
|
| 69 |
+
BytesIO(response.content)
|
| 70 |
+
).convert("RGBA")
|
| 71 |
+
|
| 72 |
+
#=========================
|
| 73 |
+
#decode Base64
|
| 74 |
+
#=========================
|
| 75 |
+
def parse_skin_input(value: str):
|
| 76 |
+
|
| 77 |
+
# обычный URL
|
| 78 |
+
if value.startswith("http://") or value.startswith("https://"):
|
| 79 |
+
return value
|
| 80 |
+
|
| 81 |
+
try:
|
| 82 |
+
|
| 83 |
+
# декодируем base64
|
| 84 |
+
decoded = base64.b64decode(value).decode("utf-8")
|
| 85 |
+
|
| 86 |
+
data = json.loads(decoded)
|
| 87 |
+
|
| 88 |
+
# достаём texture url
|
| 89 |
+
return data["textures"]["SKIN"]["url"]
|
| 90 |
+
|
| 91 |
+
except Exception:
|
| 92 |
+
return value
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# =========================
|
| 97 |
+
# MERGE FUNCTION
|
| 98 |
+
# =========================
|
| 99 |
+
|
| 100 |
+
def create_merged_skin(head_skin_url, body_skin_url):
|
| 101 |
+
|
| 102 |
+
head_skin_url = parse_skin_input(head_skin_url)
|
| 103 |
+
body_skin_url = parse_skin_input(body_skin_url)
|
| 104 |
+
|
| 105 |
+
skin_head = download_skin(head_skin_url)
|
| 106 |
+
skin_body = download_skin(body_skin_url)
|
| 107 |
+
|
| 108 |
+
result = skin_body.copy()
|
| 109 |
+
|
| 110 |
+
# полностью заменяем область головы
|
| 111 |
+
head_region = skin_head.crop((0, 0, 64, 16))
|
| 112 |
+
|
| 113 |
+
result.paste(
|
| 114 |
+
head_region,
|
| 115 |
+
(0, 0)
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
# SAVE TEST FILE
|
| 119 |
+
result.save("testingmerge.png")
|
| 120 |
+
|
| 121 |
+
return result
|
| 122 |
+
|
| 123 |
+
# =========================
|
| 124 |
+
# NORMAL MERGE PNG
|
| 125 |
+
# =========================
|
| 126 |
+
|
| 127 |
+
@app.get("/merge.png")
|
| 128 |
+
def merge_png(head_skin: str, body_skin: str):
|
| 129 |
+
|
| 130 |
+
result = create_merged_skin(
|
| 131 |
+
head_skin,
|
| 132 |
+
body_skin
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
output = BytesIO()
|
| 136 |
+
|
| 137 |
+
result.save(output, format="PNG")
|
| 138 |
+
|
| 139 |
+
output.seek(0)
|
| 140 |
+
|
| 141 |
+
return StreamingResponse(
|
| 142 |
+
output,
|
| 143 |
+
media_type="image/png"
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# =========================
|
| 147 |
+
# STATIC TEST FILE
|
| 148 |
+
# =========================
|
| 149 |
+
|
| 150 |
+
@app.api_route(
|
| 151 |
+
"/testingmerge.png",
|
| 152 |
+
methods=["GET", "HEAD"]
|
| 153 |
+
)
|
| 154 |
+
def testing_merge():
|
| 155 |
+
|
| 156 |
+
return FileResponse(
|
| 157 |
+
path="testingmerge.png",
|
| 158 |
+
media_type="image/png"
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
# =========================
|
| 162 |
+
# DISCORD UPLOAD
|
| 163 |
+
# =========================
|
| 164 |
+
@app.get("/temp.png")
|
| 165 |
+
def temp_png(head_skin: str, body_skin: str):
|
| 166 |
+
|
| 167 |
+
img = create_merged_skin(head_skin, body_skin)
|
| 168 |
+
|
| 169 |
+
buffer = BytesIO()
|
| 170 |
+
img.save(buffer, format="PNG")
|
| 171 |
+
buffer.seek(0)
|
| 172 |
+
|
| 173 |
+
return StreamingResponse(buffer, media_type="image/png")
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
# =========================
|
| 177 |
+
# MINESKIN UPLOAD
|
| 178 |
+
# =========================
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
# =========================
|
| 183 |
+
# MAIN ENDPOINT
|
| 184 |
+
# =========================
|
| 185 |
+
|
| 186 |
+
# =========================
|
| 187 |
+
# MINESKIN ENDPOINT (ВАШ ОФИЦИАЛЬНЫЙ ВАРИАНТ)
|
| 188 |
+
# =========================
|
| 189 |
+
|
| 190 |
+
# =========================
|
| 191 |
+
# ENDPOINT
|
| 192 |
+
# =========================
|
| 193 |
+
|
| 194 |
+
@app.get("/merge-to-mineskin")
|
| 195 |
+
def merge_to_mineskin(head_skin: str, body_skin: str):
|
| 196 |
+
|
| 197 |
+
try:
|
| 198 |
+
|
| 199 |
+
# создаём merged skin
|
| 200 |
+
create_merged_skin(
|
| 201 |
+
head_skin,
|
| 202 |
+
body_skin
|
| 203 |
+
)
|
| 204 |
+
|
| 205 |
+
# короткий URL
|
| 206 |
+
generated_url = "https://dedlepexa-discordapiuse.hf.space/testingmerge.png"
|
| 207 |
+
|
| 208 |
+
# MineSkin
|
| 209 |
+
url = "https://api.mineskin.org/v2/generate"
|
| 210 |
+
|
| 211 |
+
payload = json.dumps({
|
| 212 |
+
"variant": "classic",
|
| 213 |
+
"name": "My Example Skin",
|
| 214 |
+
"visibility": "public",
|
| 215 |
+
"url": generated_url
|
| 216 |
+
})
|
| 217 |
+
|
| 218 |
+
headers = {
|
| 219 |
+
'Content-Type': 'application/json',
|
| 220 |
+
'Accept': 'application/json',
|
| 221 |
+
'User-Agent': 'lol',
|
| 222 |
+
'Authorization': f'Bearer {MINESKIN_TOKEN}'
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
+
response = requests.request(
|
| 226 |
+
"POST",
|
| 227 |
+
url,
|
| 228 |
+
headers=headers,
|
| 229 |
+
data=payload
|
| 230 |
+
)
|
| 231 |
+
|
| 232 |
+
return {
|
| 233 |
+
"success": True,
|
| 234 |
+
"generated_url": generated_url,
|
| 235 |
+
"mineskin_response": response.json()
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
except Exception as e:
|
| 239 |
+
|
| 240 |
+
return {
|
| 241 |
+
"success": False,
|
| 242 |
+
"error": str(e)
|
| 243 |
+
}
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
# ОТПРАВКА В ДС
|
| 249 |
+
@app.post("/upload-and-get-link", response_model=SkinResponse)
|
| 250 |
+
async def upload_and_get_link(request: SkinRequest):
|
| 251 |
+
"""
|
| 252 |
+
Create merged skin, upload to Discord, and return the CDN link
|
| 253 |
|
| 254 |
+
Supports:
|
| 255 |
+
- Direct URLs (https://...)
|
| 256 |
+
- Base64 encoded Minecraft profile data
|
| 257 |
+
- Minecraft textures URLs
|
| 258 |
+
"""
|
| 259 |
try:
|
| 260 |
+
print(f"Processing head_skin input...")
|
| 261 |
+
print(f"Processing body_skin input...")
|
| 262 |
+
|
| 263 |
+
# Parse inputs
|
| 264 |
+
head_url = parse_skin_input(request.head_skin)
|
| 265 |
+
body_url = parse_skin_input(request.body_skin)
|
| 266 |
+
|
| 267 |
+
print(f"Parsed head URL: {head_url}")
|
| 268 |
+
print(f"Parsed body URL: {body_url}")
|
| 269 |
+
|
| 270 |
+
# Create merged skin
|
| 271 |
+
img = create_merged_skin(head_url, body_url)
|
| 272 |
+
|
| 273 |
+
# Save to bytes
|
| 274 |
+
buffer = BytesIO()
|
| 275 |
+
img.save(buffer, format="PNG")
|
| 276 |
+
buffer.seek(0)
|
| 277 |
+
image_bytes = buffer.getvalue()
|
| 278 |
+
|
| 279 |
+
# Upload to Discord
|
| 280 |
+
discord_url = upload_to_discord(image_bytes)
|
| 281 |
+
|
| 282 |
+
return {
|
| 283 |
+
"success": True,
|
| 284 |
+
"url": discord_url
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
except Exception as e:
|
| 288 |
+
print(f"Error in upload_and_get_link: {e}")
|
| 289 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 290 |
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
# =========================
|
| 304 |
# 🚀 RUN
|
| 305 |
# =========================
|
| 306 |
if __name__ == "__main__":
|
| 307 |
+
import uvicorn
|
| 308 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|