首页 > 解决方案 > 如何“复制和粘贴”我的光标悬停在 Python 脚本路径中的 URL

问题描述

在这个问题下面的 Python 脚本中,我想要一个解决方案,它可以“复制并粘贴”我的光标悬停在一个路径上的 YouTube 视频的 URL,而不是...

cd && while ! /usr/bin/python3 /usr/local/bin/youtube-dl -f  'best[filesize<100M]' URL -c --socket-timeout 5; do echo DISCONNECTED; sleep 5; done

因此,例如,它可能看起来像这样......

cd && while ! /usr/bin/python3 /usr/local/bin/youtube-dl -f  'best[filesize<100M]' https://www.youtube.com/watch?v=18nZ7zUz84U -c --socket-timeout 5; do echo DISCONNECTED; sleep 5; done

我是 Python 初学者。我在下面编写了 kludgy Python 脚本,它实际上可以正常工作。当我在 Lubuntu 20.04 LTS 上运行该脚本时,我可以使用 youtube-dl 从 YouTube 下载视频。为了使下面的脚本正常工作,我的光标必须悬停在 YouTube 视频上。

这是我当前的 Python 脚本...

import time,pyautogui,pyperclip,subprocess
time.sleep(0.2)

path = "/home/scripts/text_scripts_copy/Text to copy into a terminal to download videos with youtube-dl.txt"
print("copying contents of ", path)
The_text_of_the_file_that_will_be_copied = open(path, 'r').read()
pyperclip.copy(The_text_of_the_file_that_will_be_copied)
time.sleep(3.0)

subprocess.Popen(["/usr/bin/mousepad"])
time.sleep(2.0)

pyautogui.press('f1')     # press the f1 key
# Why press the f1 key? Because I have assigned f1 to whow the items I have cut or copied and thereby added to clipboard manager which is CopyQ.
# I am using this approach because ctrl+v often fails when pasting from one application to another when I have CopyQ installed.
time.sleep(2.0)

pyautogui.typewrite(['enter'])
time.sleep(1.5)

subprocess.run(["wmctrl", "-a", "YouTube"])
time.sleep(1.0)

pyautogui.click(button='right') # right mouse click (I need be sure that the mouse pointer is hovering over the video I want to download).
time.sleep(1.5)

pyautogui.typewrite("l")  # After pressing the letter "l" (as in "little") this will copy the link to the video I want to download.
time.sleep(1.5)

time.sleep(1.5)
subprocess.run(["wmctrl", "-a", "mousepad"]) #this will got to the oldest menu with the word "mousepad" in it. Therefore, I need to be sure that no other instances of mousepad are running.
time.sleep(1.5)

pyautogui.typewrite(['end'])
time.sleep(1.0)

pyautogui.press('left', presses=59)
time.sleep(1.0)

pyautogui.press('backspace', presses=3)
time.sleep(1.0)

pyautogui.press('f1')
time.sleep(1.5)

pyautogui.typewrite(['enter'])
time.sleep(1.5)

pyautogui.hotkey('ctrl', 'a')  # ctrl-a to select all
time.sleep(1.0)

pyautogui.hotkey('ctrl', 'x')  # ctrl-x to cut all
time.sleep(1.0)

pyautogui.hotkey('alt', 'space')  # first part of quitting the application
time.sleep(1.0)

pyautogui.typewrite(['c']) # second part of quitting the application
time.sleep(1.0)

pyautogui.hotkey('alt', 'd')  # third and final part of quitting the application (this quits without saving).
time.sleep(1.0)

subprocess.run(["wmctrl", "-a", "y@y"]) # This brings my terminal window to the forefront.
time.sleep(2.0)

pyautogui.typewrite(['cd']) # In my terminal this changes the directory to my home directory so that the YouTube video that is about to be downloaded will be stored in my home directory.
time.sleep(2.0)

pyautogui.typewrite(['enter'])
time.sleep(1.0)
pyautogui.press('f1')
time.sleep(2.0)

pyautogui.typewrite(['enter'])
time.sleep(1.0)

pyautogui.typewrite(['enter'])

文件名为Text to copy into a terminal to download videos with youtube-dl.txt在上面的 Python 脚本中包含...

cd && while ! /usr/bin/python3 /usr/local/bin/youtube-dl -f  'best[filesize<100M]' URL -c --socket-timeout 5; do echo DISCONNECTED; sleep 5; done

我加入了首字母缩略词“URL”来提醒我,“记住,这是粘贴 URL 的位置。” 我上面的脚本删除了“URL”并将其替换为我的鼠标指针悬停在的 YouTube 视频的 URL(当然,这是我要下载的 YouTube 视频)。

标签: python-3.x

解决方案


推荐阅读