#!/bin/sh
#Youtube Browser in ELinks:
# Usage: ytb [MAX RESULTS] URL
#
# In ELinks: "<" ">" to change tabs/results, "W" to wrap text.
# Enable option "document.plain.display_links" in ELinks config. for download/viewable video/image URLs.
#
# "less" could be used instead of ELinks, with navigation between results using the ":n" and ":p"  commands.
# Some X terminal programs can detect URLs by themselves.

YTBD='youtube-dl -e -g --get-thumbnail --get-description --get-duration'
YTBE='sed -e s/_webp// -e s/\.webp/.jpg/ -e s/.jpg\?.*$/.jpg/'
YTBS='csplit -z -f /tmp/yttmp -b %03d.txt - /^[0-9:]*[0-5][0-9]$/1 {*}'
YTBF='/tmp/yttmp*.txt'

[ "${1##*[!0-9]*}" ] && $YTBD --playlist-end $1 "$2"|$YTBE|$YTBS || $YTBD "$1"|$YTBE|$YTBS
elinks $YTBF
rm -f $YTBF