Skip to content

Instantly share code, notes, and snippets.

@MarkWalters-dev
Created February 25, 2021 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarkWalters-dev/92727c86d9fc98328010ee851931891c to your computer and use it in GitHub Desktop.
Save MarkWalters-dev/92727c86d9fc98328010ee851931891c to your computer and use it in GitHub Desktop.
Qutebrowser userscript. Allows you to download and watch a youtube video
#!/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 = "<br>\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"<p>\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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment