diff --git a/delojza.py b/delojza.py index 06a65cc..25f41b3 100755 --- a/delojza.py +++ b/delojza.py @@ -2,6 +2,7 @@ import errno import logging +from markov import MarkovBlabberer import os import pprint import re @@ -19,6 +20,7 @@ from random import random from sqlite3.dbapi2 import Connection from time import sleep from typing import Any, List, Optional, Tuple, cast +from util import datestr, mkdir_p import acoustid import filetype @@ -32,26 +34,11 @@ from _typeshed import StrPath from markovify.text import Text from mutagen import File, FileType from mutagen.easyid3 import EasyID3 -from requests.api import options from telegram.ext import CommandHandler, MessageHandler, Updater from youtube_dl import DownloadError from youtube_dl.version import __version__ as YTDL_VERSION -def mkdir_p(path): - try: - os.makedirs(path) - except OSError as exc: - if exc.errno == errno.EEXIST and os.path.isdir(path): - pass - else: - raise - - -def datestr(date: datetime): - return date.strftime("%Y-%m-%d@%H%M") - - class DelojzaDB: def __init__(self, db_path): self.db_path = db_path @@ -115,28 +102,6 @@ class DelojzaDB: ) self.db.commit() - -class MarkovBlabberer: - def __init__(self, filepath: StrPath): - self.logger = logging.getLogger("markov") - self.filepath = filepath - - with open(filepath) as f: - text = f.read() - self.markov: Text = markovify.NewlineText(text.lower()) - self.logger.info("Sentence of the day: " + self.make_sentence()) - - def make_sentence(self, tries: int = 100): - return self.markov.make_sentence(tries=tries) or "???" - - def add_to_corpus(self, text: str): - text = text.lower() - new_sentence = markovify.NewlineText(text) - self.markov = cast(Text, markovify.combine([self.markov, new_sentence])) - with open(self.filepath, "a") as f: - f.write(text + "\n") - - class DelojzaBot: def __init__( self, diff --git a/markov.py b/markov.py new file mode 100644 index 0000000..f336bbe --- /dev/null +++ b/markov.py @@ -0,0 +1,27 @@ +import logging +from typing import cast + +import markovify +from _typeshed import StrPath +from markovify import Text + + +class MarkovBlabberer: + def __init__(self, filepath: StrPath): + self.logger = logging.getLogger("markov") + self.filepath = filepath + + with open(filepath) as f: + text = f.read() + self.markov: Text = markovify.NewlineText(text.lower()) + self.logger.info("Sentence of the day: " + self.make_sentence()) + + def make_sentence(self, tries: int = 100): + return self.markov.make_sentence(tries=tries) or "???" + + def add_to_corpus(self, text: str): + text = text.lower() + new_sentence = markovify.NewlineText(text) + self.markov = cast(Text, markovify.combine([self.markov, new_sentence])) + with open(self.filepath, "a") as f: + f.write(text + "\n") diff --git a/util.py b/util.py new file mode 100644 index 0000000..de19e1f --- /dev/null +++ b/util.py @@ -0,0 +1,18 @@ +from _typeshed import StrPath +from datetime import datetime +import os +import errno + + +def mkdir_p(path: StrPath) -> None: + try: + os.makedirs(path) + except OSError as exc: + if exc.errno == errno.EEXIST and os.path.isdir(path): + pass + else: + raise + + +def datestr(date: datetime) -> str: + return date.strftime("%Y-%m-%d@%H%M")