-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtxt-toolbox.py
500 lines (395 loc) · 17.1 KB
/
txt-toolbox.py
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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
import os
import sys
import time
from colorama import Fore, Style
import shutil
import psutil
import multiprocessing
from tqdm import tqdm
import itertools
import string
import numpy as np
# This script is still in development, use at your own risk
# contact details can be found at github.com/mirbyte
RED = Fore.RED
ORANGE = Fore.LIGHTRED_EX
BLUE = Fore.BLUE
GREEN = Fore.GREEN
YELLOW = Fore.YELLOW
WHITE = Fore.WHITE
CYAN = Fore.CYAN
PURPLE = Fore.MAGENTA
END = Style.RESET_ALL
def clear():
os.system("cls" if os.name == "nt" else "clear")
def banner():
clear()
print(ORANGE + r"""
_ _ _ _ _
| |_ _ _| |_ | |_ ___ ___ | | |__ _____ __
| __\ \/ / __| | __/ _ \ / _ \| | '_ \ / _ \ \/ /
| |_ > <| |_ | |_|(_) | (_) | | |_) | (_) > <
\___|_/\_\\__| \__|\___/ \___/|_|_.__/ \___/_/\_\
github.com/mirbyte
Algorithmic .txt list generator & modifier
""" + END)
############
def menu():
menu_items = [
"Generate new list",
"Modify existing",
]
for index, item in enumerate(menu_items, start=1):
print(f" [{index}] {item}")
print(" [0] Exit")
############
def menu2():
menu2_items = [
"List Cleaner",
"List Duplicates Remover",
"List Combiner",
"Option 4"
]
for index, item in enumerate(menu2_items, start=1):
print(f" [{index}] {item}")
print(" [0] Exit")
############
def list_cleaner():
# get .txt files in the current dir
txt_files = [f for f in os.listdir() if f.endswith(".txt")]
if not txt_files:
print("No .txt files found in the current directory.")
return
while True:
for i, filename in enumerate(txt_files, start=1):
print(f" {i} {filename}")
print("")
filename_choice = input("\nEnter the corresponding number of the file to process: ")
try:
filename = txt_files[int(filename_choice) - 1]
break
except (ValueError, IndexError):
print("Invalid choice. Please enter a valid number from the list.")
try:
with open(filename, "r", encoding="utf-8", errors="ignore") as file:
passwords = file.readlines()
original_count = len(passwords)
print(f"Processing {filename}...")
cleaned_passwords = set()
for password in passwords:
try:
# cleaning process
cleaned_password = ''.join(char for char in password if not char.isdigit() and char != '.' and char != ' ')
cleaned_passwords.add(cleaned_password.strip())
except UnicodeDecodeError:
print(f"Warning: Skipping line with encoding error.")
new_filename = filename.replace(".txt", "_cleaned.txt")
except (FileNotFoundError, UnicodeDecodeError) as e:
print(f"Error: Encountered an issue while processing {filename}: {e}")
return # Exit the function early if an error occurs
# write to a new file using UTF-8
with open(new_filename, "w", encoding="utf-8") as file:
file.writelines(password + "\n" for password in cleaned_passwords)
changes_made = original_count - len(cleaned_passwords)
print(f"New file saved as {new_filename}")
print("")
input("Press Enter to continue...")
# back to the menu
############
def remove_duplicates():
txt_files = [f for f in os.listdir() if f.endswith(".txt")]
if not txt_files:
print("No .txt files found in the current directory.")
return
while True:
print("Text files found in the current directory:")
for i, filename in enumerate(txt_files, start=1):
print(f" {i} {filename}")
print("")
filename_choice = input("\nEnter the corresponding number of the file to process: ")
try:
filename = txt_files[int(filename_choice) - 1]
break
except (ValueError, IndexError):
print("Invalid choice. Please enter a valid number from the list.")
try:
with open(filename, "r", encoding="utf-8", errors="ignore") as file:
passwords = file.readlines()
original_count = len(passwords)
print(f"Processing {filename}...")
unique_passwords = set()
for password in passwords:
try:
unique_passwords.add(password.strip())
except UnicodeDecodeError:
print(f"Warning: Skipping line with encoding error.")
new_filename = filename.replace(".txt", "_uniques.txt")
except (FileNotFoundError, UnicodeDecodeError) as e:
print(f"Error: Encountered an issue while processing {filename}: {e}")
return
with open(new_filename, "w", encoding="utf-8") as file:
file.writelines(password + "\n" for password in unique_passwords)
duplicates_removed = original_count - len(unique_passwords)
print(f"New file saved as {new_filename}")
print(f"Found and removed {duplicates_removed} duplicates.")
print("")
input("Press Enter to continue...")
# back to the menu
############ COMBINER #############
def combine_lists():
txt_files = [f for f in os.listdir() if f.endswith(".txt")]
if not txt_files:
print("No .txt files found in the current directory.")
return
selected_files = []
while True:
print("\nAvailable text files:")
for i, filename in enumerate(tqdm(txt_files), start=1):
selected = "✓" if filename in selected_files else " "
print(f" [{selected}] {i} {filename}")
if selected_files:
break
print("\nSelect files to combine (enter numbers separated by spaces)")
print("Enter 'all' to select all files")
choice = input("> ").lower()
if choice == 'all':
selected_files = txt_files.copy()
break
else:
try:
choices = [int(c) for c in choice.split()]
for c in choices:
filename = txt_files[c - 1]
if filename not in selected_files:
selected_files.append(filename)
break
except (ValueError, IndexError):
print("Invalid selection. Please enter valid numbers.")
output_filename = input("\nEnter output filename (without extension): ") + ".txt"
if os.path.exists(output_filename):
overwrite = input(f"File {output_filename} already exists. Overwrite? (y/n): ").lower()
if overwrite != 'y':
print("Operation cancelled.")
return
try:
chunk_size = 100000 # Process 100k lines at a time
unique_lines = set()
total_lines_processed = 0
total_lines_read = 0
print("\nProcessing files...")
for filename in tqdm(selected_files, desc="Files"):
try:
with open(filename, "r", encoding="utf-8", errors="ignore") as infile:
for line in infile:
total_lines_read += 1
stripped_line = line.strip()
# Skip empty lines
if stripped_line:
unique_lines.add(stripped_line)
# Process in chunks to avoid memory issues
if len(unique_lines) >= chunk_size:
with open(output_filename, "a" if total_lines_processed > 0 else "w", encoding="utf-8") as outfile:
outfile.writelines(line + "\n" for line in unique_lines)
total_lines_processed += len(unique_lines)
unique_lines.clear()
except Exception as e:
print(f"\nWarning: Error processing file {filename}: {e}")
print("Continuing with other files...")
continue
# Write any remaining lines
if unique_lines:
with open(output_filename, "a" if total_lines_processed > 0 else "w", encoding="utf-8") as outfile:
outfile.writelines(line + "\n" for line in unique_lines)
total_lines_processed += len(unique_lines)
print(f"\nSuccessfully combined {len(selected_files)} files into {output_filename}")
unique_count = total_lines_processed
print(f"Read {total_lines_read:,} lines, wrote {unique_count:,} unique lines")
print(f"Removed {total_lines_read - unique_count:,} duplicates")
except Exception as e:
print(f"\nError occurred while combining files: {e}")
return
input("\nPress Enter to continue...")
return # sys exit
############
banner()
menu()
print(" Select Option: ")
inp = int(input(" > "))
#############GENERATOR##
if inp == 1:
clear()
banner()
def get_char_set():
while True:
print(" [1] numbers only")
print(" [2] lowercase only")
print(" [3] numbers + lowercase")
print(" [4] numbers + all letters")
print(" [5] numbers + all letters + special characters")
print(" [6] custom character set")
print("")
choice = input("Choose the character set: ")
if choice in ("1", "2", "3", "4", "5", "6"):
if choice == "1":
return string.digits
elif choice == "2":
return string.ascii_lowercase
elif choice == "3":
return string.digits + string.ascii_lowercase
elif choice == "4":
return string.digits + string.ascii_letters
elif choice == "5":
return string.digits + string.ascii_letters + string.punctuation
else: # Choice 6
custom_chars = input("Enter your custom character set: ")
if not custom_chars:
print("Character set cannot be empty")
continue
if len(set(custom_chars)) < 3:
print("Warning: Very small character set")
return ''.join(sorted(set(custom_chars)))
else:
print("Invalid choice. Please enter 1, 2, 3, 4, 5, or 6.")
chars = get_char_set()
# In the generator section:
length = 0
while length <= 0:
try:
length = int(input("Length? "))
if length <= 0:
print("Length must be a positive number.")
except ValueError:
print("Please enter a valid number.")
# warning for large combinations
total_combinations = len(chars) ** length
if total_combinations > 100000000:
print(f"Warning: This will generate {total_combinations:,} combinations.")
confirm = input("This may take a long time and use a lot of disk space. Continue? (y/n): ").lower()
if confirm != 'y':
print("Operation cancelled.")
input("Press Enter to close...")
exit()
# Fix the numeric generation case by removing the incorrect return
def get_safe_chunk_size(current_chunk_size, length, chars):
"""Dynamically adjust chunk size based on available RAM"""
mem = psutil.virtual_memory()
safe_threshold = 0.7 # Use max 70% of available RAM
# Calculate memory needed for one chunk (approximate)
bytes_per_item = length * 4 # Rough estimate (4 bytes per char)
memory_needed = current_chunk_size * bytes_per_item
# Reduce chunk size if needed
while memory_needed > (mem.available * safe_threshold):
current_chunk_size = current_chunk_size // 2
memory_needed = current_chunk_size * bytes_per_item
if current_chunk_size < 1000: # Minimum chunk size
raise MemoryError("Not enough memory to proceed safely")
return current_chunk_size
# Handle different character set options
if chars == string.digits:
chunk_size = get_safe_chunk_size(1_000_000, length, chars)
filename_prefix = "numbers_only"
output_filename = f"{filename_prefix}_len{length}.txt"
if os.path.exists(output_filename):
overwrite = input(f"File {output_filename} already exists. Overwrite? (y/n): ").lower()
if overwrite != 'y':
print("Operation cancelled.")
input("Press Enter to close...")
exit()
# Generate all possible number combinations
with open(output_filename, "wb") as file:
total_numbers = 10**length
chunk_size = min(chunk_size, total_numbers)
for start in tqdm(range(0, total_numbers, chunk_size),
desc="Generating",
unit="chunk"):
end = min(start + chunk_size, total_numbers)
chunk = [f"{num:0{length}d}\n" for num in range(start, end)]
file.write(b''.join(s.encode('utf-8') for s in chunk))
print(f"Saved to {output_filename}")
input("Press Enter to close...")
exit()
else: # For mixed character sets (options 2-6)
try:
# filename prefix
if chars == string.ascii_lowercase:
filename_prefix = "lowercase_only"
elif chars == string.digits + string.ascii_lowercase:
filename_prefix = "numbers_lowercase"
elif chars == string.digits + string.ascii_letters:
filename_prefix = "numbers_allletters"
elif chars == string.digits + string.ascii_letters + string.punctuation:
filename_prefix = "all_chars"
else:
filename_prefix = "custom_chars"
output_filename = f"{filename_prefix}_len{length}.txt"
if os.path.exists(output_filename):
overwrite = input(f"File {output_filename} already exists. Overwrite? (y/n): ").lower()
if overwrite != 'y':
print("Operation cancelled.")
input("Press Enter to close...")
exit()
chunk_size = get_safe_chunk_size(100_000, length, chars)
# Generate combinations using a more reliable approach
with open(output_filename, "wb") as file:
char_list = list(chars)
total_combinations = len(chars) ** length
if total_combinations == 0:
raise ValueError("No combinations possible with current character set")
pbar = tqdm(total=total_combinations, desc="Generating")
# Use a simpler chunking approach
product_iter = itertools.product(char_list, repeat=length)
while True:
chunk = []
# Get next chunk_size items
for _ in range(chunk_size):
try:
combo = next(product_iter)
chunk.append(''.join(combo) + '\n')
except StopIteration:
break
if not chunk: # No more combinations
break
file.write(b''.join(s.encode('utf-8') for s in chunk))
pbar.update(len(chunk))
file.flush() # Ensure data is written to disk
pbar.close()
print(f"Saved to {output_filename}")
input("Press Enter to close...")
exit()
except Exception as e:
print(f"Error during generation: {str(e)}")
input("Press Enter to close...")
exit()
########################
elif inp == 2:
clear()
banner()
menu2()
print(" Select Option: ")
inp2 = int(input(" > "))
if inp2 == 1: # List Cleaner
clear()
banner()
list_cleaner()
elif inp2 == 2: # Duplicate Remover
clear()
banner()
remove_duplicates()
elif inp2 == 3: # List Combiner
clear()
banner()
combine_lists()
elif inp2 == 0: # Exit
print("Exiting...")
exit()
else: # Invalid option
print("Invalid option selected.")
input("Press Enter to return to main menu...")
clear()
banner()
menu()
print(" Select Option: ")
inp = int(input(" > "))
# This will loop back to the main menu processing
if __name__ == "__main__":
main()