NexAddo's picture
Upload 26 files
e4f691b
raw
history blame
2.74 kB
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