-
greetings! I am trying to code a script that will help me post data to the Szurubooru instance. And the plan is convert this into an iOS/macOS Shortcut so I can upload images.
import requests
ENDPOINT = "https://booru/api"
get_headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
"Authorization": "TOKEN LTRmOWUtOTAyNC0x",
}
get_response = requests.get(url=f"{ENDPOINT}/post/1", headers=get_headers, verify=False)
import requests
ENDPOINT = "https://booru/api"
authheader = {
'Authorization': 'TOKEN LTRmOWUtOTAyNC0x',
}
data17 = {
"tags": "2process",
"safety": "safe",
"metadata": open("Downloads/bor/unknown.png", 'rb'),
}
post_response = requests.post(url=f"{ENDPOINT}/posts/", verify=False, headers=authheader, files=data17)
print(post_response.text) As I was not certain of adhering to the API guidelines I did try to seek help on forums that are specific for Python and on Reddit.. I could not make progress, so, I am trying here to check.. I tried to modify the code by reading a few articles & other GitHub projects, but I am sure I missing some thing.
thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Just putting this here for completeness as this has been solved in #572 I used noirscape's code and adapted it a bit. import base64
import requests
def btoa(txt: str):
return base64.b64encode(txt.encode()).decode()
# conventional names for a session include session, s and r. i use session here. pick your poison.
# the auth token is created by taking base64 encoding the `username:login_token`
# you can find the login token in the webui under Account -> Login tokens
session = requests.Session()
session.headers = {
"Accept": "application/json",
"Authorization": "Token " + btoa("username:01ec0fd6-2dd4-40a6-907a-c36dfec07592"),
}
API_URL = "https://booru/api"
# context manager means that the file handle will always be properly closed, even if something fails.
# some operating systems don't clean up loose file handles which can lead to OS weirdness.
with open("image.png", "rb") as uploadfile:
# first we upload the file to temporary storage and get the token from the response
temporary_data = session.post(
f"{API_URL}/uploads", files={"content": uploadfile}
).json()
file_token = temporary_data["token"]
# here you can use that token for anything else you want to do pre-upload, such as reverse image search.
# next we upload the file for real
# you can add your own parameters here - token and safety are the only required ones.
# setting content-type here explicitly means its added to the session headers
post_data = session.post(
f"{API_URL}/posts",
json={"contentToken": file_token, "safety": "safe", "tags": ["tag1", "tag2"]},
headers={"Content-Type": "application/json"},
)
print(post_data.json()) # finally we print the uploaded post data. |
Beta Was this translation helpful? Give feedback.
Just putting this here for completeness as this has been solved in #572
I used noirscape's code and adapted it a bit.