commit 45935c45fba41a83a00f19a020806ce5fbf9197c parent 0df2fd2a1cd06eae947520d243113bcadcef6d63 Author: clamiax <smoppy@gmail.com> Date: Sun, 25 May 2014 13:56:21 +0200 Add the vegask script Diffstat:
A | vegask | | | 95 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 95 insertions(+), 0 deletions(-)
diff --git a/vegask b/vegask @@ -0,0 +1,95 @@ +#!/bin/bash +# Get vegan people from Wikipedia. Written for fun. + +SOURCE="http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&titles=List_of_vegans" +GETTER="wget -t1 -qO -" +DB="/tmp/vegask.db" + +update() { + items=$($GETTER "$SOURCE" | grep -A2 '!scope=' |tr -d '[]\n' |sed 's/--/\n/g' |cut -d' ' -f3- |sed -e 's/{{\(sortname|\)\(.*\)}}/\2/g' -e 's/&/\&/g' -e 's/"/"/g') + + # clear the db + echo -n > "$DB" + + IFS=' + ' + for item in $items + do + country=$(echo $item |rev |cut -d'|' -f1 | rev) + occupation=$(echo $item |rev |cut -d'|' -f2 | rev) + fullname=$(echo $item |rev |cut -d'|' -f3- | rev) + + strip=$(echo $fullname |grep -c '|') + if [ $strip -eq 1 ]; then + fullname="$(echo $fullname |awk -F'|' '{print $1 $2}')" + fi + + echo "$fullname|$occupation|$country" >> "$DB" + done + unset IFS +} + +usage() { +echo -n "Usage: $0 [-hu1r] [-n <num>] [<pattern>] ... +-u Update the people's database +-n <num> Limit the output to <num> items +-1 Pick only one item (alias for -n1) +-r Randomize +-h Show this helps +" + +exit 1 +} + +show() { + item="$@" + name="$(echo $item |cut -d'|' -f1)" + profession="$(echo $item |cut -d'|' -f2)" + country="$(echo $item |cut -d'|' -f3)" + echo "$name, $profession. ($country)" +} + +pick() { + n=$1 + r=$2 + + if [ $r -ne 0 ]; then + items="$(cat $DB |sort -R |head -$n)" + else + items="$(cat $DB |head -$n)" + fi + + for i in $(seq 1 $n) + do + show $(echo "$items" |tail -$i) + done +} + +main() { + UPDATE=0 + LIMIT=0 + RANDOMIZE=0 + + while getopts "hulr1n:" opt; do + case $opt in + h) usage;; + u) UPDATE=1;; + l) LIMIT="$(wc -l $DB |cut -d' ' -f1)";; + n) LIMIT=$OPTARG;; + 1) LIMIT=1;; + r) RANDOMIZE=1;; + esac + done + + if [ ! -f "$DB" ]; then + update + UPDATE=0 + fi + + [ "$UPDATE" -eq 1 ] && update + [ "$LIMIT" -eq 0 ] && usage && exit + pick $LIMIT $RANDOMIZE +} + + +main "$@"