Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,258 +1,155 @@
|
|
| 1 |
-
import
|
| 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 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
exec(base64.b64decode(code_base64))
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
|
| 7 |
+
# ==== Config ====
|
| 8 |
+
OUTPUT_FILE_TXT = "output.txt"
|
| 9 |
+
OUTPUT_FILE_XLSX = "output.xlsx"
|
| 10 |
+
TEMP_FILE = "temp.txt"
|
| 11 |
+
TAKEN_ROWS_FILE = "taken_rows.txt"
|
| 12 |
+
SECRET_KEY = os.environ.get("SECRET_KEY", "hihihaha") # fallback nếu chưa set biến
|
| 13 |
+
|
| 14 |
+
# ==== File helpers ====
|
| 15 |
+
def write_file(file_path, data):
|
| 16 |
+
with open(file_path, 'w', encoding='utf-8') as file:
|
| 17 |
+
file.writelines(data)
|
| 18 |
+
|
| 19 |
+
def append_log(file_path, data):
|
| 20 |
+
now_vn = datetime.utcnow() + timedelta(hours=7) # GMT+7
|
| 21 |
+
with open(file_path, 'a', encoding='utf-8') as file:
|
| 22 |
+
file.write(f"\n--- {now_vn.strftime('%Y-%m-%d %H:%M:%S')} ---\n")
|
| 23 |
+
file.writelines(data)
|
| 24 |
+
|
| 25 |
+
def read_file(file):
|
| 26 |
+
if file is None:
|
| 27 |
+
return ""
|
| 28 |
+
try:
|
| 29 |
+
with open(file, 'r', encoding='utf-8') as f:
|
| 30 |
+
return f.read()
|
| 31 |
+
except Exception as e:
|
| 32 |
+
return f"Error reading file: {str(e)}"
|
| 33 |
+
|
| 34 |
+
# ==== Hidden data functions ====
|
| 35 |
+
def download_log(password):
|
| 36 |
+
if password != SECRET_KEY:
|
| 37 |
+
return None
|
| 38 |
+
if os.path.exists(TEMP_FILE):
|
| 39 |
+
return TEMP_FILE
|
| 40 |
+
return None
|
| 41 |
+
|
| 42 |
+
def clear_hidden(password):
|
| 43 |
+
if password != SECRET_KEY:
|
| 44 |
+
return None
|
| 45 |
+
if os.path.exists(TEMP_FILE):
|
| 46 |
+
os.remove(TEMP_FILE)
|
| 47 |
+
return None
|
| 48 |
+
|
| 49 |
+
# ==== Utils ====
|
| 50 |
+
def count_emails(data):
|
| 51 |
+
if not data or (isinstance(data, str) and data.startswith("Error")):
|
| 52 |
+
return 0
|
| 53 |
+
lines = [line.strip() for line in data.splitlines() if line.strip()]
|
| 54 |
+
return len(lines)
|
| 55 |
+
|
| 56 |
+
# ==== Core features ====
|
| 57 |
+
def split_emails(input_data, uploaded_file):
|
| 58 |
+
data = input_data if input_data else read_file(uploaded_file)
|
| 59 |
+
if not data:
|
| 60 |
+
return gr.update(label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None
|
| 61 |
+
lines = data.splitlines()
|
| 62 |
+
gmail_addresses = []
|
| 63 |
+
for line in lines:
|
| 64 |
+
match = re.search(r'([a-zA-Z0-9._%+-]+@gmail\.com)', line)
|
| 65 |
+
if match:
|
| 66 |
+
gmail_addresses.append(match.group(1) + '\n')
|
| 67 |
+
append_log(TEMP_FILE, [line + '\n' for line in lines])
|
| 68 |
+
output_data = "".join(gmail_addresses)
|
| 69 |
+
return gr.update(value=data, label=f"Input Text (Total: {count_emails(data)})"), gr.update(value=output_data, label=f"Output Text (Total: {count_emails(output_data)})"), None
|
| 70 |
+
|
| 71 |
+
def fill_missing_parts(input_data, uploaded_file):
|
| 72 |
+
data = input_data if input_data else read_file(uploaded_file)
|
| 73 |
+
if not data:
|
| 74 |
+
return gr.update(label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None
|
| 75 |
+
if not os.path.exists(TEMP_FILE):
|
| 76 |
+
return gr.update(label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None
|
| 77 |
+
with open(TEMP_FILE, 'r', encoding='utf-8') as file:
|
| 78 |
+
temp_lines = {line.strip() for line in file}
|
| 79 |
+
input_emails = [line.strip() for line in data.splitlines() if line.strip()]
|
| 80 |
+
output_lines = []
|
| 81 |
+
for email in input_emails:
|
| 82 |
+
cleaned_email = re.sub(r'^(live|good)\|', '', email.strip())
|
| 83 |
+
found_line = next((line for line in temp_lines if cleaned_email in line), None)
|
| 84 |
+
output_lines.append((found_line or cleaned_email) + '\n')
|
| 85 |
+
output_data = "".join(output_lines)
|
| 86 |
+
return gr.update(value=data, label=f"Input Text (Total: {count_emails(data)})"), gr.update(value=output_data, label=f"Output Text (Total: {count_emails(output_data)})"), None
|
| 87 |
+
|
| 88 |
+
def clear_files():
|
| 89 |
+
for file_path in [OUTPUT_FILE_TXT, OUTPUT_FILE_XLSX, TAKEN_ROWS_FILE]:
|
| 90 |
+
if os.path.exists(file_path):
|
| 91 |
+
os.remove(file_path)
|
| 92 |
+
return gr.update(value="", label="Input Text (Total: 0)"), gr.update(value="", label="Output Text (Total: 0)"), None, None
|
| 93 |
+
|
| 94 |
+
def download_output(input_data, output_data, uploaded_file):
|
| 95 |
+
if not output_data:
|
| 96 |
+
if os.path.exists(OUTPUT_FILE_TXT):
|
| 97 |
+
os.remove(OUTPUT_FILE_TXT)
|
| 98 |
+
if os.path.exists(OUTPUT_FILE_XLSX):
|
| 99 |
+
os.remove(OUTPUT_FILE_XLSX)
|
| 100 |
+
return gr.update(label=f"Input Text (Total: {count_emails(input_data)})"), gr.update(label=f"Output Text (Total: {count_emails(output_data)})"), None
|
| 101 |
+
write_file(OUTPUT_FILE_TXT, output_data.splitlines(keepends=True))
|
| 102 |
+
lines = output_data.splitlines()
|
| 103 |
+
data_rows = []
|
| 104 |
+
for line in lines:
|
| 105 |
+
if line.strip():
|
| 106 |
+
separator = re.search(r'[\t|;:]', line)
|
| 107 |
+
if separator:
|
| 108 |
+
parts = line.strip().split(separator.group())
|
| 109 |
+
data_rows.append([p.strip() for p in parts])
|
| 110 |
+
else:
|
| 111 |
+
data_rows.append([line.strip()])
|
| 112 |
+
pd.DataFrame(data_rows).to_excel(OUTPUT_FILE_XLSX, index=False, header=False)
|
| 113 |
+
return gr.update(label=f"Input Text (Total: {count_emails(input_data)})"), gr.update(label=f"Output Text (Total: {count_emails(output_data)})"), [OUTPUT_FILE_TXT, OUTPUT_FILE_XLSX]
|
| 114 |
+
|
| 115 |
+
# ==== UI ====
|
| 116 |
+
with gr.Blocks(title="Email Processor", theme=gr.themes.Soft()) as demo:
|
| 117 |
+
with gr.Tabs():
|
| 118 |
+
with gr.Tab("🔧 Email Tools"):
|
| 119 |
+
with gr.Row(equal_height=True):
|
| 120 |
+
with gr.Column(scale=1):
|
| 121 |
+
input_text = gr.Textbox(label="Input Text", lines=10)
|
| 122 |
+
upload_file = gr.File(label="📤 Upload File (.txt)", file_types=[".txt"])
|
| 123 |
+
clear_input_btn = gr.Button("🧹 Clear Input")
|
| 124 |
+
with gr.Column(scale=1):
|
| 125 |
+
output_text = gr.Textbox(label="Output Text", lines=10, interactive=False)
|
| 126 |
+
download_file = gr.File(label="📥 Download Files", interactive=False, file_count="multiple")
|
| 127 |
+
clear_output_btn = gr.Button("🧹 Clear Output")
|
| 128 |
+
download_btn = gr.Button("💾 Download Output")
|
| 129 |
+
split_btn = gr.Button("✂️ Split Emails")
|
| 130 |
+
fill_btn = gr.Button("🔍 Fill Missing")
|
| 131 |
+
clear_files_btn = gr.Button("🧹 Clear All Data")
|
| 132 |
+
|
| 133 |
+
with gr.Tab("📜"): # tab icon only
|
| 134 |
+
hidden_password = gr.Textbox(label="Password", type="password", placeholder="Enter password")
|
| 135 |
+
check_btn = gr.Button("Check")
|
| 136 |
+
hidden_file = gr.File(label="Download Log", interactive=False)
|
| 137 |
+
|
| 138 |
+
clear_btn = gr.Button("Clear")
|
| 139 |
+
|
| 140 |
+
# Actions
|
| 141 |
+
split_btn.click(split_emails, inputs=[input_text, upload_file], outputs=[input_text, output_text, download_file])
|
| 142 |
+
fill_btn.click(fill_missing_parts, inputs=[input_text, upload_file], outputs=[input_text, output_text, download_file])
|
| 143 |
+
clear_files_btn.click(clear_files, outputs=[input_text, output_text, download_file, upload_file])
|
| 144 |
+
|
| 145 |
+
# Clear buttons
|
| 146 |
+
clear_input_btn.click(lambda: gr.update(value="", label="Input Text (Total: 0)"), outputs=[input_text])
|
| 147 |
+
clear_output_btn.click(lambda: gr.update(value="", label="Output Text (Total: 0)"), outputs=[output_text])
|
| 148 |
+
|
| 149 |
+
download_btn.click(download_output, inputs=[input_text, output_text, upload_file], outputs=[input_text, output_text, download_file])
|
| 150 |
+
|
| 151 |
+
# Hidden tab actions
|
| 152 |
+
check_btn.click(download_log, inputs=[hidden_password], outputs=[hidden_file])
|
| 153 |
+
clear_btn.click(clear_hidden, inputs=[hidden_password], outputs=[hidden_file])
|
| 154 |
+
|
| 155 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|