User:Rick Bot/scripts/newnoms

From Wikipedia, the free encyclopedia
#!/bin/bash

# find newly promoted FAs and propose a nominator

# figure out this month and last month
YEAR=`date +%Y`
MONTH=`date +%B`

function prevmonth () {
  case $1 in
    January) echo "December";;
    February) echo "January";;
    March) echo "February";;
    April) echo "March";;
    May) echo "April";;
    June) echo "May";;
    July) echo "June";;
    August) echo "July";;
    September) echo "August";;
    October) echo "September";;
    November) echo "October";;
    December) echo "November";;
  esac
}

function check_month () {
  # $1 is MONTH, $2 is YEAR
  M=$1
  Y=$2
  echo "checking month - $M $Y"

  # get the noms for this month
  /usr/bin/curl "http://en.wikipedia.org/w/index.php?title=Wikipedia:Featured_article_candidates/Featured_log/${M}_${Y}&action=raw" >FAnoms.$Y.$M

  # get the current year list by nominator
  if [ ! -s FAnoms.$Y -a -s FAnoms.$Y.test ]; then
    cp FAnoms.$Y.test FAnoms.$Y 
  fi

  if [ ! -s FAnoms.$Y ]; then
    /usr/bin/curl "http://en.wikipedia.org/w/index.php?title=Wikipedia:Featured_articles_promoted_in_${Y}&action=raw" >FAnoms.$Y
    if [ ! -s FAnoms.$Y ]; then
      echo "*--- No Wikipedia:Featured_articles_promoted_in_${Y}?"
      exit
    fi
  fi

  echo "Wikipedia:Featured_article_candidates/Featured_log/${M}_${Y}"
  rm -f FAadd.$M
  MATCHED_ONE="no"
  # figure out nominator for each article we don't know about yet
  echo | cat FAnoms.$Y.$M - | while read FANOM; do
    FAFILE=`echo "$FANOM" | sed -e "s/^{{Wikipedia:Featured.article.candidates.//" -e "s/}}$//"`
    FA=${FAFILE%/archiv*}
    if [ "$FANOM" = "$FA" ]; then
      continue
    fi
    grep "^[|][|] *[[][[]$FA\]]" FAnoms.$Y >/dev/null
    if [ $? -eq 0 ]; then
      echo "Already know about $FA"
      MATCHED_ONE="yes"
      break
    fi
    NOM=`getnom "$FAFILE"`
    echo -n "Newnom: $FA --> $NOM ? "
    read ANS <&2
    if [ "$ANS" != "" ]; then
      NOM=$ANS
    fi
    echo "addnom||$FA||$NOM" >> FAadd.$M
  done

  if [ -s FAadd.$M ]; then
    addnoms $M $Y
  fi
}

function getnom () {
  # $1 is article
  echo getnom $1 >&2
  # f=`echo "$1" | cut -n -d '|' -f1 | tr " " "_"`
  f=`echo "$1" | cut -d '|' -f1 | tr " " "_"`
  echo "f=$f" >&2
  /usr/bin/curl "http://en.wikipedia.org/w/index.php?title=Wikipedia:Featured_article_candidates/$f&action=raw" | awk -v YEAR=$YEAR -v MONTH=$MONTH '
{
  gsub(/[=][=][=][=]/,"",$0)
}
$0 ~ "^===" {
  sub(/^[=][=][=]/,"",$0)
  article=$0
  sub(/[=][=][=].*/,"",article)
}

/[[][[][uU][sS][eE][rR]:/ {
  if (article != "") {
    noms=1
    user[noms] = $0
    while ( user[noms] ~ /[[][[][uU][sS][eE][rR]:/ && user[noms] !~ /^[[][[][uU][sS][eE][rR]:/ ) {
      sub(/^.[^[]*/,"",user[noms])
      if (user[noms] ~ /^[[][[][uU][sS][eE][rR]:/ ) {
        sub(/^[^[]*[[][[][uU][sS][eE][rR]:/,"",user[noms])
        user[noms+1] = user[noms]
        sub(/].*/,"",user[noms])
        sub(/[|].*/,"",user[noms])
        users[user[noms]] = users[user[noms]] + 1
        noms=noms+1
      }
    }
    # [[August]] [[2005]] ||  || [[Gray Wolf]] || [[user:Sango123|Sango123]]
    printf ("%s", "[[user:" user[1] "|" user[1] "]]")
    for (i=2; i<noms; i++) {
       printf(" & %s",  "[[user:" user[i] "|" user[i] "]]")
    }
    printf("\n")
    article = ""
  }
}' 

}

function addnoms() {
  # $1 is MONTH
  # $2 is YEAR
  echo addnoms $1 $2 >&2
  cat FAadd.$1 FAnoms.$2 | awk -v YEAR=$2 -v MONTH=$1 '
BEGIN {
  FS="\\|\\|"
  n=0
}

/^addnom/ {
  n++
  art[n] = $2
  nom[n] = $3
  next
}

/^== Promoted in |[[][[]Category:/ && (n>0) {
  if ($0 ~ MONTH " " YEAR) {
    print $0
    while ( $0 !~ /!Article!!Main page date/ ) {
      getline
      print $0
    }
    for (i=1; i <= n; i++) {
      print "|-"
      print "|| [[" art[i] "]] ||  || " nom[i]
    }
  } else {
    print "== Promoted in " MONTH " " YEAR " =="
    print ":'"''"'See logs at [[Wikipedia:Featured article candidates/Featured log/" MONTH " " YEAR "]]'"''"'"
    print "{| class=\"wikitable\" style=\"margin:auto;\" width=\"90%\""
    print "|-"
    print "!Article!!Main page date!!Nominator"
    for (i=1; i <= n; i++) {
      print "|-"
      print "|| [[" art[i] "]] ||  || " nom[i]
    }
    print "|}"
    print ""
    print $0
  }
  n=0
  next
}

{
  print $0
}' >FAnoms.$2.new

}

LASTMONTH=`prevmonth $MONTH`
LASTYEAR=$YEAR
if [ $LASTMONTH = "December" ]; then
  let LASTYEAR=$LASTYEAR-1
fi

rm -f FAnoms.$YEAR
rm -f FAnoms.$LASTYEAR
rm -f FAnoms.$YEAR.new
rm -f FAnoms.$LASTYEAR.new

check_month $LASTMONTH $LASTYEAR
if [ -s FAnoms.$LASTYEAR.new ]; then
  mv FAnoms.$LASTYEAR.new FAnoms.$LASTYEAR
fi

check_month $MONTH $YEAR
if [ -s FAnoms.$YEAR.new ]; then
  mv FAnoms.$YEAR.new FAnoms.$YEAR
fi