Advertisement
Najeebsk

SEARCH-TEXT-INSIDE-FOLDER2.0.pyw

Jul 25th, 2024 (edited)
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.53 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import tkinter as tk
  4. from tkinter import filedialog, Text, Scrollbar, messagebox
  5. import re
  6. import threading
  7. import time
  8.  
  9. # Path to VLC executable
  10. VLC_PATH = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  11.  
  12. class FileSearcherApp:
  13.     def __init__(self, root):
  14.         self.root = root
  15.         self.root.title("Najeeb Inside Folder Text File Searcher")
  16.  
  17.         # Adjust the window size to fit the screen
  18.         screen_width = self.root.winfo_screenwidth()
  19.         screen_height = self.root.winfo_screenheight()
  20.         self.root.geometry(f"{screen_width}x{screen_height}")
  21.  
  22.         # Frame for buttons
  23.         self.frame = tk.Frame(self.root)
  24.         self.frame.pack(pady=10)
  25.  
  26.         # Entry to input search keyword
  27.         self.search_label = tk.Label(self.frame, text="Search Keyword:")
  28.         self.search_label.pack(side=tk.LEFT)
  29.  
  30.         self.search_entry = tk.Entry(self.frame)
  31.         self.search_entry.pack(side=tk.LEFT, padx=5)
  32.  
  33.         # Button to select folder
  34.         self.folder_btn = tk.Button(self.frame, text="Select Folder", command=self.select_folder)
  35.         self.folder_btn.pack(side=tk.LEFT, padx=5)
  36.  
  37.         # Button to start search
  38.         self.search_btn = tk.Button(self.frame, text="Search", command=self.search_files)
  39.         self.search_btn.pack(side=tk.LEFT, padx=5)
  40.  
  41.         # Button to save results
  42.         self.save_btn = tk.Button(self.frame, text="Save", command=self.save_results)
  43.         self.save_btn.pack(side=tk.LEFT, padx=5)
  44.  
  45.         # Play button
  46.         self.play_button = tk.Button(self.frame, text="Play", command=self.play_channel)
  47.         self.play_button.pack(side=tk.LEFT, padx=5)
  48.  
  49.         # Toggle button
  50.         self.toggle_button = tk.Button(self.frame, text="Toggle Display", command=self.toggle_display)
  51.         self.toggle_button.pack(side=tk.LEFT, padx=5)
  52.  
  53.         # Capture Video button
  54.         self.capture_video_btn = tk.Button(self.frame, text="Capture Video", command=self.capture_video)
  55.         self.capture_video_btn.pack(side=tk.LEFT, padx=5)
  56.  
  57.         # Record Audio button
  58.         self.record_audio_btn = tk.Button(self.frame, text="Record Audio", command=self.record_audio)
  59.         self.record_audio_btn.pack(side=tk.LEFT, padx=5)
  60.  
  61.         # Capture Screenshots button
  62.         self.capture_screenshots_btn = tk.Button(self.frame, text="Screen Shot", command=self.capture_screenshots)
  63.         self.capture_screenshots_btn.pack(side=tk.LEFT, padx=5)
  64.  
  65.         # Stop Screenshot button
  66.         self.stop_screenshots_btn = tk.Button(self.frame, text="Stop", command=self.stop_screenshots_capture)
  67.         self.stop_screenshots_btn.pack(side=tk.LEFT, padx=5)
  68.  
  69.         # Run button
  70.         self.run_button = tk.Button(self.frame, text="Run", command=self.run_in_notepad)
  71.         self.run_button.pack(side=tk.LEFT, padx=5)
  72.  
  73.         # CMD button
  74.         self.cmd_button = tk.Button(self.frame, text="Run CMD", command=self.run_cmd)
  75.         self.cmd_button.pack(side=tk.LEFT, padx=5)
  76.  
  77.         # Text field for displaying results with a larger font size
  78.         self.result_text = Text(self.root, wrap='none', undo=True, width=80, height=20, font=("Times New Roman", 16))
  79.         self.result_text.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
  80.  
  81.         # Scrollbars for the text field
  82.         self.v_scroll = Scrollbar(self.result_text, orient=tk.VERTICAL, command=self.result_text.yview)
  83.         self.v_scroll.pack(side=tk.RIGHT, fill=tk.Y)
  84.         self.result_text['yscrollcommand'] = self.v_scroll.set
  85.  
  86.         self.h_scroll = Scrollbar(self.result_text, orient=tk.HORIZONTAL, command=self.result_text.xview)
  87.         self.h_scroll.pack(side=tk.BOTTOM, fill=tk.X)
  88.         self.result_text['xscrollcommand'] = self.h_scroll.set
  89.  
  90.         self.folder_path = ""
  91.         self.show_full_info = False
  92.         self.stop_event = threading.Event()  # Event to stop screenshot capturing
  93.  
  94.     def select_folder(self):
  95.         self.folder_path = filedialog.askdirectory()
  96.  
  97.     def search_files(self):
  98.         keyword = self.search_entry.get()
  99.         if not self.folder_path:
  100.             self.result_text.insert(tk.END, "Please select a folder first.\n")
  101.             return
  102.  
  103.         if not keyword:
  104.             self.result_text.insert(tk.END, "Please enter a keyword to search for.\n")
  105.             return
  106.  
  107.         self.result_text.delete(1.0, tk.END)  # Clear the text field
  108.  
  109.         for root, dirs, files in os.walk(self.folder_path):
  110.             for file in files:
  111.                 if file.endswith(('.txt', '.ls', '.ini', '.m3u', '.m3u8', '.py', '.pyw', '.ahk', '.bat', '.cmd', '.vbs', '.htm', '.html', '.au3', '.reg')):
  112.                     file_path = os.path.join(root, file)
  113.                     self.search_in_file(file_path, keyword)
  114.  
  115.     def search_in_file(self, file_path, keyword):
  116.         try:
  117.             with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
  118.                 lines = f.readlines()
  119.                 for i, line in enumerate(lines):
  120.                     if keyword.lower() in line.lower():
  121.                         if self.show_full_info:
  122.                             self.result_text.insert(tk.END, f"{file_path} - Line {i+1}: {line}")
  123.                         else:
  124.                             self.result_text.insert(tk.END, f"{line.strip()}\n")
  125.         except Exception as e:
  126.             self.result_text.insert(tk.END, f"Error reading {file_path}: {str(e)}\n")
  127.  
  128.     def save_results(self):
  129.         results = self.result_text.get(1.0, tk.END)
  130.         if not results.strip():
  131.             messagebox.showwarning("No Results", "There are no results to save.")
  132.             return
  133.        
  134.         save_path = filedialog.asksaveasfilename(defaultextension=".txt",
  135.                                                  filetypes=[("Text files", "*.txt"), ("All files", "*.*")])
  136.         if save_path:
  137.             try:
  138.                 with open(save_path, 'w', encoding='utf-8') as file:
  139.                     file.write(results)
  140.                 messagebox.showinfo("Success", "Results saved successfully.")
  141.             except Exception as e:
  142.                 messagebox.showerror("Error", f"Failed to save results: {str(e)}")
  143.  
  144.     def play_channel(self):
  145.         try:
  146.             selected_text = self.result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  147.             url_match = re.search(r'(https?://\S+)', selected_text)
  148.             if url_match:
  149.                 url = url_match.group(0)
  150.                 subprocess.Popen([VLC_PATH, url])
  151.             else:
  152.                 messagebox.showwarning("Invalid Selection", "No valid URL found in the selected text.")
  153.         except tk.TclError:
  154.             messagebox.showwarning("No Selection", "Please select a line containing a valid URL to play.")
  155.  
  156.     def toggle_display(self):
  157.         self.show_full_info = not self.show_full_info
  158.         state = "Full Info" if self.show_full_info else "Search Word Only"
  159.         self.toggle_button.config(text=f"Toggle Display ({state})")
  160.         self.result_text.delete(1.0, tk.END)  # Clear the text field
  161.         if self.folder_path and self.search_entry.get():
  162.             self.search_files()
  163.  
  164.     def capture_video(self):
  165.         try:
  166.             selected_text = self.result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  167.             url_match = re.search(r'(https?://\S+)', selected_text)
  168.             if url_match:
  169.                 url = url_match.group(0)
  170.                 filename = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 files", "*.mp4")])
  171.                 if filename:
  172.                     command = ['ffmpeg', '-y', '-i', url, '-t', '03:55:00', '-c', 'copy', filename]
  173.                     threading.Thread(target=lambda: subprocess.run(command)).start()
  174.                     messagebox.showinfo("Capturing", f"Capturing 03:55 minutes of video to {filename}")
  175.             else:
  176.                 messagebox.showerror("Error", "Selected text is not a valid URL.")
  177.         except tk.TclError:
  178.             messagebox.showerror("Error", "No text selected.")
  179.  
  180.     def record_audio(self):
  181.         try:
  182.             selected_text = self.result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  183.             url_match = re.search(r'(https?://\S+)', selected_text)
  184.             if url_match:
  185.                 url = url_match.group(0)
  186.                 filename = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("MP3 files", "*.mp3")])
  187.                 if filename:
  188.                     command = ['ffmpeg', '-y', '-i', url, '-f', 'mp3', '-c:a', 'libmp3lame', filename]
  189.                     threading.Thread(target=lambda: subprocess.run(command)).start()
  190.                     messagebox.showinfo("Recording", f"Recording audio to {filename}")
  191.             else:
  192.                 messagebox.showerror("Error", "Selected text is not a valid URL.")
  193.         except tk.TclError:
  194.             messagebox.showerror("Error", "No text selected.")
  195.  
  196.     def capture_screenshots(self):
  197.         def take_screenshots(url, filename_base, interval, num_screenshots):
  198.             for i in range(num_screenshots):
  199.                 if self.stop_event.is_set():
  200.                     break
  201.                 filename = f"{filename_base}_{i + 1}.png"
  202.                 command = ['ffmpeg', '-y', '-i', url, '-vframes', '1', filename]
  203.                 subprocess.run(command)
  204.                 time.sleep(interval)
  205.             messagebox.showinfo("Capturing Screenshots", f"Captured {i + 1} screenshots every {interval} seconds to {filename_base}")
  206.  
  207.         try:
  208.             selected_text = self.result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  209.             url_match = re.search(r'(https?://\S+)', selected_text)
  210.             if url_match:
  211.                 url = url_match.group(0)
  212.                 filename_base = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
  213.                 if filename_base:
  214.                     self.stop_event.clear()  # Clear the stop event flag
  215.                     interval = 2  # Interval in seconds
  216.                     num_screenshots = 100  # Number of screenshots
  217.                     threading.Thread(target=take_screenshots, args=(url, filename_base, interval, num_screenshots)).start()
  218.             else:
  219.                 messagebox.showerror("Error", "Selected text is not a valid URL.")
  220.         except tk.TclError:
  221.             messagebox.showerror("Error", "No text selected.")
  222.  
  223.     def stop_screenshots_capture(self):
  224.         self.stop_event.set()  # Set the stop event flag
  225.         messagebox.showinfo("Screenshot Capture", "Stopping screenshot capture.")
  226.  
  227.     def run_in_notepad(self):
  228.         try:
  229.             selected_text = self.result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  230.             expanded_path = os.path.expandvars(selected_text)
  231.             if os.path.isfile(expanded_path):
  232.                 subprocess.run(['notepad.exe', expanded_path])  # Open with Notepad
  233.             else:
  234.                 messagebox.showerror("Invalid Selection", "The selected text is not a valid file path.")
  235.         except tk.TclError:
  236.             messagebox.showwarning("No Selection", "Please select a valid file path.")
  237.  
  238.     def run_cmd(self):
  239.         try:
  240.             selected_text = self.result_text.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  241.             subprocess.run(selected_text, shell=True)
  242.         except tk.TclError:
  243.             messagebox.showwarning("No Selection", "Please select a command to run.")
  244.         except Exception as e:
  245.             messagebox.showerror("Error", f"Failed to execute the command: {str(e)}")
  246.  
  247. if __name__ == "__main__":
  248.     root = tk.Tk()
  249.     app = FileSearcherApp(root)
  250.     root.mainloop()
  251.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement