2021-07-21 01:12:26 +00:00
|
|
|
import requests
|
|
|
|
from os import environ
|
2021-07-30 10:43:25 +00:00
|
|
|
from PIL import Image as IImage, ImageSequence
|
2021-07-21 01:12:26 +00:00
|
|
|
import base64
|
2021-07-22 19:19:49 +00:00
|
|
|
from drama.classes.images import *
|
2021-07-21 01:12:26 +00:00
|
|
|
|
|
|
|
CF_KEY = environ.get("CLOUDFLARE_KEY").strip()
|
|
|
|
CF_ZONE = environ.get("CLOUDFLARE_ZONE").strip()
|
|
|
|
imgurkey = environ.get("imgurkey").strip()
|
|
|
|
|
|
|
|
|
2021-07-30 10:43:25 +00:00
|
|
|
def upload_file(file=None, resize=False):
|
2021-07-30 09:34:00 +00:00
|
|
|
|
|
|
|
if file: file.save("image.gif")
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-30 09:34:00 +00:00
|
|
|
if resize:
|
2021-07-30 10:43:25 +00:00
|
|
|
i = IImage.open("image.gif")
|
2021-07-30 10:45:53 +00:00
|
|
|
size = 100, 100
|
2021-07-30 10:43:25 +00:00
|
|
|
frames = ImageSequence.Iterator(i)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-30 10:43:25 +00:00
|
|
|
def thumbnails(frames):
|
|
|
|
for frame in frames:
|
|
|
|
thumbnail = frame.copy()
|
2021-07-30 10:45:53 +00:00
|
|
|
thumbnail.thumbnail(size, IImage.ANTIALIAS)
|
2021-07-30 10:43:25 +00:00
|
|
|
yield thumbnail
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-30 10:43:25 +00:00
|
|
|
frames = thumbnails(frames)
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-07-30 10:43:25 +00:00
|
|
|
om = next(frames)
|
|
|
|
om.info = i.info
|
2021-07-30 11:25:19 +00:00
|
|
|
om.save("image.gif", save_all=True, append_images=list(frames), loop=0)
|
2021-07-29 14:36:16 +00:00
|
|
|
|
2021-07-30 10:52:53 +00:00
|
|
|
try:
|
2021-07-30 11:15:44 +00:00
|
|
|
with open('image.gif', 'rb') as f:
|
2021-07-30 11:14:02 +00:00
|
|
|
data={'image': base64.b64encode(f.read())}
|
2021-07-30 11:16:57 +00:00
|
|
|
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data=data)
|
2021-07-30 10:52:53 +00:00
|
|
|
resp = req.json()['data']
|
|
|
|
url = resp['link'].replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg") + "?maxwidth=9999"
|
|
|
|
except:
|
2021-07-22 21:03:27 +00:00
|
|
|
print(req.text)
|
2021-07-21 01:12:26 +00:00
|
|
|
return
|
2021-07-29 07:53:32 +00:00
|
|
|
|
2021-07-30 09:34:00 +00:00
|
|
|
new_image = Image(text=url, deletehash=resp["deletehash"])
|
2021-07-21 01:12:26 +00:00
|
|
|
g.db.add(new_image)
|
2021-07-29 07:53:32 +00:00
|
|
|
return(url)
|