Files
CapCutAPI/util.py

83 lines
2.8 KiB
Python
Raw Normal View History

import shutil
import subprocess
import json
import re
2025-07-18 11:31:16 +08:00
import os
import hashlib
import functools
import time
2025-08-11 21:49:11 +08:00
from settings.local import DRAFT_DOMAIN, PREVIEW_ROUTER, IS_CAPCUT_ENV
def hex_to_rgb(hex_color: str) -> tuple:
2025-07-13 14:46:21 +08:00
"""Convert hexadecimal color code to RGB tuple (range 0.0-1.0)"""
hex_color = hex_color.lstrip('#')
if len(hex_color) == 3:
2025-07-13 14:46:21 +08:00
hex_color = ''.join([c*2 for c in hex_color]) # Handle shorthand form (e.g. #fff)
try:
r = int(hex_color[0:2], 16) / 255.0
g = int(hex_color[2:4], 16) / 255.0
b = int(hex_color[4:6], 16) / 255.0
return (r, g, b)
except ValueError:
2025-07-13 14:46:21 +08:00
raise ValueError(f"Invalid hexadecimal color code: {hex_color}")
def is_windows_path(path):
2025-07-13 14:46:21 +08:00
"""Detect if the path is Windows style"""
# Check if it starts with a drive letter (e.g. C:\) or contains Windows style separators
return re.match(r'^[a-zA-Z]:\\|\\\\', path) is not None
2025-07-18 11:31:16 +08:00
def zip_draft(draft_id):
current_dir = os.path.dirname(os.path.abspath(__file__))
2025-07-13 14:46:21 +08:00
# Compress folder
2025-07-18 11:31:16 +08:00
zip_dir = os.path.join(current_dir, "tmp/zip")
os.makedirs(zip_dir, exist_ok=True)
zip_path = os.path.join(zip_dir, f"{draft_id}.zip")
shutil.make_archive(os.path.join(zip_dir, draft_id), 'zip', os.path.join(current_dir, draft_id))
return zip_path
def url_to_hash(url, length=16):
"""
2025-07-13 14:46:21 +08:00
Convert URL to a fixed-length hash string (without extension)
2025-07-13 14:46:21 +08:00
Parameters:
- url: Original URL string
- length: Length of the hash string (maximum 64, default 16)
2025-07-13 14:46:21 +08:00
Returns:
- Hash string (e.g.: 3a7f9e7d9a1b4e2d)
"""
2025-07-13 14:46:21 +08:00
# Ensure URL is bytes type
url_bytes = url.encode('utf-8')
2025-07-13 14:46:21 +08:00
# Use SHA-256 to generate hash (secure and highly unique)
hash_object = hashlib.sha256(url_bytes)
2025-07-13 14:46:21 +08:00
# Truncate to specified length of hexadecimal string
return hash_object.hexdigest()[:length]
def timing_decorator(func_name):
2025-07-13 14:46:21 +08:00
"""Decorator: Used to monitor function execution time"""
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.time()
2025-07-13 14:46:21 +08:00
print(f"[{func_name}] Starting execution...")
try:
result = func(*args, **kwargs)
end_time = time.time()
duration = end_time - start_time
2025-07-13 14:46:21 +08:00
print(f"[{func_name}] Execution completed, time taken: {duration:.3f} seconds")
return result
except Exception as e:
end_time = time.time()
duration = end_time - start_time
2025-07-13 14:46:21 +08:00
print(f"[{func_name}] Execution failed, time taken: {duration:.3f} seconds, error: {e}")
raise
return wrapper
return decorator
def generate_draft_url(draft_id):
2025-08-10 15:22:49 +08:00
return f"{DRAFT_DOMAIN}{PREVIEW_ROUTER}?draft_id={draft_id}&is_capcut={1 if IS_CAPCUT_ENV else 0}"