Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter stats using gnuplot, json & gnu make
#!/usr/bin/make -sf
$(if $(i),,$(error Usage: twitter-stats i=twitter-YYYY-MM-DD-sha256.zip [lang|users|1.png|1.svg]))
n=5
top: tweets.json
json -e 'this.tweet.permalink = `https://twitter.com/i/web/status/$${this.tweet.id}`; this.tweet.rating = Number(this.tweet.favorite_count) + Number(this.tweet.retweet_count)' -c 'this.tweet.rating > 0' < $< | json -Ae 'this.sort((a,b)=> a.tweet.rating-b.tweet.rating)' | json -Ae 'this.q = this.slice(-$(n))' q | json -e 'this.q = {rating: this.tweet.rating, date: this.tweet.created_at, text: this.tweet.full_text, permalink: this.tweet.permalink}' -a q
lang: tweets.json; json -a tweet.lang < $< | $(aggregate) | $(sort)
users: tweets.json
json -e 'this.users = this.tweet.entities.user_mentions.map( v => v.screen_name).join`\n`' -a users < $< | $(aggregate) | $(sort)
tweets.json: $(i) account.json; $(call mkjson,data/tweet.js)
account.json: $(i); $(call mkjson,data/account.js)
dates.txt: tweets.json
json -e 'd = new Date(this.tweet.created_at); p = s => ("0"+s).slice(-2); this.tweet.date = [d.getFullYear(), p(d.getMonth()+1), p(d.getDate())].join`-`' -a tweet.date < $< | $(aggregate) > $@
%.svg: dates.txt; $(call plot,svg background "white")
%.png: %.svg; inkscape -d 300 -o $@ $<
plot = cat <(echo "$$plotscript") $< | gnuplot -e 'set term $1; set title "$(username)"' - > $@
username = $(shell json -a account.username < account.json)
export define plotscript =
set grid
set xdata time
set timefmt "%Y-%m-%d"
set format x "%Y-%m"
set xtics rotate by 60 right
set style fill solid
set boxwidth 1
plot "-" using 1:2 with boxes title ""
endef
mkjson = unzip -qc $< $1 | sed 1d | cat <(echo [{) - > $@
aggregate = awk '{r[$$0] += 1} END {for (k in r) print k, r[k]}'
sort = sort -k2 -n | column -t
SHELL := bash -o pipefail
.DELETE_ON_ERROR:
.INTERMEDIATE: account.json tweets.json dates.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment