def mark(text, args, Mark, extra_cli_args, *a):
    if extra_cli_args and extra_cli_args[0] not in button_map:
        print(f"The key `{extra_cli_args[0]}` is unknown.")
        print(f"You must specify one of: {', '.join(button_map.keys())}")
        return
    if args.type == "emoji" or args.type == "emoji_char_and_name":
        import demoji

        if demoji.last_downloaded_timestamp() is None:
            demoji.download_codes()
        demoji.set_emoji_pattern()
        if args.type == "emoji":
            regex = demoji._EMOJI_PAT
        else:
            emoji_name_pattern = r":[a-z0-9_+-]+:"
            regex = re.compile(r"{}|{}".format(demoji._EMOJI_PAT.pattern,
                                               emoji_name_pattern))
        args.minimum_match_length = 1
    else:
        pattern, _ = functions_for(args)
        regex = re.compile(pattern)
    for idx, (s, e, _) in enumerate(
            regex_finditer(regex, args.minimum_match_length, text)):
        lines = text[:s].split("\n")
        y = len(lines) - 1
        x = wcswidth(lines[-1])
        mark_text = text[s:e].replace("\n", "").replace("\0", "")
        yield Mark(idx, s, e, mark_text, {"x": x, "y": y})
def main():
    config = configparser.ConfigParser()
    config.read('conf.ini')
    reddit_user = config['REDDIT']['reddit_user']
    reddit_pass = config['REDDIT']['reddit_pass']
    client_id = config['REDDIT']['client_id']
    client_secret = config['REDDIT']['client_secret']
    target_subreddit = config['REDDIT']['target_subreddit']
    post_title = config['REDDIT']['post_title']

    reddit = praw.Reddit(
        username=reddit_user,
        password=reddit_pass,
        client_id=client_id,
        client_secret=client_secret,
        user_agent='Space Hunt Top 20 Reddit Poster Bot (by u/impshum)'
    )

    if not demoji.last_downloaded_timestamp():
        demoji.download_codes()

    print('Waiting until scheduled')
    schedule.every().monday.do(runner, reddit, target_subreddit, post_title)
    while True:
        schedule.run_pending()
        sleep(1)
Beispiel #3
0
def modify_name(name: str) -> str:
    if not demoji.last_downloaded_timestamp():
        demoji.download_codes()

    name = demoji.replace(string=name, repl="")
    name = unicodedata.normalize("NFKC", name)
    name = iuliia.translate(source=name, schema=iuliia.WIKIPEDIA)
    return name.title()
Beispiel #4
0
def create_app():
    app = Flask(__name__)

    app.config.from_object(config)

    login.init_app(app)
    babel.init_app(app)
    bootstrap.init_app(app)
    mail.init_app(app)
    moment.init_app(app)
    config.init_app(app)

    if not config.FLASK_DEBUG:
        log = logging.getLogger('werkzeug')
        log.setLevel(logging.ERROR)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint, url_prefix='/')

    from .admin import admin as admin_blueprint
    app.register_blueprint(admin_blueprint, url_prefix='/admin')

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    from .errors import errors as errors_blueprint
    app.register_blueprint(errors_blueprint, url_prefix='/')

    from .api import api as api_blueprint
    app.register_blueprint(api_blueprint, url_prefix='/api')

    app.jinja_env.globals.update(cache_suffix=comm.cache_suffix, user=comm.get_current_user, is_dark_mode=comm.is_dark_mode,
                                 is_pygal_chart=comm.is_pygal_chart, is_pandas_table=comm.is_pandas_table,
                                 get_locale=get_locale, Global=Global, render_chart_png_inline=render.render_chart_png_inline)

    if demoji.last_downloaded_timestamp() is None:
        demoji.download_codes()

    return app
from nltk import word_tokenize, pos_tag
from nltk.corpus import stopwords
import demoji
import html, re
import string
# Demoji package - https://pypi.org/project/demoji/

if demoji.last_downloaded_timestamp() is None:
    demoji.download_codes() # one-time run

urlPattern = re.compile(r'((www\.[^\s]+)|(https?://[^\s]+))')
MENTION_PATTERN = re.compile(r'@\w*')
HASHTAG_PATTERN = re.compile(r'#\w*')
non_alphanumeric_or_twitter_characters = re.compile(r'[^a-zA-Z0-9\s|#|@|\'|\.|’|\"|”]')
sentence_ending_punctuation = [".", "!", "?"]
#weather_channels = ["weatherchannel", "weather channel", "accuweather"]

def remove_non_ascii(s):
    #thanks to https://stackoverflow.com/questions/1342000/
    #how-to-make-the-python-interpreter-correctly-handle-non-ascii-characters-in-stri
    return "".join(i for i in s if ord(i)<128)

#def remove_weather_channels(text):
#    t = text.lower()
#    for channel in weather_channels:
#        if channel in t:
#            t = t.replace(channel, "")
#    return t

def remove_URL(text):
    return re.sub(urlPattern," ",text).strip() #strips leading/trailing whitespace
