#!/bin/sh
# 1.sh
# usage: 1.sh query
# usage: 1.sh < query.yts >> query.yts
_x2_(){ local x=$(printf '\r\n');sed "s/ *//;s/$/$x/";};
case $# in :-)
;;0) # continuation search
KEY=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8;
MIN=\"context\":{\"client\":{\"hl\":\"en-US\",\"gl\":\"US\",\"clientName\":\"MWEB\",\"clientVersion\":\"2.20210916.06.01\"}},\"continuation\":;
TOKEN=$(sed -n "s/\"token\":/$MIN/p"|sed -n \$p);
_x2_ <<eof|nc -vvn 127.8 80|yy045|yy059
POST /youtubei/v1/search?key=$KEY HTTP/1.1
Host: www.youtube.com
Content-Length: ${#TOKEN}
Content-Type: application/json
Connection: close
$TOKEN
eof
;;*) # initial search
S=$(echo "$@"|sed 's/ /+/g');
_x2_ <<eof|nc -vvn 127.8 80|yy045|yy059|sed -n "/\"responseContext\":/,/\"searchSubMenu\":/w$S.yts"
GET /results?search_query="$S" HTTP/1.1
Host: www.youtube.com
Connection: close
eof
esac;
For an alternative way to search YouTube from the command line and create TSV tables of results, see, e.g.,https://github.com/MarcoLucidi01/bin/blob/master/ytsearch
No support for continuation searches unfortunately.