summaryrefslogtreecommitdiff
path: root/.config/qutebrowser/userscripts/youtube-dl
blob: 5cd71e60636386fa959ef6daab2b81af4b7c68a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/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()