Advertisement
Najeebsk

ADVANCE-SEARCH2.0.pyw

Aug 13th, 2024 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.95 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import threading
  4. import re
  5. import tkinter as tk
  6. from tkinter import filedialog, messagebox, Scrollbar, Text, END
  7. from googleapiclient.discovery import build
  8.  
  9. # Path to VLC executable
  10. VLC_PATH = r"C:\Program Files\VideoLAN\VLC\vlc.exe"
  11.  
  12. # YouTube API key
  13. YOUTUBE_API_KEY = 'AIzaSyCzS7PGThVFxD83UFbfU5DOSZBMTxNpEeA'
  14.  
  15. class AdvancedFileSearchApp(tk.Tk):
  16.     def __init__(self):
  17.         super().__init__()
  18.         self.title("Najeeb Advanced File Search with YouTube and Media Controls")
  19.         self.geometry("900x700")
  20.         self.configure(bg="#282C34")
  21.  
  22.         self.folder_path = None
  23.         self.lines = []
  24.         self.stop_event = threading.Event()
  25.         self.create_widgets()
  26.  
  27.     def create_widgets(self):
  28.         # Top Frame for main controls
  29.         self.top_frame = tk.Frame(self, bg="#282C34")
  30.         self.top_frame.pack(fill=tk.X, pady=10)
  31.  
  32.         # Drive Selector
  33.         self.drive_var = tk.StringVar()
  34.         self.drive_dropdown = tk.OptionMenu(self.top_frame, self.drive_var, *self.get_drives())
  35.         self.drive_dropdown.config(width=6, bg="#3E4451", fg="#ABB2BF", highlightbackground="#4B5263", highlightcolor="#4B5263")
  36.         self.drive_var.set(self.get_drives()[0] if self.get_drives() else "")
  37.         self.drive_dropdown.pack(side=tk.LEFT, padx=5)
  38.  
  39.         # Select Folder Button
  40.         self.select_folder_btn = tk.Button(self.top_frame, text="Select Folder", command=self.select_folder, bg='lightgreen', fg='black')
  41.         self.select_folder_btn.pack(side=tk.LEFT, padx=5)
  42.  
  43.         # First Search Entry
  44.         self.search_var = tk.StringVar()
  45.         self.search_entry = tk.Entry(self.top_frame, textvariable=self.search_var, width=24, bg="#3E4451", fg="#ABB2BF")
  46.         self.search_entry.pack(side=tk.LEFT, padx=5)
  47.  
  48.         # Second Search Entry
  49.         self.search_var2 = tk.StringVar()
  50.         self.search_entry2 = tk.Entry(self.top_frame, textvariable=self.search_var2, width=24)
  51.         self.search_entry2.pack(side=tk.LEFT, padx=5)
  52.  
  53.         # Search Text Button
  54.         self.search_text_btn = tk.Button(self.top_frame, text="Search Text All", command=self.search_files, bg='lightcoral', fg='black')
  55.         self.search_text_btn.pack(side=tk.LEFT, padx=5)
  56.  
  57.         # Search Word Button
  58.         self.search_word_btn = tk.Button(self.top_frame, text="Search Word", command=self.search_files, bg='lightcoral', fg='black')
  59.         self.search_word_btn.pack(side=tk.LEFT, padx=5)
  60.  
  61.         # Search Drive Button
  62.         self.search_drive_btn = tk.Button(self.top_frame, text="Search Drive", command=self.search_drive, bg='lightgreen', fg='black')
  63.         self.search_drive_btn.pack(side=tk.LEFT, padx=5)
  64.  
  65.         # YouTube Search Button
  66.         self.youtube_search_btn = tk.Button(self.top_frame, text="Search YouTube", command=self.search_youtube, bg='lightyellow', fg='black')
  67.         self.youtube_search_btn.pack(side=tk.LEFT, padx=5)
  68.                
  69.         # Second Line Frame for additional controls
  70.         self.second_line_frame = tk.Frame(self, bg="#282C34")
  71.         self.second_line_frame.pack(fill=tk.X, pady=10)
  72.  
  73.         # Capture Video Button
  74.         self.capture_video_btn = tk.Button(self.second_line_frame, text="Capture Video", command=self.capture_video, bg='lightblue', fg='black')
  75.         self.capture_video_btn.pack(side=tk.LEFT, padx=5)
  76.  
  77.         # Record Audio Button
  78.         self.record_audio_btn = tk.Button(self.second_line_frame, text="Record Audio", command=self.record_audio, bg='lightgreen', fg='black')
  79.         self.record_audio_btn.pack(side=tk.LEFT, padx=5)
  80.              
  81.         # Download Button
  82.         self.download_btn = tk.Button(self.second_line_frame, text="Download Video", command=self.download_video, bg='lightgoldenrod', fg='black')
  83.         self.download_btn.pack(side=tk.LEFT, padx=5)
  84.  
  85.         # Run button
  86.         self.run_button = tk.Button(self.second_line_frame, text="Open Notepad", command=self.run_in_notepad, bg='#90EE90', fg='black')
  87.         self.run_button.pack(side=tk.LEFT, padx=5)
  88.        
  89.         # CMD button
  90.         self.cmd_button = tk.Button(self.second_line_frame, text="Run CMD", command=self.run_cmd, bg='#90EE90', fg='black')
  91.         self.cmd_button.pack(side=tk.LEFT, padx=5)
  92.        
  93.         # Play PotPlayer Button
  94.         self.play_btn = tk.Button(self.second_line_frame, text="Play Selected PotPlayer", command=self.play_selected, bg='lightpink', fg='black')
  95.         self.play_btn.pack(side=tk.LEFT, padx=5)
  96.  
  97.         # Play VLC Player Button
  98.         self.play_vlc_btn = tk.Button(self.second_line_frame, text="Play Selected VLC", command=self.play_selected_vlc, bg='lightpink', fg='black')
  99.         self.play_vlc_btn.pack(side=tk.LEFT, padx=5)
  100.  
  101.         # Save List Button
  102.         self.save_list_btn = tk.Button(self.second_line_frame, text="Save List", command=self.save_list, bg='lightblue', fg='black')
  103.         self.save_list_btn.pack(side=tk.LEFT, padx=5)
  104.  
  105.         # Toggle button
  106.         self.toggle_button = tk.Button(self.second_line_frame, text="Toggle Display", command=self.toggle_display, bg='#ffcc00', fg='black')
  107.         self.toggle_button.pack(side=tk.LEFT, padx=5)
  108.  
  109.        
  110.         # Text widget with Scrollbars
  111.         self.text_frame = tk.Frame(self, bg="#282C34")
  112.         self.text_frame.pack(fill=tk.BOTH, expand=True)
  113.  
  114.         self.result_area = Text(self.text_frame, wrap=tk.NONE, bg='#1E2127', fg='#ABB2BF')
  115.         self.result_area.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  116.  
  117.         self.scrollbar_y = Scrollbar(self.text_frame, orient=tk.VERTICAL)
  118.         self.scrollbar_y.pack(side=tk.RIGHT, fill=tk.Y)
  119.  
  120.         self.scrollbar_x = Scrollbar(self, orient=tk.HORIZONTAL)
  121.         self.scrollbar_x.pack(fill=tk.X)
  122.  
  123.         self.result_area.config(yscrollcommand=self.scrollbar_y.set, xscrollcommand=self.scrollbar_x.set)
  124.         self.scrollbar_y.config(command=self.result_area.yview)
  125.         self.scrollbar_x.config(command=self.result_area.xview)
  126.  
  127.         self.folder_path = ""
  128.         self.show_full_info = False
  129.  
  130.  
  131.     def get_drives(self):
  132.         """Return a list of all available drives on the system."""
  133.         drives = []
  134.         for drive in range(ord('A'), ord('Z')+1):
  135.             drive_letter = f"{chr(drive)}:\\"
  136.             if os.path.exists(drive_letter):
  137.                 drives.append(drive_letter)
  138.         return drives
  139.  
  140.     def select_folder(self):
  141.         self.folder_path = filedialog.askdirectory()
  142.         if self.folder_path:
  143.             messagebox.showinfo("Selected Folder", f"Selected folder: {self.folder_path}")
  144.  
  145.     def search_files(self):
  146.         keyword1 = self.search_entry.get().strip()
  147.         keyword2 = self.search_entry2.get().strip()
  148.         if not self.folder_path:
  149.             self.result_area.insert(END, "Please select a folder first.\n")
  150.             return
  151.  
  152.         if not keyword1 and not keyword2:
  153.             self.result_area.insert(END, "Please enter at least one keyword to search for.\n")
  154.             return
  155.  
  156.         self.result_area.delete(1.0, END)  # Clear the text field
  157.  
  158.         for root, dirs, files in os.walk(self.folder_path):
  159.             for file in files:
  160.                 if file.endswith(('.txt', '.ls', '.ini', '.m3u', '.m3u8', '.py', '.pyw', '.ahk', '.bat', '.cmd', '.vbs', '.htm', '.html', '.au3', '.reg')):
  161.                     file_path = os.path.join(root, file)
  162.                     self.search_in_file(file_path, keyword1, keyword2)
  163.  
  164.     def search_in_file(self, file_path, keyword1, keyword2):
  165.         try:
  166.             with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
  167.                 lines = f.readlines()
  168.                 for i, line in enumerate(lines):
  169.                     if (keyword1.lower() in line.lower()) or (keyword2.lower() in line.lower()):
  170.                         if self.show_full_info:
  171.                             self.result_area.insert(tk.END, f"{file_path} - Line {i+1}: {line}")
  172.                         else:
  173.                             self.result_area.insert(tk.END, f"{line.strip()}\n")
  174.         except Exception as e:
  175.             self.result_area.insert(tk.END, f"Error reading {file_path}: {str(e)}\n")
  176.  
  177.     def save_list(self):
  178.         save_path = filedialog.asksaveasfilename(defaultextension=".txt", filetypes=[("Text files", "*.txt")])
  179.         if save_path:
  180.             try:
  181.                 with open(save_path, 'w', encoding='utf-8') as f:
  182.                     f.write(self.result_area.get(1.0, END))
  183.                 messagebox.showinfo("Save Success", f"Results saved to {save_path}")
  184.             except Exception as e:
  185.                 messagebox.showerror("Save Error", f"Could not save results: {str(e)}")
  186.  
  187.     def run_in_notepad(self):
  188.         selected_text = self.result_area.get(tk.SEL_FIRST, tk.SEL_LAST)
  189.         with open("temp_file.txt", "w", encoding='utf-8') as f:
  190.             f.write(selected_text)
  191.         os.startfile("temp_file.txt")
  192.    
  193.     def run_cmd(self):
  194.         selected_text = self.result_area.get(tk.SEL_FIRST, tk.SEL_LAST)
  195.         os.system(f'cmd /c "{selected_text}"')
  196.        
  197.     def toggle_display(self):
  198.         self.show_full_info = not self.show_full_info
  199.         state = "Full Info" if self.show_full_info else "Search Word Only"
  200.         self.toggle_button.config(text=f"Toggle Display ({state})")
  201.         self.result_area.delete(1.0, tk.END)  # Clear the text field
  202.         if self.folder_path and self.search_entry.get():
  203.             self.search_files()
  204.            
  205.     def search_youtube(self):
  206.         query = self.search_var.get()
  207.         if not query:
  208.             self.result_area.insert(END, "Please enter a query for YouTube search.\n")
  209.             return
  210.  
  211.         youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY)
  212.         request = youtube.search().list(q=query, part='snippet', type='video', maxResults=5)
  213.         response = request.execute()
  214.  
  215.         self.result_area.delete(1.0, END)
  216.         for item in response['items']:
  217.             video_title = item['snippet']['title']
  218.             video_url = f"https://www.youtube.com/watch?v={item['id']['videoId']}"
  219.             self.result_area.insert(END, f"{video_title}\n{video_url}\n\n")
  220.            
  221.     def capture_video(self):
  222.         # Implement video capture functionality
  223.         try:
  224.             selected_text = self.result_area.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  225.             url_match = re.search(r'(https?://\S+)', selected_text)
  226.             if url_match:
  227.                 url = url_match.group(0)
  228.                 filename = filedialog.asksaveasfilename(defaultextension=".mp4", filetypes=[("MP4 files", "*.mp4")])
  229.                 if filename:
  230.                     command = ['ffmpeg', '-y', '-i', url, '-t', '01:35:55', '-c', 'copy', filename]
  231.                     threading.Thread(target=lambda: subprocess.run(command)).start()
  232.                     messagebox.showinfo("Capturing", f"Capturing 03:55 minutes of video to {filename}")
  233.             else:
  234.                 messagebox.showerror("Error", "Selected text is not a valid URL.")
  235.         except tk.TclError:
  236.             messagebox.showerror("Error", "No text selected.")
  237.  
  238.  
  239.     def record_audio(self):
  240.         # Implement audio recording functionality
  241.         try:
  242.             selected_text = self.result_area.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  243.             url_match = re.search(r'(https?://\S+)', selected_text)
  244.             if url_match:
  245.                 url = url_match.group(0)
  246.                 filename = filedialog.asksaveasfilename(defaultextension=".mp3", filetypes=[("MP3 files", "*.mp3")])
  247.                 if filename:
  248.                     command = ['ffmpeg', '-y', '-i', url, '-f', 'mp3', '-c:a', 'libmp3lame', filename]
  249.                     threading.Thread(target=lambda: subprocess.run(command)).start()
  250.                     messagebox.showinfo("Recording", f"Recording audio to {filename}")
  251.             else:
  252.                 messagebox.showerror("Error", "Selected text is not a valid URL.")
  253.         except tk.TclError:
  254.             messagebox.showerror("Error", "No text selected.")
  255.  
  256.     def download_video(self):
  257.         # Implement video download functionality
  258.         #selected_quality = self.quality_var.get()
  259.         selected_text = self.result_area.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  260.         if not selected_text.startswith("http"):
  261.             messagebox.showerror("Invalid Selection", "Please select a valid YouTube URL.")
  262.             return
  263.  
  264.         download_command = f'youtube-dl -f "bestvideo[height<=360]+bestaudio/best" {selected_text} -o "%(title)s.%(ext)s"'
  265.         subprocess.run(download_command, shell=True)
  266.         messagebox.showinfo("Download Complete", "Video downloaded successfully.")
  267.    
  268.     def play_selected(self):
  269.         selected_text = self.result_area.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  270.         if selected_text:
  271.             subprocess.Popen([r"C:\Program Files\DAUM\PotPlayer\PotPlayerMini.exe", selected_text])
  272.         else:
  273.             messagebox.showwarning("No Selection", "Please select a URL or file path to play.")
  274.  
  275.     def play_selected_vlc(self):
  276.         selected_text = self.result_area.get(tk.SEL_FIRST, tk.SEL_LAST).strip()
  277.         if selected_text:
  278.             subprocess.Popen([VLC_PATH, selected_text])
  279.         else:
  280.             messagebox.showwarning("No Selection", "Please select a URL or file path to play.")
  281.  
  282.     def search(self, event=None):
  283.         drive = self.drive_var.get()
  284.         search_term = self.search_var.get().lower()
  285.         self.result_area.delete(1.0, END)
  286.  
  287.         if not drive or not search_term:
  288.             messagebox.showwarning("Warning", "Please select a drive and enter a search term.")
  289.             return
  290.  
  291.         # Search for files
  292.         for root, dirs, files in os.walk(drive):
  293.             for file in files:
  294.                 if search_term in file.lower():
  295.                     self.result_area.insert(END, os.path.join(root, file) + "\n")
  296.  
  297.     def search_drive(self):
  298.         self.search()  # Reuse the search function for drive-specific searches
  299.  
  300. if __name__ == "__main__":
  301.     app = AdvancedFileSearchApp()
  302.     app.mainloop()
  303.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement