File size: 4,257 Bytes
fc9c13b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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'&nbsp;', " ", 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