Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 90 lines (74 sloc) 1.83 KB
#!/bin/bash
set -euo pipefail
here=$(dirname "${BASH_SOURCE[0]}")
function usage {
echo "$0: interact with Wikipedia"
echo " age <actor> [as-of-date, eg 1985-07-20]"
echo " ages best_actor"
echo " ages best_actress"
echo " bday <actor>"
echo " best_actor"
echo " best_actress"
echo " fetch best_actor"
echo " fetch best_actress"
echo " infobox <actor>"
exit 1
}
function parse_table {
pup | gawk '
/<tr>/ {
found_actor=0
}
match($0, /([0-9]+)_in_film/, result) {
year=result[1]
mode="won"
}
!found_actor && !/_in_film/ && !/_Academy_Awards/ && match($0, /href="\/wiki\/([^"]+)"/, result) {
print year, result[1], mode
mode="nominated"
found_actor=1
}
'
}
function best_actor {
bin/fetch Academy_Award_for_Best_Actor '.sortable:not(.plainrowheaders)' | parse_table
}
function best_actress {
bin/fetch Academy_Award_for_Best_Actress '.sortable:not(.plainrowheaders)' | parse_table
}
function infobox {
"$here"/bin/fetch "$1" .infobox
}
function fetch {
"$@" | while read -r year slug result; do
echo -n "$slug..."
infobox "$slug" | wc -c
done
}
function bday {
# ~1% of articles don't have infoboxes, so we hardcode some dates
# for them.
case "$1" in
"Cary_Grant") echo "1904-01-18" ;;
"Grace_Moore") echo "1898-12-05" ;;
"Kenneth_Branagh") echo "1960-12-10" ;;
"Laurence_Olivier") echo "1907-05-22" ;;
"Lawrence_Tibbett") echo "1896-11-16" ;;
"Rod_Steiger") echo "1925-04-14" ;;
*) infobox "$1" | pup '.bday text{}'
esac
}
function age {
from=$(bday "$1")
at=${2:-$(date +%Y-%m-%d)}
echo $((($(date +%s --date "$at")-$(date +%s --date "$from")) / 86400 / 365))
}
function ages {
"$@" | while read -r year slug result; do
echo "$year $slug $result $(age "$slug" "$year-01-01")"
done
}
if [ $# == "0" ]; then
usage
fi
"$@"