#!/usr/bin/env bash
#set -o xtrace
set -o nounset
set -o errexit
set -o pipefail
URL="$1"
DIR="$PWD"
FILENAME=$(yt-dlp --print filename -o "%(title)s" "$URL")
FILE_JSON="$DIR/$FILENAME".json
FILE_COMMENTS="$DIR/$(date +"%y%m%d") $FILENAME".txt
yt-dlp --write-comments --dump-single-json "$URL" > "$FILE_JSON"
echo -e "$URL\n" > "$FILE_COMMENTS"
cat "$FILE_JSON" | jq -r '.title' >> "$FILE_COMMENTS"
echo "--------------------------------------------------" >> "$FILE_COMMENTS"
cat "$FILE_JSON" | jq -r '"Просмотров: \(.view_count) Лайков: \(.like_count) Комментариев: \(.comment_count)"' >> "$FILE_COMMENTS"
echo "--------------------------------------------------" >> "$FILE_COMMENTS"
cat "$FILE_JSON" | jq -r '.comments[] | if .parent == "root" then "\n+\(.like_count) \(.author) >>> \(.text)" else "\t+\(.like_count) \(.author) >>> \(.text)" end' >> "$FILE_COMMENTS"