#!/usr/bin/env python # coding=utf-8 """ Description: Qutebrowser userscript. Allows you to download and watch a youtube video Keyboard binding: config.bind("\\", 'hint all userscript watchyt.py') Config file location: ~/.config/qutebrowser/config.py Script file location: ~/.local/share/qutebrowser/userscripts/watchyt.py Manually set keybinding: :bind \ hint all userscript watchyt.py Usage: Press \ then the hint keys for the video you wish to watch. You must select the video title not the image This is my first Qutebrowser plugin so it is pretty basic right now. Why download and watch when you can stream to mpv? mpv has a thumbnails plugin that doesn't work when streaming. You don't have to deal with buffering when it is already downloaded. """ from __future__ import unicode_literals import os import youtube_dl from executor import execute from lxml import html from qutescript import userscript @userscript def watch_youtube(request): # TODO: Add title, url, img, filepath to a database # TODO: Make webpage to watch the downloaded videos # dom = html.parse(request.html) # out = dom.find(".//title").text # out = "
\n".join([f"{j}: {k}" for j, k in os.environ.items()]) # title = os.environ["QUTE_SELECTED_TEXT"].strip() # TODO: Verify it is a video link. Not an image. If image then search for correct URL in dom url = os.environ["QUTE_URL"].strip() # href = url.split("/")[-1] # img = dom.xpath(f".//a[@id='thumbnail' and @href='/{href}']/yt-img-shadow/img")[0].get("src") # out += f"

\n\n{title} - {url} - /{href} - {img}" # TODO: Use fallback formats incase 18 is not available. with youtube_dl.YoutubeDL({ "restrictfilenames": True, "format": "18", "outtmpl": "/yt/%(upload_date)s_%(title)s-%(id)s.%(ext)s", }) as ydl: info_dict = ydl.extract_info(url) filepath = ydl.prepare_filename(info_dict) execute(f"mpv {filepath}") # request.send_html(out) if __name__ == "__main__": watch_youtube()