4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

bashでYouTubeライブのコメント(チャットのリプレイ)をサクッと取得する

Last updated at Posted at 2020-08-02

事前の準備

  • ライブ配信アーカイブの/live_chat_replay?continuation=XXX XXX部分の値を取得する

ScreenShot.png

Script


#/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

実行

  • 事前準備で取得したXXXを第一引数で実行する
% bash ./run.sh XXX

実行結果

% head -n 20 ./comment_*.tsv
		
-0:04	M M	来ました!笑
-0:01	寒色系ブルー	おつ
-0:01	しゅーへー	あ
0:00	アルカディア	おお!
0:06	私はダメです	!
0:07	Rena	こんばんは
0:07	REX	えいやっさ!
0:10	。 a	楽しみ〜
0:10	y t	きた!
0:10	ずんだ	はよ
0:11	ルビリアルビィ	こんばんは!
0:12	天下の大泥棒石川五右衛門	おお
0:12	しゅんちゃん!	!
0:12	。くろ	やべぇ!
0:13	きなこきなこ	いぇい
0:13	黒崎嶺二	こんにちは
0:15	渡邉りさちゃん	あああああ
0:16	ふにゃっしー	こんばんはわ
0:17	しゅーへー	にコメ
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?