Last active
April 14, 2022 06:06
-
-
Save hibiyasleep/837bc67c02253b40f831336caea0489f to your computer and use it in GitHub Desktop.
Watch following/followers and generates diff. (crontab required)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# (only tested with zsh, anyway) | |
# | |
# followlog.sh | |
# | |
# (c) 2016 Kuriyama hibiya | |
# All rights reserved. | |
# | |
# Usage: | |
# | |
# 1. Place this script with a directory named as account name to track. | |
# 2. Attach this to crontab like this: (ACCOUNT_TO_READ should be logged in .trc) | |
# */4 * * * * /home/hibiya/followlog/crontab.sh <READER_ACCOUNT> 2> /dev/null | |
# 3. Wait for mail from cron. | |
# | |
# Warning: | |
# | |
# * target account SHOULD visible by READER_ACCOUNT. | |
# logging is unavailable when: | |
# * target account blocked READER_ACCOUNT | |
# * target account is protected, but READER_ACCOUNT doesn't follow target | |
# * etc | |
# * Twitter's API limited to 15 request / 15 minutes, so you cannot set | |
# cron rule's divider (*/n) more than count of accounts. | |
# | |
# Shell/Env Requirements: | |
# | |
# * `dir/*(.)` | |
# * GNU sed | |
# | |
# Requirements: | |
# https://github.com/sferik/t | |
# | |
cd "$(dirname "$0")" | |
d=$(date +%Y/%m) | |
file=$(date +%d-%H-%M) | |
t set account $1 > /dev/null | |
function load { | |
# $1: 'followers', 'followings' | |
# $2: account name | |
# $3: retval | |
local dir=$2/$1/$d | |
local csv=$(t $1 --csv $2 2> /dev/null) | |
local header="[$1/@$2]" | |
# limited | |
if [ "$?" -ne "0" ]; then | |
>&2 echo "$header\tRate Limited!" | |
exit 1 | |
fi | |
if [ ${#csv} -lt 2 ]; then | |
>&2 echo "$header\tGot empty list!" | |
exit 1 | |
fi | |
list=$(echo $csv | grep -E '^[0-9]+,(.+,){8,}' | cut -d, -f1,9 | sort) | |
# first logging of month | |
if [ ! -d "$dir/diff" ]; then | |
>&2 echo "[$1/@$2]\tWriting first log." | |
mkdir -p $dir/diff 2> /dev/null | |
echo $list > $dir/$file | |
else | |
local lastlog=$(echo $dir/*(.) | tr ' ' '\n' | sort | tail -n 1) | |
local differ=$(echo $list | diff -a -u $lastlog - | grep -E "^[+-][^+-]") | |
local lines=$(echo "$differ" | wc -l) | |
echo "$differ" | grep -q "Binary file (standard input) matches" | |
if [ $? -eq 0 ]; then | |
>&2 echo "$header\tgrep treated stdin as binary!" | |
exit 1 | |
fi | |
# something changed | |
if [ -n "$differ" ]; then | |
if [ ! -f "$2/.no-mail" ]; then | |
echo $differ | sed -e "s/^/[$1\/@$2]\t/" | |
echo | |
fi | |
>&2 echo "$header\tdiffer!" | |
echo $list > $dir/$file | |
echo $differ > $dir/diff/$file | |
else | |
eval $1='' | |
>&2 echo "$header\tNothing changed." | |
fi | |
fi | |
} | |
for i in $(ls -d */); do | |
account=${i%%/} | |
load followings $account & | |
pid1=$! | |
load followers $account & | |
pid2=$! | |
wait $pid1 | |
wait $pid2 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment