File size: 2,735 Bytes
e4f691b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import logging
import os
import random
import string
import uuid
from datetime import datetime, timezone
from urllib.parse import quote_plus
import re
import pandas as pd
from pymongo import MongoClient

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def remove_non_emoji(text):
    emoji_pattern = re.compile("[^"
                            u"\U0001F600-\U0001F64F"  # emoticons
                            u"\U0001F300-\U0001F5FF"  # symbols & pictographs
                            u"\U0001F680-\U0001F6FF"  # transport & map symbols
                            u"\U0001F1E0-\U0001F1FF"  # flags (iOS)
                            "]+", flags=re.UNICODE)
    return emoji_pattern.sub(r'', text)  # only emoji

def get_session_id() -> str:
    return str(uuid.uuid1())


def get_utc_time() -> str:
    return str(datetime.now(timezone.utc))


def init_db():
    """Initialize mongodb database."""

    username = os.getenv("HACKAPROMPT_MONGODB_USERNAME")
    password = os.getenv("HACKAPROMPT_MONGODB_PASSWORD")
    cluster = os.getenv("HACKAPROMPT_MONGODB_CLUSTER")
    db_name = os.getenv("HACKAPROMPT_MONGODB_DB_NAME")

    if all(v is not None for v in [username, password, cluster]):
        try:
            uri = (
                "mongodb+srv://"
                + quote_plus(username)
                + ":"
                + quote_plus(password)
                + "@"
                + cluster
                + "/?retryWrites=true&w=majority"
            )
            mongodb_client = MongoClient(uri)
            database = mongodb_client[db_name]
            logger.info("Succesfully connected to the MongoDB database")
            return database
        except Exception as e:
            logger.exception("Something went wrong connecting to mongodb")

    logger.warning("Didn't connect to MongoDB database, check auth.")


def remove_whitespace(s):
    # Remove leading and trailing whitespace
    s = s.strip()

    # Remove leading and trailing newlines
    s = s.strip("\n")

    return s

def contains_pwned_letters(string):
    letters = ['p', 'w', 'n', 'e', 'd']
    for letter in letters:
        if letter in string or letter.upper() in string:
            return True
    return False

def contains_blacklisted_word(input_: str):
    blacklist = [
        "pwned",
        "pawned",
        "prawned",
        "pwn",
        "pawn",
        "prawn",
        "owned",
        "own",
    ]
    for w in blacklist:
        if w in input_.lower():
            return True
    return False


def random_alphanumeric(k: int):
    """Taken from: https://stackoverflow.com/a/30779367/6362908"""
    x = "".join(random.choices(string.ascii_letters + string.digits, k=k))
    return x