Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import time
- import pyperclip
- import yt_dlp as ytdl
- import keyboard
- import re
- from datetime import datetime
- # Set the save directory to Downloads folder
- save_directory = os.path.join(os.path.expanduser('~'), 'Downloads', 'ClipboardFiles') # Saves to "ClipboardFiles" folder in Downloads
- # Create the folder if it doesn't exist
- if not os.path.exists(save_directory):
- os.makedirs(save_directory)
- # Function to download YouTube video using yt-dlp
- def download_youtube(url):
- try:
- ydl_opts = {
- 'outtmpl': os.path.join(save_directory, '%(title)s.%(ext)s'), # Save with video title in the specified folder
- }
- with ytdl.YoutubeDL(ydl_opts) as ydl:
- ydl.download([url])
- print(f"Downloaded: {url}")
- except Exception as e:
- print(f"Error downloading video: {e}")
- # Function to sanitize and generate a filename from the clipboard content
- def sanitize_filename(text):
- # Remove all non-alphanumeric characters and limit length to 50 characters
- sanitized = re.sub(r'[^a-zA-Z0-9]', '_', text) # Replace anything that's not alphanumeric with underscores
- sanitized = sanitized[:50] # Truncate to the first 50 characters
- return sanitized
- # Function to save text from clipboard to a file and replace clipboard with filename
- def save_text_to_file(text):
- filename = sanitize_filename(text) # Generate a sanitized filename based on clipboard content
- try:
- # Save text to the file in the specified directory
- file_path = os.path.join(save_directory, filename + ".txt")
- with open(file_path, 'w') as file:
- file.write(text)
- print(f"Saved text to {file_path}.")
- return filename # Return the sanitized filename for clipboard replacement
- except Exception as e:
- print(f"Error saving text to file: {e}")
- return filename # Return the sanitized filename, even if saving failed
- # Check if clipboard contains a YouTube URL
- def is_youtube_link(text):
- youtube_regex = r'(https?://)?(www\.)?(youtube|youtu|youtube-nocookie)\.(com|be)/.*(?:/|v=)([a-zA-Z0-9_-]+)'
- return bool(re.match(youtube_regex, text))
- # Main function that listens for the hotkey and processes the clipboard
- def main():
- print("Press Ctrl+Shift+V to process the clipboard...")
- # Loop to listen for Ctrl + Shift + V
- while True:
- # If Ctrl+Shift+V is pressed
- if keyboard.is_pressed('ctrl+shift+v'):
- clipboard_content = pyperclip.paste() # Get current clipboard content
- if is_youtube_link(clipboard_content):
- print("Detected YouTube link, downloading...")
- download_youtube(clipboard_content)
- else:
- print("Detected non-YouTube text, saving to file...")
- filename = save_text_to_file(clipboard_content)
- # Replace clipboard with the sanitized filename (stripped of file extension) whether save succeeds or fails
- pyperclip.copy(filename) # Replace clipboard with the filename (without the .txt extension)
- # Prevent multiple triggers by waiting a little
- time.sleep(1) # Sleep for 1 second
- # Prevent CPU overuse
- time.sleep(0.1)
- # Make the script persistent by running it on startup
- if __name__ == '__main__':
- # Add the script to the Windows startup folder
- startup_path = os.path.expanduser(r'~\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup')
- script_name = os.path.basename(__file__)
- script_path = os.path.abspath(__file__)
- if not any(script_name in f for f in os.listdir(startup_path)):
- os.symlink(script_path, os.path.join(startup_path, script_name))
- # Run the main loop
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement