25 lines
985 B
Python
25 lines
985 B
Python
import aiohttp as ah
|
|
import json as j
|
|
from os import path
|
|
|
|
#conf=j.load(open(path.join(path.dirname(__file__),'config.json')))[path.splitext(path.basename(__file__))[0]]
|
|
|
|
baseurl='https://gelbooru.com/index.php'
|
|
|
|
async def getgelbimg(tags):
|
|
params={'page':'dapi','s':'post','q':'index','limit':1,'json':1}
|
|
params|=conf
|
|
if isinstance(tags,str): params['tags']=f'{tags} sort:random'
|
|
if isinstance(tags,list): params['tags']=' '.join(tags+['sort:random'])
|
|
async with ah.ClientSession() as sess:
|
|
async with sess.get(baseurl,params=params) as resp:
|
|
try: data=(await resp.json())['post'][0]
|
|
except KeyError: print('no result'); return None,None,None,None
|
|
async with sess.get(data['file_url']) as resp:
|
|
img=await resp.read()
|
|
src=f'https://gelbooru.com/index.php?page=post&s=view&id={data["id"]}'
|
|
ecchi=(data['rating']!="general") #lmao they changed it, need to fix this now
|
|
name=data['image']
|
|
return img,name,src,ecchi
|
|
|
|
#Gelb needs to put more API endpoints. Like edits. |