Handle Giphy

master
HeyMoon 2022-08-21 18:27:33 -05:00
parent 4ee6231ce1
commit 2fa671a2a3
2 changed files with 25 additions and 12 deletions

View File

@ -28,8 +28,11 @@ ALLOWED_COMMENTS_PER_USER_PER_DAY = 20
EMOJI_REGEX = r":[^ ]*:" EMOJI_REGEX = r":[^ ]*:"
IMAGE_REGEX = r"!\[\]\(/images/([1234567890]*)\.webp\)" IMAGE_REGEX = r"!\[\]\(/images/([1234567890]*)\.webp\)"
GIF_REGEX = r"https:\/\/media\.giphy\.com\/media\/([a-zA-Z0-9]*)\/giphy\.webp"
PARSED_IMAGE_REGEX = r"IMAGE:/images/[1234567890]*\.webp" PARSED_IMAGE_REGEX = r"IMAGE:/images/[1234567890]*\.webp"
PARSED_GIF_REGEX = r"GIF:([a-zA-Z0-9]*)"
INJECTABLE_IMAGE_REGEX = r"IMAGE:/images/\1\.webp" INJECTABLE_IMAGE_REGEX = r"IMAGE:/images/\1\.webp"
INJECTABLE_GIF_REGEX = r"GIF:\1"
# rdrama = RDramaAPIInterface(TEST_AUTH_TOKEN, "localhost", sleep=5, https=False) # rdrama = RDramaAPIInterface(TEST_AUTH_TOKEN, "localhost", sleep=5, https=False)
# print(open('emoji_cache/klanjak.webp', 'rb')) # print(open('emoji_cache/klanjak.webp', 'rb'))
@ -96,12 +99,19 @@ class Text(TextElement):
return f"Text({self.text})" return f"Text({self.text})"
class Image(TextElement): class Image(TextElement):
def __init__(self, link): def __init__(self, link, gif):
self.link = link self.link = link
self.gif = gif
def __repr__(self) -> str: def __repr__(self) -> str:
return f"Link({self.link})" return f"Image({self.link}, gif = {self.gif})"
@property
def url(self):
if not self.gif:
return get_full_rdrama_image_url(self.link)
else:
return f"https://media.giphy.com/media/{self.link}/giphy.webp"
class Emoji(TextElement): class Emoji(TextElement):
def __init__(self, emoji, big): def __init__(self, emoji, big):
self.emoji = emoji self.emoji = emoji
@ -131,14 +141,17 @@ def text_elements(string : str):
big = False big = False
element = element.strip(":") element = element.strip(":")
to_return.append(Emoji(element, big)) to_return.append(Emoji(element, big))
elif re.match(PARSED_GIF_REGEX, element):
to_return.append(Image(element.strip()[4:], True))
elif re.match(PARSED_IMAGE_REGEX, element): elif re.match(PARSED_IMAGE_REGEX, element):
to_return.append(Image(element.strip()[6:])) to_return.append(Image(element.strip()[6:], False))
else: else:
to_return.append(Text(element.strip())) to_return.append(Text(element.strip()))
return to_return return to_return
def strip_markdown(markdown_string): def strip_markdown(markdown_string):
markdown_string = re.sub(IMAGE_REGEX, INJECTABLE_IMAGE_REGEX, markdown_string) markdown_string = re.sub(IMAGE_REGEX, INJECTABLE_IMAGE_REGEX, markdown_string)
markdown_string = re.sub(GIF_REGEX, INJECTABLE_GIF_REGEX, markdown_string)
markdown_string = re.sub(">.*\n", "", markdown_string) markdown_string = re.sub(">.*\n", "", markdown_string)
try: try:
html = markdown(markdown_string) html = markdown(markdown_string)
@ -269,8 +282,8 @@ def main_processing_task(rdrama : RDramaAPIInterface, session : Session):
text_line = pure_text_lines[0] text_line = pure_text_lines[0]
image_line = image_lines[0] image_line = image_lines[0]
image = image_line.images[0].link image = image_line.images[0]
full_image_url = get_full_rdrama_image_url(image) full_image_url = image.url
caption = text_line.text caption = text_line.text
image = meme_generator.create_modern_meme_from_url(full_image_url, caption) image = meme_generator.create_modern_meme_from_url(full_image_url, caption)
@ -290,8 +303,8 @@ def main_processing_task(rdrama : RDramaAPIInterface, session : Session):
bottom_text_line = pure_text_lines[1] bottom_text_line = pure_text_lines[1]
image_line = image_lines[0] image_line = image_lines[0]
image = image_line.images[0].link image = image_line.images[0]
full_image_url = get_full_rdrama_image_url(image) full_image_url = image.url
top_caption = top_text_line.text top_caption = top_text_line.text
bottom_caption = bottom_text_line.text bottom_caption = bottom_text_line.text

View File

@ -75,8 +75,8 @@ def create_classic_meme(image: 'AnimatedImage', top_caption : str, bottom_captio
UNIT = 5 UNIT = 5
caption_size = int(image_y_size/UNIT) caption_size = int(image_y_size/UNIT)
image = add_text_box(image, top_caption, (image_x_size, caption_size), (0,0), "impact.ttf", 60, align="cvch", color=ColorScheme.WHITE_WITH_BLACK_BORDER) image = add_text_box(image, top_caption, (image_x_size, caption_size), (0,0), "impact.ttf", caption_size, align="cvch", color=ColorScheme.WHITE_WITH_BLACK_BORDER)
image = add_text_box(image, bottom_caption, (image_x_size, caption_size), (0,int((UNIT-1)*(image_y_size/UNIT))), "impact.ttf", 60, align="cvch", color=ColorScheme.WHITE_WITH_BLACK_BORDER) image = add_text_box(image, bottom_caption, (image_x_size, caption_size), (0,int((UNIT-1)*(image_y_size/UNIT))), "impact.ttf", caption_size, align="cvch", color=ColorScheme.WHITE_WITH_BLACK_BORDER)
return image return image
@ -84,7 +84,7 @@ def create_classic_meme_from_url(url : str, top_caption : str, bottom_caption :
return create_classic_meme(get_image_file_from_url(url), top_caption, bottom_caption) return create_classic_meme(get_image_file_from_url(url), top_caption, bottom_caption)
def create_classic_meme_from_emoji(emoji : str, top_caption : str, bottom_caption : str): def create_classic_meme_from_emoji(emoji : str, top_caption : str, bottom_caption : str):
EMOJI_SIZE = 400 EMOJI_SIZE = 600
BORDER_SIZE = 100 BORDER_SIZE = 100
marsey = get_emoji_from_rdrama(emoji) marsey = get_emoji_from_rdrama(emoji)
@ -107,7 +107,7 @@ def create_modern_meme_from_url(url : str, caption : str):
return create_modern_meme(get_image_file_from_url(url), caption) return create_modern_meme(get_image_file_from_url(url), caption)
def create_modern_meme_from_emoji(emoji: str, caption: str): def create_modern_meme_from_emoji(emoji: str, caption: str):
EMOJI_SIZE = 400 EMOJI_SIZE = 600
BORDER_SIZE = 10 BORDER_SIZE = 10
marsey = get_emoji_from_rdrama(emoji) marsey = get_emoji_from_rdrama(emoji)