Advertisement
Guest User

Untitled

a guest
Jan 2nd, 2025
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. import os
  2. import time
  3. import pyperclip
  4. import yt_dlp as ytdl
  5. import keyboard
  6. import re
  7. from datetime import datetime
  8.  
  9. # Set the save directory to Downloads folder
  10. save_directory = os.path.join(os.path.expanduser('~'), 'Downloads', 'ClipboardFiles') # Saves to "ClipboardFiles" folder in Downloads
  11. # Create the folder if it doesn't exist
  12. if not os.path.exists(save_directory):
  13. os.makedirs(save_directory)
  14.  
  15. # Function to download YouTube video using yt-dlp
  16. def download_youtube(url):
  17. try:
  18. ydl_opts = {
  19. 'outtmpl': os.path.join(save_directory, '%(title)s.%(ext)s'), # Save with video title in the specified folder
  20. }
  21. with ytdl.YoutubeDL(ydl_opts) as ydl:
  22. ydl.download([url])
  23. print(f"Downloaded: {url}")
  24. except Exception as e:
  25. print(f"Error downloading video: {e}")
  26.  
  27. # Function to sanitize and generate a filename from the clipboard content
  28. def sanitize_filename(text):
  29. # Remove all non-alphanumeric characters and limit length to 50 characters
  30. sanitized = re.sub(r'[^a-zA-Z0-9]', '_', text) # Replace anything that's not alphanumeric with underscores
  31. sanitized = sanitized[:50] # Truncate to the first 50 characters
  32. return sanitized
  33.  
  34. # Function to save text from clipboard to a file and replace clipboard with filename
  35. def save_text_to_file(text):
  36. filename = sanitize_filename(text) # Generate a sanitized filename based on clipboard content
  37. try:
  38. # Save text to the file in the specified directory
  39. file_path = os.path.join(save_directory, filename + ".txt")
  40. with open(file_path, 'w') as file:
  41. file.write(text)
  42.  
  43. print(f"Saved text to {file_path}.")
  44. return filename # Return the sanitized filename for clipboard replacement
  45.  
  46. except Exception as e:
  47. print(f"Error saving text to file: {e}")
  48. return filename # Return the sanitized filename, even if saving failed
  49.  
  50. # Check if clipboard contains a YouTube URL
  51. def is_youtube_link(text):
  52. youtube_regex = r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/.*(?:/|v=)([a-zA-Z0-9_-]+)'
  53. return bool(re.match(youtube_regex, text))
  54.  
  55. # Main function that listens for the hotkey and processes the clipboard
  56. def main():
  57. print("Press Ctrl+Shift+V to process the clipboard...")
  58.  
  59. # Loop to listen for Ctrl + Shift + V
  60. while True:
  61. # If Ctrl+Shift+V is pressed
  62. if keyboard.is_pressed('ctrl+shift+v'):
  63. clipboard_content = pyperclip.paste() # Get current clipboard content
  64.  
  65. if is_youtube_link(clipboard_content):
  66. print("Detected YouTube link, downloading...")
  67. download_youtube(clipboard_content)
  68. else:
  69. print("Detected non-YouTube text, saving to file...")
  70. filename = save_text_to_file(clipboard_content)
  71.  
  72. # Replace clipboard with the sanitized filename (stripped of file extension) whether save succeeds or fails
  73. pyperclip.copy(filename) # Replace clipboard with the filename (without the .txt extension)
  74.  
  75. # Prevent multiple triggers by waiting a little
  76. time.sleep(1) # Sleep for 1 second
  77.  
  78. # Prevent CPU overuse
  79. time.sleep(0.1)
  80.  
  81. # Make the script persistent by running it on startup
  82. if __name__ == '__main__':
  83. # Add the script to the Windows startup folder
  84. startup_path = os.path.expanduser(r'~\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup')
  85. script_name = os.path.basename(__file__)
  86. script_path = os.path.abspath(__file__)
  87.  
  88. if not any(script_name in f for f in os.listdir(startup_path)):
  89. os.symlink(script_path, os.path.join(startup_path, script_name))
  90.  
  91. # Run the main loop
  92. main()
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement