import mysql.connector from mysql.connector import Error import os, re ### db def create_connection(config): """ Establishes a connection to the database and returns the connection object. """ try: conn = mysql.connector.connect(**config) if conn.is_connected(): print("Successfully connected to the database") return conn except Error as e: print(f"Error connecting to MySQL: {e}") return None def mysqli_return_number(conn, query): cursor = conn.cursor() cursor.execute(query) result = cursor.fetchone() cursor.close() return result[0] if result else 0 ### Files def save_to_file(path, data): with open(path, "w", encoding='utf-8', errors='ignore') as file: file.write(data) def get_file_content(file_path): if os.path.exists(file_path): with open(file_path, 'r', encoding='utf8', errors='ignore') as file: return file.read() else: return '' ### clean text functions def clean_text(text): replacements = { chr(8217): "'", "`": "'", "’": "'" } for orig, repl in replacements.items(): text = text.replace(orig, repl) allowed_chars = set("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/*-+.$*ù^£–—“”—" + "µ%¨!:;,?./§&é\"'(è_çàâ)=°ç[{#}]¤~²ôûîïäëêíáã•©®€¢¥ƒÁÂÀÅÃäÄæÇÉÊÈËÍîÎñÑÓÔóÒøØõÕö" + "ÖœŒšŠßðÐþÞÚúûÛùÙüÜýÝÿŸαβγδεζηθικλμνξοπρστυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ×÷" + "±∓≠<>≤≥≈∝∞∫∑∈∉⊆⊇⊂⊃∪∩∧∨¬∀∃π×⨯⊗→←↑↓↔ ϒϖϑℵ⟩⟨⋅⊗⊄≅∗∉∅⇓⇑⇐↵ℜℑ℘⊕⊥∼∴∪∨∧∠∝" + "∋∈∇∃∀⇔⇒∩¬∞√∑∏∂″′ªº³²¹¾½¼‰¶‡†¿§«»@") text = ''.join(c if c in allowed_chars else ' ' for c in text) remove = ['. .', '..', "?.", "!.", ";.", "??", "? ?", ] for phrase in remove: text = text.replace(phrase, phrase[0] + ' ') text = re.sub(r' ', " ", text) text = re.sub(r'&#(x[0-9a-fA-F]+|\d+);', ' ', text) text = re.sub(r'&#\d+;', ' ', text) text = re.sub(r'\s{2,}', ' ', text) replacements = { "À": "À", "Â": "Â", "Ã": "Ã", "Ä": "Ä", "Â": "Â", "Ã…": "Å", "á": "á", "â": "â", "ã": "ã", "ä": "ä", "Ã¥": "å", "Ã’": "Ò", "Ó": "Ó", "Ô": "Ô", "Õ": "Õ", "Ö": "Ö", "Ø": "Ø", "ò": "ò", "ó": "ó", "ô": "ô", "õ": "õ", "ö": "ö", "ø": "ø", "È": "È", "É": "É", "Ê": "Ê", "Ë": "Ë", "è": "è", "é": "é", "ê": "ê", "ë": "ë", "Ç": "Ç", "ç": "ç", "ÃŒ": "Ì", "ÃŽ": "Î", "ì": "ì", "í": "í", "î": "î", "ï": "ï", "Ù": "Ù", "Ú": "Ú", "Û": "Û", "Ãœ": "Ü", "ù": "ù", "ú": "ú", "û": "û", "ü": "ü", "ÿ": "ÿ", "Ñ": "Ñ", "ñ": "ñ", "Á": "Á", "Í": "Í", "à ": "à", '’': '\'', "«": "«", '»': '»' } for key, value in replacements.items(): text = text.replace(key, value) return text def decode_unicode_escapes(s): def decode_match(match): return bytes(match.group(0), "utf-8").decode("unicode_escape") return re.sub(r'(\\u[0-9a-fA-F]{4})', decode_match, s) def decode_content(content_bytes): for encoding in ['utf-8-sig', 'mac_roman', 'ascii', 'windows-1254', 'windows-1252']: try: return content_bytes.decode(encoding) except UnicodeDecodeError: continue return None