#/bin/bash
NEXT_URL=${1}
readonly API='https://www.youtube.com/live_chat_replay?continuation='
readonly UA='User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
readonly TS=$(date +%s)
function get_comment_json(){
curl -s -H "${UA}" ${1} | \
grep 'responseContext'| \
sed -e 's/window\["ytInitialData"\]\ =\ //' -e 's/;$//' \
> /tmp/out.json
}
function output_comment(){
cat /tmp/out.json |jq -r '.continuationContents[].actions[].replayChatItemAction.actions[].addChatItemAction.item.liveChatTextMessageRenderer| [.timestampText.simpleText , .authorName.simpleText , .message.runs[0].text , .message.runs[1].text]|@tsv' 2>/dev/null| \
awk -F '\t' '{print $1 "\t" $2 "\t" $3 $4 }' \
>> ./comment_${TS}.tsv
}
while [ "${NEXT_URL}" != 'null' ]
do
get_comment_json ${API}${NEXT_URL}
output_comment
NEXT_URL=$(cat /tmp/out.json |jq -r .continuationContents.liveChatContinuation.continuations[0].liveChatReplayContinuationData.continuation 2>/dev/null)
sleep 1
done
rm /tmp/out.json
exit 0