added support for more sizes + smoother gifs

This commit is contained in:
Juan 2023-07-25 21:31:20 -04:00
parent ce79e80f2a
commit 8e7789d4b5
2 changed files with 33 additions and 49 deletions

View file

@ -1,54 +1,27 @@
import io
import os
import random
import seventv
import requests
from PIL import Image
import imageio
import requests
import os
import io
import pyvips
import seventv
def get_response(message: str):
p_message = message.lower()
if p_message == 'hello':
return 'Hey there!'
if p_message[:3] == ("add"):
url = p_message.split(" ")[1]
return(addGif(url))
return addGif(url)
if p_message == 'help':
if p_message == "help":
return helpText()
print(p_message[:3])
return 'I didn\'t understand what you wrote. Try typing "help".'
def helpText():
return '`This is a help message that you can modify.`'
'''
keeping old code just in case pyvips needs to be used
def addGif(url):
webpUrl = seventv.get_webp_url(url)
try:
webp_data = requests.get(webpUrl).content
except requests.exceptions.RequestException:
return ("Invalid URL")
image = pyvips.Image.new_from_buffer(webp_data, '')
gif_data = image.write_to_buffer('.gif[loop=0]')
gif_name = s
with open(gif_name, 'wb') as f:
f.write(gif_data)
return gif_data, gif_name
'''
return "`?add <7tv url> to add a 7tv emoji to your server`"
def addGif(url):
@ -56,7 +29,7 @@ def addGif(url):
try:
webp_data = requests.get(webpUrl).content
except requests.exceptions.RequestException:
return ("Invalid URL")
return "Invalid URL"
# Extract the name from the url and replace .webp extension with .gif
gif_name = seventv.get_emote_name(url)
@ -75,9 +48,17 @@ def addGif(url):
# Create a byte buffer and save the gif data into it
gif_data_buffer = io.BytesIO()
imageio.mimwrite(gif_data_buffer, frames, 'GIF', loop=0)
# Set the duration of each frame based on the original image's frame rate
duration_per_frame = image.info.get(
"duration", 100
) # Default to 100ms if no duration is set
imageio.mimwrite(
gif_data_buffer, frames, "GIF", duration=duration_per_frame, loop=0
)
# Get the gif data as bytes
gif_data = gif_data_buffer.getvalue()
return gif_data, gif_name
return gif_data, gif_name

View file

@ -3,25 +3,28 @@ import requests
def get_webp_url(url):
emote_id = url[23:]
api_call = ('https://7tv.io/v3/emotes/' + emote_id)
api_call = "https://7tv.io/v3/emotes/" + emote_id
response = requests.get(api_call).json()
image_url = response["host"]["url"]
max_name = ""
max_size = 262144
image_url = response['host']['url']
for file in response['host']['files']:
if file['width'] == 128 and file['name'][3:] == 'webp':
return ('https:' + image_url + '/' + file['name'])
for file in response["host"]["files"]:
if (
file["width"] <= 128 and file["height"] <= 128 and file["size"] <= max_size
) and file["name"][3:] == "webp":
max_name = file["name"]
print(max_size)
return "https:" + image_url + "/" + max_name
return ('No 128x128 file detected')
def get_emote_name(url):
emote_id = url[23:]
api_call = ('https://7tv.io/v3/emotes/' + emote_id)
api_call = "https://7tv.io/v3/emotes/" + emote_id
response = requests.get(api_call).json()
emote_name = response['name']
emote_name = response["name"]
return emote_name