Add QoL features
This commit is contained in:
+52
-17
@@ -5,7 +5,8 @@ from fastapi import Depends, FastAPI, HTTPException, status
|
||||
from fastapi.security import APIKeyHeader
|
||||
|
||||
import models
|
||||
from utils import roblox_service
|
||||
from utils import roblox_service, hashing, discord
|
||||
import database
|
||||
|
||||
load_dotenv()
|
||||
|
||||
@@ -42,26 +43,60 @@ async def get_asset_info(asset_id: int, _: str = Depends(verify_api_key)):
|
||||
return final_dict
|
||||
|
||||
|
||||
# Global dictionary to store locks per image hash
|
||||
upload_locks: dict[str, asyncio.Lock] = {}
|
||||
|
||||
@app.post("/create/")
|
||||
async def reupload_asset(asset_id: int, _: str = Depends(verify_api_key)):
|
||||
asset = await roblox_service.asset_from_id(asset_id)
|
||||
if isinstance(asset, models.ClothingAsset):
|
||||
print("clothing asset found")
|
||||
print(f"Clothing asset found: {asset.name}")
|
||||
image = await roblox_service.fetch_clothing_image(asset)
|
||||
uploaded = await roblox_service.upload_clothing_image(
|
||||
image,
|
||||
asset.name,
|
||||
asset.description,
|
||||
asset.asset_type,
|
||||
models.RbxCreator(int(TARGET), "Upload_Group", "Group"),
|
||||
)
|
||||
new_asset_id = uploaded.get("asset_id")
|
||||
if new_asset_id:
|
||||
onsale = await roblox_service.onsale_asset(
|
||||
new_asset_id,
|
||||
|
||||
# Check for duplicates using hash
|
||||
image_hash = hashing.get_image_hash(image)
|
||||
|
||||
# Get or create a lock for this specific hash
|
||||
if image_hash not in upload_locks:
|
||||
upload_locks[image_hash] = asyncio.Lock()
|
||||
|
||||
async with upload_locks[image_hash]:
|
||||
# Double-check database inside the lock
|
||||
existing_new_id = database.get_uploaded_asset(image_hash)
|
||||
|
||||
if existing_new_id:
|
||||
print(f"Asset already uploaded (hash match): {existing_new_id}")
|
||||
return {"uploaded": {"asset_id": existing_new_id}}
|
||||
|
||||
# Prepare description with original URL
|
||||
original_url = f"https://www.roblox.com/catalog/{asset_id}"
|
||||
new_description = f"{asset.description}\n\nOriginal: {original_url}"
|
||||
|
||||
uploaded = await roblox_service.upload_clothing_image(
|
||||
image,
|
||||
asset.name,
|
||||
asset.description,
|
||||
int(TARGET),
|
||||
new_description,
|
||||
asset.asset_type,
|
||||
models.RbxCreator(int(TARGET), "Upload_Group", "Group"),
|
||||
)
|
||||
return {"uploaded": uploaded}
|
||||
return uploaded
|
||||
new_asset_id = uploaded.get("asset_id")
|
||||
if new_asset_id:
|
||||
# Save to database
|
||||
database.save_uploaded_asset(image_hash, asset_id, new_asset_id)
|
||||
|
||||
onsale = await roblox_service.onsale_asset(
|
||||
new_asset_id,
|
||||
asset.name,
|
||||
new_description,
|
||||
int(TARGET),
|
||||
)
|
||||
|
||||
# Send Discord notification
|
||||
asset_type_name = "Shirt" if asset.asset_type == models.RbxAssetType.SHIRT else "Pants"
|
||||
await discord.send_upload_webhook(
|
||||
asset.name, asset_id, new_asset_id, asset_type_name
|
||||
)
|
||||
|
||||
return {"uploaded": uploaded}
|
||||
return uploaded
|
||||
raise HTTPException(status_code=400, detail="Asset is not a clothing asset.")
|
||||
|
||||
Reference in New Issue
Block a user