2021-07-21 01:12:26 +00:00
|
|
|
import requests
|
2021-09-05 16:53:01 +00:00
|
|
|
from os import environ, path, remove
|
2021-09-15 00:46:08 +00:00
|
|
|
from PIL import Image as IImage, ImageSequence
|
2021-07-21 01:12:26 +00:00
|
|
|
import base64
|
2021-08-04 15:35:10 +00:00
|
|
|
from files.classes.images import *
|
2021-08-19 05:59:44 +00:00
|
|
|
from flask import g
|
2021-09-05 16:53:01 +00:00
|
|
|
from werkzeug.utils import secure_filename
|
2021-09-15 09:15:56 +00:00
|
|
|
from webptools import gifwebp
|
2021-07-21 01:12:26 +00:00
|
|
|
|
2021-08-11 15:15:51 +00:00
|
|
|
CF_KEY = environ.get("CLOUDFLARE_KEY", "").strip()
|
|
|
|
CF_ZONE = environ.get("CLOUDFLARE_ZONE", "").strip()
|
|
|
|
IMGUR_KEY = environ.get("IMGUR_KEY", "").strip()
|
2021-09-02 14:06:28 +00:00
|
|
|
IBB_KEY = environ.get("IBB_KEY", "").strip()
|
|
|
|
|
2021-09-13 16:35:26 +00:00
|
|
|
def upload_ibb(file=None, resize=False):
|
|
|
|
|
|
|
|
if file: file.save("image.webp")
|
|
|
|
|
2021-09-13 17:01:55 +00:00
|
|
|
if resize:
|
2021-09-15 00:46:08 +00:00
|
|
|
i = IImage.open("image.webp")
|
2021-09-13 17:01:55 +00:00
|
|
|
size = 100, 100
|
|
|
|
frames = ImageSequence.Iterator(i)
|
2021-09-13 16:56:22 +00:00
|
|
|
|
2021-09-13 17:01:55 +00:00
|
|
|
def thumbnails(frames):
|
|
|
|
for frame in frames:
|
2021-09-13 16:58:15 +00:00
|
|
|
thumbnail = frame.copy()
|
2021-09-15 00:46:25 +00:00
|
|
|
thumbnail.thumbnail(size)
|
2021-09-13 16:58:15 +00:00
|
|
|
yield thumbnail
|
|
|
|
|
2021-09-13 17:01:55 +00:00
|
|
|
frames = thumbnails(frames)
|
2021-09-13 16:56:22 +00:00
|
|
|
|
2021-09-13 17:01:55 +00:00
|
|
|
om = next(frames)
|
|
|
|
om.info = i.info
|
|
|
|
try: om.save("image.webp", save_all=True, append_images=list(frames), loop=0, optimize=True, quality=30)
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
return
|
2021-09-15 00:42:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-15 00:46:08 +00:00
|
|
|
else:
|
2021-09-15 09:15:56 +00:00
|
|
|
print(gifwebp(input_image="image.webp", output_image="image.webp", option="-q 80", logging="-v"))
|
2021-09-15 00:42:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-09-13 16:35:26 +00:00
|
|
|
try:
|
|
|
|
with open("image.webp", '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']
|
2021-09-15 00:53:45 +00:00
|
|
|
url = resp['url']
|
2021-09-13 16:35:26 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
print(req.text)
|
|
|
|
return
|
|
|
|
|
|
|
|
return url
|
|
|
|
|
|
|
|
|
2021-09-05 16:53:01 +00:00
|
|
|
class UploadException(Exception):
|
|
|
|
"""Custom exception to raise if upload goes wrong"""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2021-09-15 00:55:48 +00:00
|
|
|
|
2021-09-05 16:53:01 +00:00
|
|
|
def upload_video(file):
|
|
|
|
|
|
|
|
file_path = path.join("temp", secure_filename(file.filename))
|
2021-09-05 17:14:52 +00:00
|
|
|
file.save(file_path)
|
2021-09-05 16:53:01 +00:00
|
|
|
|
|
|
|
headers = {"Authorization": f"Client-ID {IMGUR_KEY}"}
|
|
|
|
with open(file_path, 'rb') as f:
|
|
|
|
try:
|
2021-09-05 17:52:40 +00:00
|
|
|
r = requests.post('https://api.imgur.com/3/upload', headers=headers, files={"video": f})
|
2021-09-05 17:54:02 +00:00
|
|
|
|
2021-09-05 18:14:44 +00:00
|
|
|
r.raise_for_status()
|
2021-09-05 16:53:01 +00:00
|
|
|
|
|
|
|
resp = r.json()['data']
|
|
|
|
except requests.HTTPError as e:
|
2021-09-05 18:14:44 +00:00
|
|
|
raise UploadException("Invalid video. Make sure it's 1 minute long or shorter.")
|
2021-09-07 02:07:11 +00:00
|
|
|
except:
|
2021-09-05 16:53:01 +00:00
|
|
|
raise UploadException("Error, please try again later.")
|
|
|
|
finally:
|
|
|
|
remove(file_path)
|
|
|
|
|
2021-09-06 21:38:17 +00:00
|
|
|
link = resp['link']
|
|
|
|
img = Image(text=link, deletehash=resp['deletehash'])
|
|
|
|
g.db.add(img)
|
|
|
|
|
2021-09-10 04:08:32 +00:00
|
|
|
return link
|