master
Aevann1 2021-09-10 06:08:32 +02:00
parent d359f5fdae
commit 4a78b5667f
1 changed files with 33 additions and 31 deletions

View File

@ -13,13 +13,10 @@ IBB_KEY = environ.get("IBB_KEY", "").strip()
def upload_ibb(filepath=None, file=None, resize=False):
if file:
format = file.filename.split('.')[-1].lower().replace('jpg','png').replace('jpeg','png')
filepath = f"image.{format}"
file.save(filepath)
if file: file.save("image.gif")
i = IImage.open(filepath)
if resize:
i = IImage.open("image.gif")
size = 100, 100
frames = ImageSequence.Iterator(i)
@ -33,28 +30,29 @@ def upload_ibb(filepath=None, file=None, resize=False):
om = next(frames)
om.info = i.info
om.save(filepath, save_all=True, append_images=list(frames), loop=0, optimize=True, quality=30)
try: om.save("image.gif", save_all=True, append_images=list(frames), loop=0)
except: return
else: i.save(filepath, optimize=True, quality=30)
with open(filepath, 'rb') as f:
data={'image': base64.b64encode(f.read())}
req = requests.post(f'https://api.imgbb.com/1/upload?key={IBB_KEY}', data=data)
resp = req.json()['data']
url = resp['url']
filedir = "image.gif"
try:
with open(filedir, 'rb') as f:
data={'image': base64.b64encode(f.read())}
req = requests.post(f'https://api.imgbb.com/1/upload?key={IBB_KEY}', data=data)
resp = req.json()['data']
url = resp['url']
except:
if req: print(req.json())
return
return url
def upload_imgur(filepath=None, file=None, resize=False):
def upload_imgur(file=None, resize=False):
if file:
format = file.filename.split('.')[-1].lower().replace('jpg','png').replace('jpeg','png')
filepath = f"image.{format}"
file.save(filepath)
if file: file.save("image.gif")
i = IImage.open(filepath)
if resize:
i = IImage.open("image.gif")
size = 100, 100
frames = ImageSequence.Iterator(i)
@ -68,18 +66,22 @@ def upload_imgur(filepath=None, file=None, resize=False):
om = next(frames)
om.info = i.info
om.save(filepath, save_all=True, append_images=list(frames), loop=0)
try: om.save("image.gif", save_all=True, append_images=list(frames), loop=0)
except: return(None)
else: i.save(filepath, optimize=True, quality=30)
with open(filepath, 'rb') as f:
data={'image': base64.b64encode(f.read())}
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}, data=data)
resp = req.json()['data']
url = resp['link']
if not "_d." in url:
url = url.replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg")
if "_d." in url: url += "?maxwidth=9999"
filedir = "image.gif"
try:
with open(filedir, 'rb') as f:
data={'image': base64.b64encode(f.read())}
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}, data=data)
resp = req.json()['data']
url = resp['link']
if not "_d." in url:
url = url.replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg")
if "_d." in url: url += "?maxwidth=9999"
except:
if req: print(req.json())
return
new_image = Image(text=url, deletehash=resp["deletehash"])
g.db.add(new_image)
@ -115,4 +117,4 @@ def upload_video(file):
img = Image(text=link, deletehash=resp['deletehash'])
g.db.add(img)
return link
return link