Advertisement
Najeebsk

ADVANCE-SEARCH.pyw

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