From 9e3cdc25e9ab9ae843c031010ee3fc407bb4904f Mon Sep 17 00:00:00 2001 From: HF Date: Sat, 2 May 2020 02:54:28 +0200 Subject: [PATCH] add logging helper script and update historyDownload script --- utils/README.md | 4 +++ utils/historyDownload.py | 9 +++-- utils/liveLog.sh | 75 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 2 deletions(-) create mode 100755 utils/liveLog.sh diff --git a/utils/README.md b/utils/README.md index bfb05bb..5969020 100644 --- a/utils/README.md +++ b/utils/README.md @@ -51,5 +51,9 @@ same as historyDownload, just that its designed for running on the storage serve ## backupSync.sh shell script that can be launched with backup.js to sync to a storage server after every backup. It uses rsync which is much faster than ftp, sftp or any other methode +## liveLog.sh +shell script that watches the pixel.log file and outputs the stats of the current IPs placing there +Usage: `./liveLog.sh LOGFILE CANVASID STARTX_STARTY ENDX_ENDY` + ## pp-center\*.png center logo of pixelplanet diff --git a/utils/historyDownload.py b/utils/historyDownload.py index a8d7971..f01f557 100755 --- a/utils/historyDownload.py +++ b/utils/historyDownload.py @@ -68,8 +68,13 @@ async def get_area(x, y, w, h, start_date, end_date): cnt += 1 #frames.append(image.copy()) image.save('./timelapse/t%s.png' % (cnt)) - async with session.get('https://pixelplanet.fun/api/history?day=%s&id=%s' % (iter_date, canvas_id)) as resp: - time_list = json.loads(await resp.text()) + while True: + async with session.get('https://pixelplanet.fun/api/history?day=%s&id=%s' % (iter_date, canvas_id)) as resp: + try: + time_list = json.loads(await resp.text()) + break + except: + print('Couldn\'t decode json for day %s, trying again' % (iter_date)) i = 0 for time in time_list: i += 1 diff --git a/utils/liveLog.sh b/utils/liveLog.sh new file mode 100755 index 0000000..b50db92 --- /dev/null +++ b/utils/liveLog.sh @@ -0,0 +1,75 @@ +#!/bin/bash +# this script parses the pixellogs live and shows which IPs are currently active in +# a given area and where they placed their last pixel +# Usage: ./liveLog.sh LOGFILE CANVASID STARTX_STARTY ENDX_ENDY +LOGFILE=$1 +CANVAS=$2 +STARTCOORDS=$3 +ENDCOORDS=$4 +STARTX=`echo ${STARTCOORDS} | sed 's/_.*$//'` +STARTY=`echo ${STARTCOORDS} | sed 's/^.*_//'` +ENDX=`echo ${ENDCOORDS} | sed 's/_.*$//'` +ENDY=`echo ${ENDCOORDS} | sed 's/^.*_//'` + +if [ "$#" -ne 4 ] +then + echo " Usage: ./liveLog.sh LOGFILE CANVASID STARTX_STARTY ENDX_ENDY" + echo "" + echo "this script parses the pixellogs live and shows which IPs are currently active in " + echo "a given area and where they placed their last pixel" + exit 1 +fi + + +parse_log() +{ + while read -r -a args + do + CAN=${args[2]} + X=${args[3]} + Y=${args[4]} + if [ "$CAN" -eq "$CANVAS" -a "$X" -ge "$STARTX" -a "$X" -le "$ENDX" -a "$Y" -ge "$STARTY" -a "$Y" -le "$ENDY" ] + then + IP=${args[0]} + CLR=${args[6]} + printf "%-40s | %-18s | %5s\n" "$IP" "$X,$Y" "$CLR" + fi + done <&0 +} + +declare -A ACTIVEIPS +parse_log_active_ips() +{ + while read -r -a args + do + CAN=${args[2]} + X=${args[3]} + Y=${args[4]} + if [ "$CAN" -eq "$CANVAS" -a "$X" -ge "$STARTX" -a "$X" -le "$ENDX" -a "$Y" -ge "$STARTY" -a "$Y" -le "$ENDY" ] + then + IP=${args[0]} + if [ -z "${ACTIVEIPS[$IP]}" ] + then + CNT=0 + else + CNT=`echo ${ACTIVEIPS[$IP]} | sed 's/ .*//'` + fi + CNT=$((${CNT} + 1)) + CLR=${args[6]} + ACTIVEIPS[$IP]="$CNT $IP $X,$Y $CLR" + print_active_ips | sort -rV + fi + done <&0 +} + +print_active_ips() +{ + clear + for IP in "${!ACTIVEIPS[@]}" + do + printf "%-7s | %-40s | %-18s | %5s\n" ${ACTIVEIPS[$IP]} + done +} + + +tail -f ${LOGFILE} | parse_log_active_ips