Changed script to use json and the actual API
parent
0472ca941a
commit
33075b08b7
|
|
@ -10,53 +10,35 @@ import requests
|
|||
import bs4
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
|
||||
def on_ui_settings():
|
||||
section = ('gelbooru-prompt', "Gelbooru Prompt")
|
||||
|
||||
def fetch(image):
|
||||
#update hash based on image
|
||||
#name = image.name
|
||||
name = image.orig_name
|
||||
print("name: " + name)
|
||||
hash = name.split(".")[0]
|
||||
if hash.startswith("sample_"):
|
||||
hash = hash.replace("sample_", "")
|
||||
if hash.startswith("thumbnail_"):
|
||||
hash = hash.replace("thumbnail_", "")
|
||||
print("hash: " + hash)
|
||||
|
||||
url = "https://gelbooru.com/index.php?page=post&s=list&tags=md5%3a" + hash
|
||||
req = requests.get(url, 'html.parser')
|
||||
soup = BeautifulSoup(req.content , 'html.parser')
|
||||
found = None
|
||||
for link in soup.find_all('a'):
|
||||
link = link.get('href')
|
||||
if link.startswith("https://gelbooru.com/index.php?page=post"):
|
||||
if not link.endswith("tags=all"):
|
||||
found = link
|
||||
break
|
||||
if found is not None:
|
||||
print("FOUND: " + found)
|
||||
req = requests.get(found, 'html.parser')
|
||||
soup = BeautifulSoup(req.content , 'html.parser')
|
||||
#tag_data = soup.find_all("textarea")
|
||||
tag_data = soup.find_all("textarea", {"id": "tags"})
|
||||
if len(tag_data) > 0:
|
||||
#print(tag_data)
|
||||
tag_data = tag_data[0].string.split(" ")
|
||||
else:
|
||||
return("Invalid Image Format...")
|
||||
url = "https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&tags=md5:" + hash
|
||||
req = requests.get(url)
|
||||
data = req.json()
|
||||
if data["@attributes"]["count"] > 1:
|
||||
return("No image found with that hash...")
|
||||
else:
|
||||
post = data["post"][0]
|
||||
tags = post["tags"]
|
||||
|
||||
parsed = []
|
||||
for tag in tag_data:
|
||||
for tag in tags.split():
|
||||
tag = tag.replace("_", " ")
|
||||
parsed.append(tag)
|
||||
#print("Parsed tags: " + str(parsed))
|
||||
parsed = (", ").join(parsed)
|
||||
#print()
|
||||
print(parsed)
|
||||
#print()
|
||||
return(parsed)
|
||||
else:
|
||||
return("No image found with that hash...")
|
||||
|
||||
class BooruPromptsScript(scripts.Script):
|
||||
def __init__(self) -> None:
|
||||
|
|
@ -70,12 +52,10 @@ class BooruPromptsScript(scripts.Script):
|
|||
with gr.Group():
|
||||
with gr.Accordion("Gelbooru Prompt", open=False):
|
||||
fetch_tags = gr.Button(value='Get Tags', variant='primary')
|
||||
#image = gr.Image(source="upload", type="file", label="Image with MD5 Hash")
|
||||
image = gr.File(type="file", label="Image with MD5 Hash")
|
||||
tags = gr.Textbox(value = "", label="Tags", lines=5)
|
||||
|
||||
fetch_tags.click(fn=fetch, inputs=[image], outputs=[tags])
|
||||
return [image, tags, fetch_tags]
|
||||
|
||||
|
||||
script_callbacks.on_ui_settings(on_ui_settings)
|
||||
script_callbacks.on_ui_settings(on_ui_settings)
|
||||
|
|
|
|||
Loading…
Reference in New Issue