tags category leaderboard, refer to api docs for enums
use https://danbooru.donmai.us/counts/posts instead
#!/usr/bin/python | |
# Usage: danbooru-tag-counter.py [tags...] | |
import requests | |
import sys | |
import base64 | |
# Define your username and API key | |
username = "" | |
api_key = "" | |
def generate_auth_header(username, api_key): | |
if not username or not api_key: | |
return None | |
auth_str = f"{username}:{api_key}" | |
auth_bytes = auth_str.encode('utf-8') | |
auth_base64 = base64.b64encode(auth_bytes).decode('utf-8') | |
return f"Basic {auth_base64}" | |
def check_post_count_of_tag(tag): | |
response = requests.get(f"https://danbooru.donmai.us/tags.json?search[name]={tag}") | |
data = response.json() | |
if isinstance(data, list) and len(data) > 0: | |
post_count = data[0].get("post_count") | |
return post_count | |
else: | |
return None | |
def main(): | |
args = sys.argv[1:] | |
if not args: | |
print("Please provide tags as command-line arguments.") | |
return | |
auth_header = generate_auth_header(username, api_key) | |
for i in args: | |
count = check_post_count_of_tag(i) | |
if count is not None: | |
print(f"There are {count} posts of {i}") | |
else: | |
print(f"{i} tag doesn't exist") | |
tag = ' '.join(args) | |
limit_per_page = 200 # max limit for posts | |
total_post_count = 0 | |
page = 1 | |
while True: | |
# stop loop cuz it's one tag | |
if len(args) <= 1: | |
break | |
url = f"https://danbooru.donmai.us/posts.json?tags={tag}&page={page}&limit={limit_per_page}" | |
headers = {'Authorization': auth_header} | |
response = requests.get(url, headers=headers) | |
if response.status_code != 200: | |
print(f"Error: {response.json().get('message', 'Unknown error')}") | |
break | |
data = response.json() | |
if not data: # If the response is empty, break out of the loop | |
break | |
post_count = len(data) | |
total_post_count += post_count | |
print(f"Page/Posts: {page}/{total_post_count}", end='\r') | |
page += 1 | |
if len(args) > 1: | |
print(f"\nTotal number of posts with tags [{tag}] is:", total_post_count) | |
if __name__ == "__main__": | |
main() |
tags category leaderboard, refer to api docs for enums
use https://danbooru.donmai.us/counts/posts instead