From 7a2837b62c02afa82a6cf18d35b4e59371fa43f3 Mon Sep 17 00:00:00 2001 From: Albert Sanchez Date: Tue, 16 Nov 2021 18:35:28 -0600 Subject: [PATCH] scooped out command parser for later consumption New Command() class handles this. --- pseudbot/command.py | 48 +++++++++++++++++++++++++++++++++++++++++++ pseudbot/tweet_bot.py | 44 +++++++-------------------------------- 2 files changed, 55 insertions(+), 37 deletions(-) create mode 100644 pseudbot/command.py diff --git a/pseudbot/command.py b/pseudbot/command.py new file mode 100644 index 0000000..01cfea1 --- /dev/null +++ b/pseudbot/command.py @@ -0,0 +1,48 @@ +import random +import re + +from .media import MEDIA + + +class Command: + pasta = True + media = [] + + def __init__(self, text: str, debug: bool = False): + self.text = text + self.debug = debug + self.parse() + + def parse(self): + words = re.split(r'[\s.;():"]+', self.text) + if len(words[-1]) < 1: + words.pop() + + if self.debug is True: + print(words) + + stupid_emoji = "🖼" + b"\xef\xb8\x8f".decode() + if stupid_emoji in words or "🖼" in words: + for i in range(len(words)): + if words[i] in ("🖼", stupid_emoji): + try: + media_category = words[i + 1] + i += 1 + except IndexError: + self.pasta = False + break + + if media_category in MEDIA: + self.media.append(random.choice(MEDIA[media_category])) + + if len(self.media) == 0: + self.pasta = True + + +def mk_commands(text: str) -> [Command]: + commands = [] + + for command_string in text.split("|"): + commands.append(Command(text=command_string)) + + return commands diff --git a/pseudbot/tweet_bot.py b/pseudbot/tweet_bot.py index 60b70ed..14c8d6b 100644 --- a/pseudbot/tweet_bot.py +++ b/pseudbot/tweet_bot.py @@ -1,5 +1,4 @@ import random -import re from sys import stderr from textwrap import indent from time import sleep, time @@ -7,8 +6,8 @@ import tweepy as t from tweepy.errors import Forbidden, TooManyRequests import typing +from .command import Command, mk_commands from .exceptions import * -from .media import MEDIA from .pastas import PASTAS from .util import ( get_timestamp_s, @@ -291,45 +290,16 @@ class PseudBot: """ (parent_id, parent_screen_name) = self._get_reply_parent(tweet) - text = get_tweet_text(tweet) - - for command_string in text.split("|"): - words = re.split(r'[\s.;():"]+', command_string) - if len(words[-1]) < 1: - words.pop() - - if self.debug is True: - print(words) - - media = [] - do_pasta = True - - stupid_emoji = "🖼" + b"\xef\xb8\x8f".decode() - if stupid_emoji in words or "🖼" in words: - for i in range(len(words)): - if words[i] in ("🖼", stupid_emoji): - try: - media_category = words[i + 1] - i += 1 - except IndexError: - do_pasta = False - break - - if media_category in MEDIA: - media.append(random.choice(MEDIA[media_category])) - - if len(media) == 0: - do_pasta = True - - if do_pasta is True: + for command in mk_commands(get_tweet_text(tweet)): + if command.pasta is True: pasta = self._make_pasta_chain(parent_screen_name) - self._tweet_pasta(parent_id, pasta, media) - elif len(media) > 0: - self._tweet_media(parent_id, parent_screen_name, media) + self._tweet_pasta(parent_id, pasta, command.media) + elif len(command.media) > 0: + self._tweet_media(parent_id, parent_screen_name, command.media) else: print( '[WARN]: Unable to parse tweet segment: "{}"'.format( - command_string + command.text ), file=stderr, )