Advertisement
Guest User

YT_URL_opener

a guest
Jun 8th, 2025
22
0
360 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | Software | 0 0
  1. import pynput
  2. import keyboard
  3. import pyperclip
  4. import win32gui
  5. import subprocess
  6. import time
  7. import logging
  8. from urllib.parse import urlparse
  9.  
  10. # pip install pynput keyboard pyperclip pywin32
  11.  
  12. # Configure logging to output to console
  13. logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(message)s')
  14.  
  15. # Path to MPC-HC executable (update if necessary)
  16. MPC_HC_PATH = r"C:\Program Files\MPC-HC\mpc-hc64.exe"
  17.  
  18. # Define the hotkey for the Firefox extension
  19. EXTENSION_HOTKEY = ['ctrl', 'shift', 'u'] # Ctrl+Shift+U for the addon
  20.  
  21. # Define the hotkey for the Python script
  22. SCRIPT_HOTKEY = ['alt', 'u'] # Alt+U to trigger the script
  23.  
  24. # Constant delay for key presses and releases (in seconds)
  25. KEY_DELAY = 0.2
  26.  
  27. def on_hotkey():
  28. """
  29. Triggered by Alt+U. Triggers a Firefox extension to copy the current tab's URL,
  30. then opens it in MPC-HC if it’s a valid YouTube URL.
  31. """
  32. logging.debug("Hotkey Alt+U detected")
  33.  
  34. try:
  35. # Check if Firefox is the active window
  36. hwnd = win32gui.GetForegroundWindow()
  37. window_title = win32gui.GetWindowText(hwnd)
  38. class_name = win32gui.GetClassName(hwnd)
  39. logging.debug(f"Active window: {window_title} (Class: {class_name})")
  40.  
  41. if class_name == "MozillaWindowClass":
  42. logging.debug("Firefox detected, proceeding with extension trigger")
  43.  
  44. time.sleep(0.5)
  45.  
  46. # Save current clipboard content
  47. original_clipboard = pyperclip.paste()
  48. logging.debug(f"Original clipboard: {original_clipboard}")
  49.  
  50. # Simulate the Firefox extension hotkey dynamically
  51. for key in EXTENSION_HOTKEY:
  52. keyboard.press(key)
  53. time.sleep(KEY_DELAY) # Use constant delay for each press
  54. for key in reversed(EXTENSION_HOTKEY):
  55. keyboard.release(key)
  56. time.sleep(KEY_DELAY) # Use constant delay for each release
  57. logging.debug(f"Simulated extension hotkey: {EXTENSION_HOTKEY}")
  58. time.sleep(1) # Wait for the extension to copy the URL
  59.  
  60. # Retrieve the URL from the clipboard
  61. url = pyperclip.paste()
  62. logging.debug(f"Copied URL: {url}")
  63.  
  64. # Check if clipboard content changed
  65. if url != original_clipboard:
  66. logging.debug("Clipboard updated successfully")
  67.  
  68. # Validate the URL
  69. parsed = urlparse(url)
  70. if parsed.scheme and parsed.netloc:
  71. logging.debug("URL is valid")
  72. if parsed.netloc.endswith('youtube.com') or parsed.netloc == 'youtu.be':
  73. subprocess.Popen([MPC_HC_PATH, url])
  74. logging.debug("Opened YouTube URL in MPC-HC")
  75. else:
  76. logging.debug("Not a YouTube URL")
  77. else:
  78. logging.debug("Invalid URL")
  79. else:
  80. logging.debug("Clipboard unchanged, extension may have failed")
  81. else:
  82. logging.debug("Active window is not Firefox, ignoring")
  83. except Exception as e:
  84. logging.error(f"Error: {e}")
  85.  
  86. # Dynamically construct the hotkey string for pynput
  87. # Modifiers need < >, regular keys do not
  88. modifiers = {'ctrl', 'shift', 'alt', 'cmd'}
  89. hotkey_parts = [f'<{key}>' if key in modifiers else key for key in SCRIPT_HOTKEY]
  90. hotkey_string = '+'.join(hotkey_parts)
  91.  
  92. # Set up the global hotkey listener
  93. with pynput.keyboard.GlobalHotKeys({
  94. hotkey_string: on_hotkey
  95. }) as h:
  96. logging.info(f"Script started, listening for {SCRIPT_HOTKEY}")
  97. h.join()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement