過去に投稿した記事
の投稿から7年、それからTwitterもⅩに替わってAPI投稿もいろいろ制限され、イーロンくんに翻弄されながらMastodonやBlueskyなどを渡り歩きながらも録画サーバーの状態表示を垂れ流していました。
でも、そもそもの話として自分しか見ない情報なので、それなら自分宛てにDiscodeの自分用の非公開サーバーを立てて、そこにメッセージで飛ばしてもらったほうがTLを汚している後ろめたさもないのでは?とようやく気付きました。
Discodeのwebfook、使ったことが無かったので食わず嫌いをしていました。
Twitter、Mastodon、Bluesky、LINE… いろいろなサービスを試してきましたがどれもAPIキーの取得手続きが面倒だったり、投稿用スクリプトを組まなければならない等の面倒がありましたが、Discodeのwebfookはとても簡単に利用できます。
チャンネルを作成
※録画サーバーの場合はプライベートチャンネルの設定を必ずONにして下さい

チャンネル編集から、連携サービス > ウェブフック
[ウェブフックURLをコピー] で取得できます

投稿用スクリプトを組む必要も無く、curlコマンドだけで完結
curl -s -X POST -F content="HOGEHOGE" https://discord.com/api/webhooks/0000000000000000000/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
と、いうわけで過去に作った録画通知スクリプトに組み込んでみました。投稿用スクリプトを必要としないのでとてもシンプルです。非公開にしたことで他人に覗かれる心配もないのでエンコード済の視聴用URLを飛ばすように処理も入れてみました、
エンコード完了を視聴用URL付きで知らせてくれるので、録画消化が捗ってとても便利になりました。
※視聴用URLについては非公開としたうえ、パスワード保護やVPN接続が必要なローカルアドレス等としたもので個人使用の範囲で運用してください
vi /usr/local/bin/foltwi
#!/bin/sh
#===================================
# foltia 録画・変換監視スクリプト
# Rel.260319 io_oj
# (chkconfigに登録して実行して下さい)
#===================================
export LANG=en_US.utf8
tweet() {
while read i
do
set -- $i
## 環境設定
PG=/opt/rh/rh-postgresql95/root/usr/bin
## Discode WebhookのURL
dcurl=https://discord.com/api/webhooks/0000000000000000000/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
svurl=http://192.168.1.100/tv/
## messagesログから録画開始を抽出
if [ "${6} ${7}" = "recwrap RECSTART" ]; then
## 抽出行から放送局コード、開始時刻、タイトルコード、話数を抽出
staid=${12}
stime=$(echo ${14} | sed -e 's/--/-#-/g' | cut -d '-' -f4)
stim2=$(echo ${14} | sed -e 's/--/-#-/g' | cut -d '-' -f3)
etime=$(date --date "${stime} ${11} sec" +%H%M)
ttlid=${15}
epsde=${16}
tsfil=0
## ツイートを有効にする場合は次の行のコメントアウトを外して下さい
twisub 🔴録画開始 話 0
fi
## messagesログから録画完了を抽出
if [ "${6} ${7}" = "digitaltvrecording RECEND" ]; then
## 抽出行から放送局コード、開始時刻、タイトルコード、話数を抽出
staid=${11}
stime=$(echo ${13} | sed -e 's/--/-#-/g' | cut -d '-' -f4)
stim2=$(echo ${13} | sed -e 's/--/-#-/g' | cut -d '-' -f3)
etime=$(date --date "${stime} ${10} sec" +%H%M)
ttlid=${14}
epsde=${15}
tsfil=${13}
## ツイートを有効にする場合は次の行のコメントアウトを外して下さい
twisub 🟦録画終了 話 1
fi
## messagesログからradikoの録音開始を抽出
if [ "${6} ${11}" = "addpidatq -2" ]; then
## 抽出行から放送局コード、開始時刻、タイトルコード、話数を抽出
staid=${17}
stime=$(date --date "$(echo ${8} | cut -c 9-12) 1 min" +%H%M)
etime=$(date --date "${stime} ${12} sec" +%H%M)
ttlid=${14}
epsde=${15}
tsfil=0
## ツイートを有効にする場合は次の行のコメントアウトを外して下さい
twisub 🔴録音開始 回 0
fi
## messagesログからradikoの録音完了を抽出
if [ "${6} ${7}" = "digitalradiorecording Finish" ]; then
## 抽出行から放送局コード、開始時刻、タイトルコード、話数を抽出
staid=69
stime=$(echo ${8} | cut -d '-' -f5)
etime=$(date --date "${3} 6 min ago" +%H%M)
ttlid=64
epsde=0
tsfil=0
## ツイートを有効にする場合は次の行のコメントアウトを外して下さい
twisub 🟦録音終了 回 0
fi
## messagesログからエンコード開始を抽出
if [ "${6} ${7}" = "ipodtranscode BonTsDemux" ]; then
## 抽出行から放送局コード、開始時刻、タイトルコード、話数を抽出
tsfil=$(echo "${8}" | sed -e 's/\/.*\///g')
# EPG予約時の連続ハイフン等を考慮してパース
ttlid=$(echo "${tsfil}" | sed -e 's/^-/#/g' -e 's/--/-#-/g' | cut -d '-' -f1 | sed -e 's/#/-/g')
epsde=$(echo "${tsfil}" | sed -e 's/^-/#/g' -e 's/--/-#-/g' | cut -d '-' -f2 | sed -e 's/#/-1/g')
stim2=$(echo "${tsfil}" | sed -e 's/^-/#/g' -e 's/--/-#-/g' | cut -d '-' -f3)
stime=$(echo "${tsfil}" | sed -e 's/^-/#/g' -e 's/--/-#-/g' | cut -d '-' -f4)
digch=$(echo "${tsfil}" | sed -e 's/^-/#/g' -e 's/--/-#-/g' -e 's/\./-/g'| cut -d '-' -f5)
stanm=$(ssh root@127.0.0.1 "$PG/psql -q -U foltia -d foltia --no-align -t -c \"select stationname from foltia_station where digitalch='${digch}';\" | head -n 1")
## 取得した内容を表示フォーマットに埋め込んで整形します
# TIDが -1 もしくは 話数が -1 の場合はEPG録画として扱う
if [ "${ttlid}" = "-1" ] || [ "${epsde}" = "-1" ]; then
wdtmp="ℹ️録画ファイルのエンコードを開始しました"
## EPG録画の場合の表示フォーマット整形
eps_part=""
intwd=$(echo -e "${wdtmp} 📔${stime:0:2}:${stime:2:2} [${stanm}]")
else
wdtmp="ℹ️録画ファイルのエンコードを開始しました"
## 番組予約モードの場合の表示フォーマット整形
title=$(ssh root@127.0.0.1 "$PG/psql -q -U foltia -d foltia --no-align -t -c \"select title from foltia_program where tid='${ttlid}';\"")
sbttl=$(ssh root@127.0.0.1 "$PG/psql -q -U foltia -d foltia --no-align -t -c \"select foltia_subtitle.subtitle from foltia_subtitle left join foltia_station on foltia_subtitle.stationid = foltia_station.stationid where tid='${ttlid}' and countno='${epsde}' and digitalch='${digch}';\" | head -n 1")
# 話数が正の整数の場合のみ #番号 を付与
eps_part=""
if [[ "${epsde}" =~ ^[0-9]+$ ]] && [ "${epsde}" -gt 0 ]; then
eps_part="${epsde}"
fi
# サブタイトルがある場合のみ 『タイトル を付付与
sb_part=""
if [ -n "${sbttl}" ]; then
sb_part="『${sbttl}"
fi
intwd=$(echo -e "${wdtmp} 📔${stime:0:2}:${stime:2:2} [${stanm}] ${title} #${eps_part} ${sb_part}")
fi
## 文字列整形と出力
twiwd=$(echo -e "${intwd:0:128}$([ ${#intwd} -gt 128 ] && echo '...』' || [ -n "${sbttl}" ] && echo '』')")
m4url=${svurl}${ttlid}.localized/mp4/MHD-${ttlid}-${eps_part}-${stim2}-${stime}-${digch}.MP4
curl -s -X POST -F content="${twiwd} ${m4url}" ${dcurl} > /dev/null ## discode
echo -e "$(date "+%Y/%m/%d %H:%M:%S") $i" >>/var/log/foltwi.log
echo -e "$(date "+%Y/%m/%d %H:%M:%S") ${twiwd} ${m4url}" >>/var/log/foltwi.log
fi
## messagesログからエンコード開始を抽出(デフォルトではログ出力のみ)
# if [ "${6} ${7}" = "ipodtranscode BonTsDemux" ]; then
# twiwd=$(echo -e "ℹ️録画ファイルのエンコードを開始しました")
# ## ツイートを有効にする場合は次の行のコメントアウトを外して下さい
# curl -s -X POST -F content="${twiwd:0:128}" ${dcurl} > /dev/null ## discode
# echo -e "$(date "+%Y/%m/%d %H:%M:%S") $i" >>/var/log/foltwi.log
# echo -e "$(date "+%Y/%m/%d %H:%M:%S") ${twiwd:0:128}" >>/var/log/foltwi.log
# fi
## messagesログからエンコード完了を抽出(デフォルトではログ出力のみ)
if [ "${6} ${7} ${8}" = "ipodtranscode ALL COMPLETE" ]; then
twiwd=$(echo -e "ℹ️録画ファイルのエンコードが完了しました")
## ツイートを有効にする場合は次の行のコメントアウトを外して下さい
curl -s -X POST -F content="${twiwd:0:128}" ${dcurl} > /dev/null ## discode
echo -e "$(date "+%Y/%m/%d %H:%M:%S") $i" >>/var/log/foltwi.log
echo -e "$(date "+%Y/%m/%d %H:%M:%S") ${twiwd:0:128}" >>/var/log/foltwi.log
fi
## messagesログから録画失敗を抽出
if [ $(echo $i | grep -c "recwrap ERR 2:Device busy;Giving up digital recording.") = 1 ]; then
twiwd=$(echo -e "⚠録画開始できませんでした")
## ツイートを有効にする場合は次の行のコメントアウトを外して下さい
curl -s -X POST -F content="${twiwd:0:128}" ${dcurl} > /dev/null ## discode
echo -e "$(date "+%Y/%m/%d %H:%M:%S") $i" >>/var/log/foltwi.log
echo -e "$(date "+%Y/%m/%d %H:%M:%S") ${twiwd:0:128}" >>/var/log/foltwi.log
fi
done
}
twisub() {
## 環境設定
PG=/opt/rh/rh-postgresql95/root/usr/bin
## タイトルコードからの番組名をDBから取得
title=$(ssh root@127.0.0.1 "$PG/psql -q -U foltia -d foltia --no-align -t -c 'select title from foltia_program where tid='${ttlid}';'")
## タイトルコードと話数からのサブタイトルをDBから取得
sbttl=$(ssh root@127.0.0.1 "$PG/psql -q -U foltia -d foltia --no-align -t -c 'select subtitle from foltia_subtitle where tid='${ttlid}' and countno='${epsde}' and stationid='${staid}';' | head -n 1")
## 放送局コードからの放送局名をDBから取得
stanm=$(ssh root@127.0.0.1 "$PG/psql -q -U foltia -d foltia --no-align -t -c 'select stationname from foltia_station where stationid='${staid}';'")
## 放送局コードからの局タグをDBから取得
sttag=$(ssh root@127.0.0.1 "$PG/psql -q -U foltia -d foltia --no-align -t -c 'select ext_stationtag from foltia_station where stationid='${staid}';'")
## タイトルコードからの番組名をしょぼいカレンダーから取得
ohurl=$(wget "http://cal.syoboi.jp/db.php?Command=TitleLookup&TID=${ttlid}&Fields=Comment" -O /dev/stdout 2>&1 | grep "http" | sed -n 2P | sed -e 's/^.*http/http/g' | sed -e 's/].*$//g')
## 引数1受け取り
wdtmp=$1
## TSファイル解析
if [ ${tsfil} != 0 ]; then
tstmp=$(/home/foltia/perl/tool/tsselect /home/foltia/php/tv/${tsfil} | awk -F'[=,]' '{ drop += $6 } { scr += $8} END{print drop","scr}')
if [ $(echo ${tstmp} | awk -F'[,]' '{print $1 + $2}') = 0 ]; then
wdtmp=🟦録画終了
tsqry="[Drop:0 Scr:0]"
else
wdtmp=⚠️録画終了
tsqry="[Drop:$(echo ${tstmp} | cut -d ',' -f 1) Scr:$(echo ${tstmp} | cut -d ',' -f 2)]"
fi
else
tsqry=
fi
## 取得した内容を表示フォーマットに埋め込んで整形します
if [ ${ttlid} = -1 ]; then
## EPG録画の場合の表示フォーマット整形
intwd=$(echo -e "${wdtmp} ${tsqry} 📔${stime:0:2}:${stime:2:2}-${etime:0:2}:${etime:2:2} [${stanm}] ${title} $([ ${#sbttl} -gt 0 ] && echo "『${sbttl}")")
else
## 番組予約モードの場合の表示フォーマット整形
intwd=$(echo -e "${wdtmp} ${tsqry} 📔${stime:0:2}:${stime:2:2}-${etime:0:2}:${etime:2:2} [${stanm}] ${title} $([ ${epsde} -gt 0 ] && echo "第${epsde}$2" ) $([ ${#sbttl} -gt 0 ] && echo "『${sbttl}")")
fi
## 文字数溢れした場合の省略処理と番組公式Web、局タグの追加
if [ $3 = 0 ]; then
## 番組公式Webと局タグをつける(引数3を 0 にした場合) Summary Cardが埋め込まれます
twiwd=$(echo -e "${intwd:0:128}$([ ${#intwd} -gt 128 ] && echo '...』' || [ ${#sbttl} -gt 0 ] && echo '』') $([ ! ${#sttag} = 0 ] && echo "#${sttag} ")"'#foltia'" ${ohurl}")
else
## 番組公式Webと局タグをつけない(引数3を 1 にした場合) Summary Cardが埋め込まれるとウザい場合
intwd=$(echo -e "${intwd}")
twiwd=$(echo -e "${intwd:0:128}$([ ${#intwd} -gt 128 ] && echo '...』' || [ ${#sbttl} -gt 0 ] && echo '』') $([ ! ${#sttag} = 0 ] && echo "#${sttag} ")"'#foltia')
fi
## ツイート処理とログ出力処理
curl -s -X POST -F content="${twiwd}" ${dcurl} > /dev/null ## discode
echo -e "$(date "+%Y/%m/%d %H:%M:%S") $i" >>/var/log/foltwi.log
echo -e "$(date "+%Y/%m/%d %H:%M:%S") ${twiwd}" >>/var/log/foltwi.log
}
start(){
printf "%s\n" starting:
## 多重起動すると厄介なので実質Restart処理です
CMD0="tail -n 0 --follow=name --retry /var/log/foltia.log"
# CMD0="tail -n 0 -F /var/log/foltia.log"
PID0=$(ps -e -o pid,cmd | sed "s/^ //g" | grep -v "grep" | grep -v $$ | grep "$CMD0" | cut -f 1 -d " ")
if [ -n "$PID0" ];
then kill $PID0
fi
$CMD0 | tweet &
PID3=$(ps -e -o pid,cmd | sed "s/^ //g" | grep -v "grep" | grep "$CMD0" | cut -f 1 -d " ")
echo $PID0 >/var/run/foltwi.pid
return 0
}
stop(){
printf "%s\n" stoping:
## プロセスリストからPIDを検索して終了処理をかけます
CMD1="tail -n 0 --follow=name --retry /var/log/foltia.log"
# CMD1="tail -n 0 -F /var/log/foltia.log"
PID1=$(ps -e -o pid,cmd | sed "s/^ //g" | grep -v "grep" | grep -v $$ | grep "$CMD1" | cut -f 1 -d " ")
if [ -n "$PID1" ];
then kill $PID1
fi
CMD2="/bin/sh /usr/local/bin/foltwi.sh"
PID2=$(ps -e -o pid,cmd | sed "s/^ //g" | grep -v "grep" | grep -v $$ | grep "$CMD2" | cut -f 1 -d " ")
if [ -n "$PID2" ];
then kill $PID2
rm -f /var/run/foltwi.pid
fi
return 0
}
restart(){
start
return 0
}
case $1 in
start)
start
;;
stop)
stop
;;
*)
start
;;
esac
