Add delete method

This commit is contained in:
Kenwood 2025-08-08 13:21:02 -04:00
parent 639304f311
commit d338c881aa
1 changed files with 95 additions and 0 deletions

View File

@ -195,6 +195,10 @@ def append_post_tags(post_id, new_tags, danbooru_url, api_key, username, clear_t
# Get the current tags
current_tags = get_post_tags(post_id, danbooru_url, api_key, username)
# Handle case where get_post_tags returns None
if current_tags is None:
current_tags = []
# Ensure we can iterate the tags even if they're not a list
if not isinstance(new_tags, list):
new_tags = [new_tags]
@ -476,3 +480,94 @@ def get_autotagger_suggestions(post_id, api_url, api_key, username, min_score=95
logging.warning(f"Failed to get autotagger suggestions for post {post_id}")
logging.debug(response)
return []
def get_user_profile(danbooru_url, api_key, username):
"""
Get the user profile to check permissions and user level.
Args:
danbooru_url (str): The base URL of the booru
api_key (str): The API key for authentication
username (str): The username for authentication
Returns:
dict: User profile information or None if failed
"""
url = f"{danbooru_url}/profile.json"
params = {
"login": username,
"api_key": api_key,
}
try:
response = requests.get(url, params=params)
if response.status_code == 200:
return response.json()
else:
print(f"Failed to get user profile. Status code: {response.status_code}")
print(f"Response: {response.text}")
return None
except Exception as e:
print(f"Exception getting user profile: {e}")
return None
def delete_post(post_id, danbooru_url, api_key, username, reason=""):
"""
Deletes a post from the booru with an optional reason.
Args:
post_id (int): The ID of the post to delete
danbooru_url (str): The base URL of the booru
api_key (str): The API key for authentication
username (str): The username for authentication
reason (str): Optional reason for deletion
Returns:
bool: True if deletion was successful, False otherwise
"""
url = f"{danbooru_url}/posts/{post_id}.json"
print(f"Attempting to delete post {post_id} from {url}")
data = {
"commit": "Delete",
"post[move_favorites]": "true",
}
if reason:
data["post[reason]"] = reason
headers = {"Content-Type": "application/x-www-form-urlencoded"}
try:
response = requests.delete(url, auth=(username, api_key), data=data, headers=headers)
print(f"Response status code: {response.status_code}")
if response.status_code in [200, 204, 202]:
# Check if the post was actually marked as deleted
if response.status_code == 200:
try:
post_data = response.json()
if post_data.get("is_deleted", False):
print(f"Post {post_id} was successfully deleted")
return True
else:
print(f"Post {post_id} was not marked as deleted")
return False
except:
# If we can't parse JSON, assume success for 200 status
print(f"Post {post_id} deletion successful (status 200)")
return True
else:
# For 204/202, assume success
print(f"Post {post_id} deletion successful (status {response.status_code})")
return True
else:
print(f"Failed to delete post {post_id}. Status code: {response.status_code}")
if "ActiveRecord::RecordInvalid" in response.text:
print("Error: Post validation failed - the post may have children, favorites, or other dependencies that prevent deletion.")
return False
except Exception as e:
print(f"Exception during deletion: {e}")
return False