Beispiel #6
0
def test_demoji_main(tweet):
    assert not demoji.findall("Hi")
    assert demoji.replace("Hi") == "Hi"
    assert not demoji.findall("2 ! $&%((@)# $)@ ")
    assert demoji.findall("The 🌓 shall rise again") == {
        "🌓": "first quarter moon"
    }
    allhands = "Someone actually gets paid to make a %s, a %s, and a %s" % (
        person_tipping_hand,
        man_tipping_hand,
        woman_tipping_hand,
    )
    assert demoji.findall(allhands) == {
        person_tipping_hand: "person tipping hand",
        man_tipping_hand: "man tipping hand",
        woman_tipping_hand: "woman tipping hand",
    }
    assert (
        demoji.replace(allhands)
        == "Someone actually gets paid to make a , a , and a "
    )
    assert (
        demoji.replace(allhands, "X")
        == "Someone actually gets paid to make a X, a X, and a X"
    )
    assert isinstance(demoji.last_downloaded_timestamp(), datetime.datetime)

    # Something for everyone...
    batch = [
        "😀",
        "😂",
        "🤩",
        "🤐",
        "🤢",
        "🙁",
        "😫",
        "🙀",
        "💓",
        "🧡",
        "🖤",
        "👁️‍🗨️",
        "✋",
        "🤙",
        "👊",
        "🙏",
        "👂",
        "👱‍♂️",
        "🧓",
        "🙍‍♀️",
        "🙋",
        "🙇",
        "👩‍⚕️",
        "👩‍🔧",
        "👨‍🚒",
        "👼",
        "🦸",
        "🧝‍♀️",
        "👯‍♀️",
        "🤽",
        "🤼‍♀️",
        "🏴󠁧󠁢󠁳󠁣󠁴󠁿",
        "👩‍👧‍👦",
        "🐷",
        "2️⃣",
        "8️⃣",
        "🆖",
        "🈳",
        "الجزيرة‎",
        "傳騰訊入股Reddit 言論自由不保?",
        "🇩🇯",
        "⬛",
        "🔵",
        "🇨🇫",
        "‼",
    ]
    assert len(demoji.findall(" xxx ".join(batch))) == len(batch) - 2
    assert demoji.findall(tweet) == {
        "🔥": "fire",
        "🌋": "volcano",
        "👨🏽\u200d⚖️": "man judge: medium skin tone",
        "🎅🏾": "Santa Claus: medium-dark skin tone",
        "🇲🇽": "flag: Mexico",
        "👹": "ogre",
        "🤡": "clown face",
        "🇳🇮": "flag: Nicaragua",
        "🚣🏼": "person rowing boat: medium-light skin tone",
        "🐂": "ox",
    }
Beispiel #7
0
def test_last_downloaded_timestamp_rettype():
    ts = demoji.last_downloaded_timestamp()
    assert isinstance(ts, datetime.datetime)
Beispiel #8
0
def test_set_emoji_pattern_no_json_raises():
    if os.path.isfile(demoji.CACHEPATH):
        os.remove(demoji.CACHEPATH)
    with pytest.raises(IOError):
        demoji.set_emoji_pattern()
    assert demoji.last_downloaded_timestamp() is None
Beispiel #9
0
def test_load_codes_from_file_raises_if_dne():
    if os.path.isfile(demoji.CACHEPATH):
        os.remove(demoji.CACHEPATH)
    with pytest.raises(IOError):
        demoji._load_codes_from_file()
    assert demoji.last_downloaded_timestamp() is None
Beispiel #10
0
import re

import demoji
import dispy_markdown as md
import pendulum

from api.emoji import EMOJI_LIST, UNICODE_LIST

if not demoji.last_downloaded_timestamp() or pendulum.now() > \
        (pendulum.instance(demoji.last_downloaded_timestamp()).add(days=7)):
    demoji.download_codes()

demoji.set_emoji_pattern()
UNICODE_EMOJI_PAT = demoji._EMOJI_PAT.pattern
jumbo_pat = re.compile(fr'<a?:.*?:\d*>|:[\wñ+-]+:|{UNICODE_EMOJI_PAT}')


class BlockQuote(md.classes['block_quote']):

    @staticmethod
    def html(node, output, state):
        return md.html_tag(
            'div', md.html_tag(
                'div', '', {'class': 'blockquoteDivider-2hH8H6'}, state
            ) + md.html_tag(
                'blockquote', output(node['content'], state)
            ), {'class': 'blockquoteContainer-U5TVEi'}, state
        )


class CodeBlock(md.classes['code_block']):
Beispiel #11
0
import base64
import html
import re
from datetime import timedelta, datetime

import demoji
import pytz

from django_logs.emoji import EMOJI_LIST, EMOJI_REGEX, UNICODE_LIST

if not demoji.last_downloaded_timestamp() or datetime.now(pytz.UTC) > \
        (demoji.last_downloaded_timestamp() + timedelta(days=7)):
    demoji.download_codes()

if not demoji._EMOJI_PAT:
    demoji.set_emoji_pattern()


def format_content_html(content: str,
                        users: dict = None,
                        masked_links: bool = False,
                        newlines: bool = True) -> str:
    # HTML-encode content

    def encode_codeblock(m):
        encoded = base64.b64encode(m.group(1).encode()).decode()
        return '\x1AM' + encoded + '\x1AM'

    # Encode multiline codeblocks (```text```)
    content = re.sub(r'```+((?:[^`]*?\n)?(?:[\s\S]+))\n?```+',
                     encode_codeblock, content)
Beispiel #12
0
            print(f"Error: Another instance of the bot is already running (PID {pid}). Please close it before starting a new instance.")
            sys.exit(1)
        else:
            print("PID file is stale, removing")
            os.unlink("botc.pid")

    with open("botc.pid", "w") as fd:
        fd.write(str(os.getpid()))

    def remove_pidfile():
        os.unlink("botc.pid")

    atexit.register(remove_pidfile)

    
    last_downloaded = demoji.last_downloaded_timestamp()
    if last_downloaded is None or (datetime.now(pytz.utc) - last_downloaded).days > 100:
        # Download emojis
        print(f"===== DOWNLOADING EMOJI DATA =====")
        demoji.download_codes()

    globvars.init_client()
    globvars.init_master_state()

    def command_prefix(bot, message):
        if message.guild is None:
            return (PREFIX, "")
        else:
            return PREFIX

    # The help command