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 random
import seventv
import requests
from PIL import Image from PIL import Image
import imageio import imageio
import requests import requests
import os import seventv
import io
import pyvips
def get_response(message: str): def get_response(message: str):
p_message = message.lower() p_message = message.lower()
if p_message == 'hello':
return 'Hey there!'
if p_message[:3] == ("add"): if p_message[:3] == ("add"):
url = p_message.split(" ")[1] url = p_message.split(" ")[1]
return(addGif(url)) return addGif(url)
if p_message == 'help': if p_message == "help":
return helpText() return helpText()
print(p_message[:3])
return 'I didn\'t understand what you wrote. Try typing "help".' return 'I didn\'t understand what you wrote. Try typing "help".'
def helpText(): def helpText():
return '`This is a help message that you can modify.`' return "`?add <7tv url> to add a 7tv emoji to your server`"
'''
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
'''
def addGif(url): def addGif(url):
@ -56,7 +29,7 @@ def addGif(url):
try: try:
webp_data = requests.get(webpUrl).content webp_data = requests.get(webpUrl).content
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
return ("Invalid URL") return "Invalid URL"
# Extract the name from the url and replace .webp extension with .gif # Extract the name from the url and replace .webp extension with .gif
gif_name = seventv.get_emote_name(url) gif_name = seventv.get_emote_name(url)
@ -75,7 +48,15 @@ def addGif(url):
# Create a byte buffer and save the gif data into it # Create a byte buffer and save the gif data into it
gif_data_buffer = io.BytesIO() 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 # Get the gif data as bytes
gif_data = gif_data_buffer.getvalue() gif_data = gif_data_buffer.getvalue()

View file

@ -3,25 +3,28 @@ import requests
def get_webp_url(url): def get_webp_url(url):
emote_id = url[23:] 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() 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"]:
for file in response['host']['files']: if (
if file['width'] == 128 and file['name'][3:] == 'webp': file["width"] <= 128 and file["height"] <= 128 and file["size"] <= max_size
return ('https:' + image_url + '/' + file['name']) ) 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): def get_emote_name(url):
emote_id = url[23:] 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() response = requests.get(api_call).json()
emote_name = response['name'] emote_name = response["name"]
return emote_name return emote_name