mirror of
https://git.gay/Juli/imagebot.git
synced 2025-05-23 05:25:15 +00:00
meow
This commit is contained in:
parent
c9751f0457
commit
2db5c57530
3 changed files with 46 additions and 38 deletions
Binary file not shown.
|
@ -1,6 +1,8 @@
|
|||
import os
|
||||
import requests
|
||||
from dotenv import load_dotenv, dotenv_values
|
||||
|
||||
load_dotenv()
|
||||
|
||||
path = "/app/create"
|
||||
url = os.getenv("baseurl")+path
|
||||
|
@ -16,4 +18,4 @@ data = {
|
|||
|
||||
response = requests.post(url, json=data)
|
||||
|
||||
print(response.text)
|
||||
print(response.json()["secret"])
|
|
@ -3,35 +3,39 @@ import random
|
|||
import requests
|
||||
|
||||
# Danbooru API base URL
|
||||
base_url = 'https://danbooru.donmai.us/posts.json'
|
||||
base_url = "https://danbooru.donmai.us/posts.json"
|
||||
|
||||
# Parameters from .env file
|
||||
search_query = os.getenv('searchquery')
|
||||
banned_tags = os.getenv('bannedtags').split(',')
|
||||
danbooru_user = os.getenv('danbooru_user')
|
||||
danbooru_apikey = os.getenv('danbooru_apikey')
|
||||
search_query = os.getenv("searchquery")
|
||||
banned_tags = os.getenv("bannedtags").split(",")
|
||||
danbooru_user = os.getenv("danbooru_user")
|
||||
danbooru_apikey = os.getenv("danbooru_apikey")
|
||||
|
||||
|
||||
# Function to fetch a random post
|
||||
def fetch_random_post():
|
||||
try:
|
||||
tags = search_query.split()
|
||||
|
||||
|
||||
|
||||
tags = tags[:2]
|
||||
|
||||
params = {
|
||||
'tags': ' '.join(tags),
|
||||
'random': 'true',
|
||||
'login': danbooru_user,
|
||||
'api_key': danbooru_apikey
|
||||
"tags": " ".join(tags),
|
||||
"random": "true",
|
||||
"login": danbooru_user,
|
||||
"api_key": danbooru_apikey,
|
||||
}
|
||||
|
||||
response = requests.get(base_url, params=params)
|
||||
response.raise_for_status()
|
||||
|
||||
posts = response.json()
|
||||
|
||||
filtered_posts = [post for post in posts if not any(tag in post['tag_string'] for tag in banned_tags)]
|
||||
|
||||
filtered_posts = [
|
||||
post
|
||||
for post in posts
|
||||
if not any(tag in post["tag_string"] for tag in banned_tags)
|
||||
]
|
||||
|
||||
if filtered_posts:
|
||||
random_post = random.choice(filtered_posts)
|
||||
|
@ -44,40 +48,42 @@ def fetch_random_post():
|
|||
print(f"Error fetching data: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def get_image_url(post):
|
||||
if 'large_file_url' in post:
|
||||
return post['large_file_url']
|
||||
if "large_file_url" in post:
|
||||
return post["large_file_url"]
|
||||
else:
|
||||
print("Image URL not found in the post data.")
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def get_artist_names(post):
|
||||
return post['tag_string_artist'].split(',') if 'tag_string_artist' in post else []
|
||||
return post["tag_string_artist"].split(",") if "tag_string_artist" in post else []
|
||||
|
||||
|
||||
# Main function
|
||||
def main():
|
||||
random_post = fetch_random_post()
|
||||
if random_post:
|
||||
print(f"URL: https://danbooru.donmai.us/posts/{random_post['id']}")
|
||||
print(random_post["tag_string_character"])
|
||||
image_url = get_image_url(random_post)
|
||||
# if image_url:
|
||||
# r = requests.get(image_url)
|
||||
# with open("out.jpg","wb") as f:
|
||||
# f.write(r.content)
|
||||
# print(f"Image URL: {image_url}")
|
||||
# else:
|
||||
# print("Failed to fetch a random post.")
|
||||
|
||||
|
||||
artist_names = get_artist_names(random_post)
|
||||
if artist_names:
|
||||
artists = f"{', '.join(artist_names)}"
|
||||
try:
|
||||
random_post = fetch_random_post()
|
||||
if random_post:
|
||||
print(f"URL: https://danbooru.donmai.us/posts/{random_post['id']}")
|
||||
print(random_post["tag_string_character"])
|
||||
image_url = get_image_url(random_post)
|
||||
artist_names = get_artist_names(random_post)
|
||||
if artist_names:
|
||||
artists = f"{', '.join(artist_names)}"
|
||||
else:
|
||||
artists = "Unknown"
|
||||
else:
|
||||
artists = "Unknown"
|
||||
return main()
|
||||
except:
|
||||
return main()
|
||||
return (
|
||||
f"https://danbooru.donmai.us/posts/{random_post['id']}",
|
||||
image_url,
|
||||
artists,
|
||||
random_post["tag_string_character"],
|
||||
)
|
||||
|
||||
return f"https://danbooru.donmai.us/posts/{random_post['id']}", image_url, artists, random_post["tag_string_character"]
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue