Utente:DonPaolo/LastContrib.sh
(Reindirizzamento da Utente:DonPaolo/DataUltimoContributo.sh)
#!/bin/sh PATH="/bin:/usr/bin" # get the program name PROGRAMNAME=`echo $0| tr \/ \\\n |tail -n 1` VERSION=1.2.6 LASTEDITDATE="7/10/2006" USAGE="$PROGRAMNAME v. $VERSION ($LASTEDITDATE) Copyright Paolo Benvenuto (donpaolo@gsi.it) Credits to Ivan Vighetto (Sbisolo), HelLViS69, Marco D'Itri License GPL A program that retrieves from it.wikipedia.org and writes to standard output or sends to an e-mail address the last contribution date of one or more it.wiki users. * Invoked without args, it asks for one or more it.wiki user(s) and returns his/her/their last contribution date * Invoked with one or more users, it returns the corresponding dates of last contribution Usage: $PROGRAMNAME { -f file } { -m e-mail-address } { -a | -d | -u } { -r } { -h } { it.wiki-user }.. Switches: -a, --sort-alfa sort results alfabetically -d, --sort-date sort results according to date -f <file>, --file <file> returns last contribution dates of the users specified in <file> -h, --help help -m <address>, --mail <address> mail results to <address> instead of sending it to standard output --sort-no-reverse sort results normal (no reverse) order -r, --sort-reverse sort results reverse order -u, --unsorted do not sort results (default) You can mix one or more user names with one or more files, in any order. User names' first letter is case insensitive (as in wikipedia), and is presented upper case. Removes leading and trailing spaces from usernames; removes duplicates user names and void lines." # TODO: permit selecting another wikipedia: needs changing the way months are converted to numbers # The file where the user names are temporarily stored USERLISTFILE="/tmp/LastContrib.list" if [ -e "$USERLISTFILE" ] ; then rm "$USERLISTFILE" fi touch "$USERLISTFILE" # The file where the temp results are stored RESULTFILE="/tmp/LastContrib.result" if [ -e "$RESULTFILE" ] ; then rm "$RESULTFILE" fi touch "$RESULTFILE" SORT=unsorted SORTMODE=normal while [ $# -gt 0 ] ; do case "$1" in -a | --sort-alfa ) SORT=alfa shift ;; -d | --sort-date ) SORT=date shift ;; -f | --file ) # next parameter is the file with the USERNAME, one for line shift if [ $# -eq 0 ] ; then echo "Missing file after -f switch, ignoring -f" elif [ ! -e $1 ] ; then echo "Unexistent file" $1 ": ignoring it" else cat $1 >> "$USERLISTFILE" fi shift ;; -h | --help ) echo "$USAGE" exit 0 ;; -m | --mail ) shift if [ $# -eq 0 ] ; then echo "Missing parameter after -m switch, ignoring -m" else MAIL=$1 fi shift ;; --sort-no-reverse ) SORTMODE=normal shift ;; -r | --sort-reverse ) SORTMODE=reverse shift ;; -u | --unsorted ) SORT=unsorted shift ;; -*) echo >&2 "=========> Unrecognized option $1" echo echo "$USAGE" exit 1 ;; *) # One or more USERNAME echo $1 >> "$USERLISTFILE" shift ;; esac done # The script is invoked with no command-line args: ask for one or more it.wiki user if [ ! -s "$USERLISTFILE" ] ; then echo "This program retrieves the date of the last contribution to it.wiki of its users." echo "Enter one or more it.wiki users." echo "You can separate them with <RETURN>. Hit ctl-d to end." echo -n "User(s): " cat < /dev/stdin >> "$USERLISTFILE" fi # removing initial and final spaces and void lines cp "$USERLISTFILE" "$USERLISTFILE".tmp cat "$USERLISTFILE".tmp | sed 's/^ *//' | sed 's/ *$//' | sed '/^$/ d' > "$USERLISTFILE" # removing cp "$USERLISTFILE" "$USERLISTFILE".tmp cat "$USERLISTFILE".tmp > "$USERLISTFILE" # changing internal spaces to undescores while ( grep \ "$USERLISTFILE" > /dev/null ) ; do cp "$USERLISTFILE" "$USERLISTFILE".tmp cat "$USERLISTFILE".tmp | sed 's/ /_/' > "$USERLISTFILE" done # 1st letter case upper case (in wikipedia it's case insensitive, and is represented upper case) - perl code kindly contributed by Marco D'Itri cp "$USERLISTFILE" "$USERLISTFILE".tmp cat "$USERLISTFILE".tmp | perl -npe 's/^(.)/uc($1)/e' > "$USERLISTFILE" # remove duplicate users preserving order cp "$USERLISTFILE" "$USERLISTFILE".tmp nl "$USERLISTFILE".tmp | sort -k2 | uniq -f1| sort | cut -f2 > "$USERLISTFILE" # Calculates the number of users NLINES=`wc -l "$USERLISTFILE" |cut -d " " -f1` echo -n "Retrieving last contribution date for $NLINES it.wiki user name" if [ "$NLINES" = 1 ] ; then echo ": " else echo "s: " fi # loop on the usernames for I in `seq 1 "$NLINES"`; do # print the advancing indicator to screen if [ "$I" -lt 10 ] ; then echo -n " " fi echo -n "$I " # get the i-th USERNAME USERNAME=`head -n "$I" "$USERLISTFILE" | tail -n 1` # get the corresponding last contribution date in italian DATATMP=`wget -O - "http://it.wikipedia.org/w/index.php?title=Speciale:Contributions&limit=1&target=$USERNAME" 2> /dev/null |grep \<li\> | head -1 | sed 's/<li>//' | sed 's/ (.*//'` # DATATMP format is for it.wiki "22:16, 29 lug 2006" HOUR=`echo "$DATATMP" | cut -d',' -f1` DAY=`echo "$DATATMP" | cut -d' ' -f2` MONTHTMP=`echo "$DATATMP" | cut -d' ' -f3` YEAR=`echo "$DATATMP" | cut -d' ' -f4` UNEXISTENTUSER=no # we need converting months to numbers because date -d doesn't accept argument in a locale different from C case "$MONTHTMP" in gen) MONTH="01" ;; feb) MONTH="02" ;; mar) MONTH="03" ;; apr) MONTH="04" ;; mag) MONTH="05" ;; giu) MONTH="06" ;; lug) MONTH="07" ;; ago) MONTH="08" ;; set) MONTH="09" ;; ott) MONTH="10" ;; nov) MONTH="11" ;; dic) MONTH="12" ;; # if the month isn't recognized, then the server answered "unexistent user" *) UNEXISTENTUSER=yes esac # fill with an initial 0 if day<10 if [ ${#DAY} = 1 ] ; then DAY=0"$DAY" fi DATE="$YEAR$MONTH$DAY $HOUR" # write the date and the user name to the temporaly result file if [ "$UNEXISTENTUSER" = "yes" ] ; then echo "Unexistent user $USERNAME" >> "$RESULTFILE" else echo "`date -d "$DATE" +%F` `date -d "$DATE" +%H:%M:%S` $USERNAME" >> "$RESULTFILE" fi # lacking control on $I multiple of 10 (one space more) if ( echo "$I" | grep 0$ > /dev/null ) ; then echo fi done echo "done!" # sort the results cp "$RESULTFILE" "$RESULTFILE".tmp if [ "$SORT" = "date" -a "$SORTMODE" = "normal" ] ; then sort -f "$RESULTFILE".tmp > "$RESULTFILE" elif [ "$SORT" = "date" -a "$SORTMODE" = "reverse" ] ; then sort -f -r "$RESULTFILE".tmp > "$RESULTFILE" elif [ "$SORT" = "alfa" -a "$SORTMODE" = "normal" ] ; then sort -f -k3 "$RESULTFILE".tmp > "$RESULTFILE" elif [ "$SORT" = "alfa" -a "$SORTMODE" = "reverse" ] ; then sort -f -k3 -r "$RESULTFILE".tmp > "$RESULTFILE" fi # add line numbers cp "$RESULTFILE" "$RESULTFILE".tmp nl "$RESULTFILE".tmp > "$RESULTFILE" # mail the result if requested, otherwise output it to standard output if [ "$MAIL" ] ; then cat "$RESULTFILE" | mail -s "it.wiki Last contribution dates" "$MAIL" && echo "Sent result mail to $MAIL" else cat "$RESULTFILE" fi exit 0