~marnold128/vsss

3f24107a28bcbd29058ccf369a18a040e09074a0 — Matt Arnold a month ago 9347f4d master
Refactor emojifilter some
2 files changed, 76 insertions(+), 41 deletions(-)

M emojifilter.py
M vsss.conf.in.matt
M emojifilter.py => emojifilter.py +75 -40
@@ 1,17 1,34 @@
#!/usr/bin/env python3
# This file is misleadingly named
# it's a generic preprocessor now
# It's called emojifilter, but that never been the purpose of the progrm
# it was always just the name of the file
# This does all the detecting, inspecting, injecting, selecting AND REJECTING
# on a piece of text from the clipboard, before the text is passed to the speech backend
# Now i've been a tryin to avoid writing this program for literal YEARS
# But we got a MOVEMENT ON the internet goin on to have little pictures in text
# And i don't wanna be left out
# so one of the things this program inspects for is emojis, and turns em into text
# it works kinda like a filter and so it's called emojifilter
# But that ain't all it does, it can also perform regex replacement
# and add metadata announcements to the begining of readings
# so emojifilter is just the name of the file

from io import StringIO
import os
import re
import json
from sys import stdin, stdout, exit
import math
import emoji
import argparse
import os

read_rate = os.getenv('rate')
MAX_WPM = int(read_rate) if read_rate is not None else 175
_sb = os.getenv('nostats')
SHOW_STATS = False if _sb is not None else True



parser = argparse.ArgumentParser()
parser.add_argument('-s', '--scope', help='a context scope for filtering')
args = parser.parse_args()

def extract_urls(text):
    """Extract URLs from a given text."""


@@ 24,48 41,66 @@ def replace_urls(text, urls):
        text = text.replace(url, f'[{i}]')
    return text

if not args.scope:
    replfile = os.path.expanduser("~/.vsss/replace.json")
else:
    if not args.scope.endswith(".json"):
        args.scope = args.scope + ".json"
    replfile = os.path.join(os.path.expanduser("~/.vsss"), args.scope)

output = StringIO()

repld = {}
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('-s', '--scope', help='a context scope for filtering')
    args = parser.parse_args()
    ourinput = stdin.read()

if not os.path.exists(replfile):
   print("Error Replacement file must exist at " + replfile)
   exit(1)

ourinput = stdin.read()
newout = None
with open(replfile) as fp:
    repld = json.loads(fp.read())
    if not args.scope:
        replfile = os.path.expanduser("~/.vsss/replace.json")
    else:
        if not args.scope.endswith(".json"):
            args.scope = args.scope + ".json"
        replfile = os.path.join(os.path.expanduser("~/.vsss"), args.scope)

urls_list = extract_urls(ourinput)
newout = replace_urls(ourinput, urls_list)
    output = StringIO()

for key in repld:
    newout = re.sub(key, repld[key], ourinput)
    repld = {}

for ch in newout:
    if emoji.is_emoji(ch):
        output.write(emoji.demojize(ch))
    else:
        output.write(ch)
    if not os.path.exists(replfile):
        print("Error Replacement file must exist at " + replfile)
        exit(1)
    
    if SHOW_STATS:
        
        wordcount = len(ourinput.split(' '))
        eta = math.ceil(wordcount/MAX_WPM)
        msg = f"Article has {wordcount} words will finish in {eta} minutes"
        print(msg)
    
    newout = None
    with open(replfile) as fp:
        repld = json.loads(fp.read())

    urls_list = extract_urls(ourinput)
    newout = replace_urls(ourinput, urls_list)

    for key in repld:
        newout = re.sub(key, repld[key], ourinput)

    for ch in newout:
        if emoji.is_emoji(ch):
            output.write(emoji.demojize(ch))
        else:
            output.write(ch)

    for i, url in enumerate(urls_list, start=1):
        output.write(f"[{i}]: {url}\n")
    # Now we come to the weird/non self explainitory parts
    # we need to copy the output buffer char by char ending 
    # in \n for follow along highlighting to work properly for backends
    # which support it
    output.seek(0)
    for ch in output:
        stdout.write(ch)

for i, url in enumerate(urls_list, start=1):
    output.write(f"[{i}]: {url}\n")
# Now we come to the weird/non self explainitory parts
# we need to copy the output buffer char by char ending 
# in \n for follow along highlighting to work properly for backends
# which support it
output.seek(0)
for ch in output:
    stdout.write(ch)
    stdout.write('\n')
    exit(0)

stdout.write('\n')
exit(0)

if __name__ == '__main__':
    main()

M vsss.conf.in.matt => vsss.conf.in.matt +1 -1
@@ 1,5 1,5 @@
VOX="Callie"
rate=190
export rate=190
PIPE_COLOR="1;33;45m"
export PIPE_COLOR
audio_bckend="padsp"