#!/bin/bash# configurable env settingspipe=${pipe:-/tmp/viewcount-control}db=${db:-viewer.db}wndb=${wndb:-wn.db}table=${table:-viewer}interval=${interval:-20}poll_interval=${poll_interval:-60}# Log a timestamped message to stdout.# $1 - messagefunction log(){echo`date "+%Y-%m-%dT%H:%M:%S "`$1}# Make sure the pipe for sending commands to listen() exists.function initialize(){ log "init - $pipe"[ -p "$pipe"]|| mkfifo "$pipe"}# Run collect.sh and store data in $db.# $1 - urlfunction collect(){url=$1video_id=${1#*=} log "collect - begin - $video_id" env db="$db"table="$table"interval="$interval" ./collect.sh "$url"&& log "collect - end - $video_id"}# Print the latest non-auPAY video_id in schedule table.function latest_video_id(){echo"SELECT video_id FROM schedule WHERE segment_id != 8 AND video_id IS NOT NULL ORDER BY jst DESC LIMIT 1;"| sqlite3 -cmd ".timeout 5000"$wndb# wait for db to unlock: https://stackoverflow.com/a/57453186}# Query $wndb for new video_ids periodically and send messages to listen# when a new video_id is discovered. Also keep track of video_ids that# have already been processed in $seen.function poll_video_ids(){# dictionary of seen video_idsdeclare -A seen
video_id=$(latest_video_id)whiletrue;doif[["${seen[$video_id]}" -eq 1]];then#log "poll - seen $video_id already"trueelseurl="https://www.youtube.com/watch?v=$video_id" log "poll - new stream $url"echo"$url" > $pipe seen[$video_id]=1fi sleep $poll_intervalvideo_id=$(latest_video_id)done}# Read commands from $pipe and execute them.function listen(){# monitor pipe for YouTube stream URLswhileread cmd < $pipe;doif[["$cmd"== http* ]];then collect $cmd&elif[["$cmd"==test]];then log "test - test"else log "unknown - $cmd"fidone}log "pid - $$"log "settings - pipe=$pipe"log "settings - db=$db"log "settings - wndb=$wndb"log "settings - table=$table"log "settings - interval=$interval"log "settings - poll_interval=$poll_interval"initialize
(sleep 1; poll_video_ids &)listen
Warning
LINK
You are about to visit a link which has been flagged with the above content warnings. Do you wish to continue?