Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pynput
- import keyboard
- import pyperclip
- import win32gui
- import subprocess
- import time
- import logging
- from urllib.parse import urlparse
- # pip install pynput keyboard pyperclip pywin32
- # Configure logging to output to console
- logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(message)s')
- # Path to MPC-HC executable (update if necessary)
- MPC_HC_PATH = r"C:\Program Files\MPC-HC\mpc-hc64.exe"
- # Define the hotkey for the Firefox extension
- EXTENSION_HOTKEY = ['ctrl', 'shift', 'u'] # Ctrl+Shift+U for the addon
- # Define the hotkey for the Python script
- SCRIPT_HOTKEY = ['alt', 'u'] # Alt+U to trigger the script
- # Constant delay for key presses and releases (in seconds)
- KEY_DELAY = 0.2
- def on_hotkey():
- """
- Triggered by Alt+U. Triggers a Firefox extension to copy the current tab's URL,
- then opens it in MPC-HC if it’s a valid YouTube URL.
- """
- logging.debug("Hotkey Alt+U detected")
- try:
- # Check if Firefox is the active window
- hwnd = win32gui.GetForegroundWindow()
- window_title = win32gui.GetWindowText(hwnd)
- class_name = win32gui.GetClassName(hwnd)
- logging.debug(f"Active window: {window_title} (Class: {class_name})")
- if class_name == "MozillaWindowClass":
- logging.debug("Firefox detected, proceeding with extension trigger")
- time.sleep(0.5)
- # Save current clipboard content
- original_clipboard = pyperclip.paste()
- logging.debug(f"Original clipboard: {original_clipboard}")
- # Simulate the Firefox extension hotkey dynamically
- for key in EXTENSION_HOTKEY:
- keyboard.press(key)
- time.sleep(KEY_DELAY) # Use constant delay for each press
- for key in reversed(EXTENSION_HOTKEY):
- keyboard.release(key)
- time.sleep(KEY_DELAY) # Use constant delay for each release
- logging.debug(f"Simulated extension hotkey: {EXTENSION_HOTKEY}")
- time.sleep(1) # Wait for the extension to copy the URL
- # Retrieve the URL from the clipboard
- url = pyperclip.paste()
- logging.debug(f"Copied URL: {url}")
- # Check if clipboard content changed
- if url != original_clipboard:
- logging.debug("Clipboard updated successfully")
- # Validate the URL
- parsed = urlparse(url)
- if parsed.scheme and parsed.netloc:
- logging.debug("URL is valid")
- if parsed.netloc.endswith('youtube.com') or parsed.netloc == 'youtu.be':
- subprocess.Popen([MPC_HC_PATH, url])
- logging.debug("Opened YouTube URL in MPC-HC")
- else:
- logging.debug("Not a YouTube URL")
- else:
- logging.debug("Invalid URL")
- else:
- logging.debug("Clipboard unchanged, extension may have failed")
- else:
- logging.debug("Active window is not Firefox, ignoring")
- except Exception as e:
- logging.error(f"Error: {e}")
- # Dynamically construct the hotkey string for pynput
- # Modifiers need < >, regular keys do not
- modifiers = {'ctrl', 'shift', 'alt', 'cmd'}
- hotkey_parts = [f'<{key}>' if key in modifiers else key for key in SCRIPT_HOTKEY]
- hotkey_string = '+'.join(hotkey_parts)
- # Set up the global hotkey listener
- with pynput.keyboard.GlobalHotKeys({
- hotkey_string: on_hotkey
- }) as h:
- logging.info(f"Script started, listening for {SCRIPT_HOTKEY}")
- h.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement