Όνομα εργαλείου: getnewpages.sh


Περιγραφή:

Αυτό το εργαλείο καταγράφει τους τίτλους σελίδων που δημιουργήθηκαν μέσα σε ένα ορισμένο χρονικό διάστημα. Το αρχείο τίτλων αποθηκεύεται στο "tmp_np/titles.<ημερομηνία>.txt", με αντίγραφο στο "titles_np.txt". Μαζί με τους τίτλους καταγράφονται και η ημερομηνία και η ώρα δημιουργίας της σελίδας καθώς και οι πρώτες σειρές του περιεχομένου της. Για να πάρετε τους τίτλους που επιθυμείτε (π.χ. όλους τους τίτλους των καινούργιων γαλλικών λημμάτων), θα πρέπει να τρέξετε άλλη εντολή πάνω στο αρχείο: πχ.

cat titles_np.txt | grep '{{-fr-}}' | awk -F" 2010" '{ print $1 }' > γαλλικά_λήμματα.txt

Αν ξανατρέξετε το ίδιο εργαλείο την ίδια μέρα, θα διαγραφεί το αρχείο τίτλων και θα δημιουργηθεί καινούριο. Αρχεία που δημιουργήθηκαν άλλες μέρες δεν θα διαγραφούν.

Παράμετροι:

  • την τελευταία μέρα από την οποία θέλετε σελίδες
  • την πρώτη μέρα από την οποία θέλετε σελίδες
Παραδείγματα: today, today-3d (τρεις μέρες πριν από σήμερα), today-1h (μία ώρα πριν από τώρα), 2008-02-06T08:54:06Z (6 Φεβρουαρίου 2008 στις 8:54, ώρα UTC)
Ειδική περίπτωση: η λέξη "lastrun" αναφέρεται στην τελευταία φορά που τρέξατε την εντολή. Πχ. αν δώσετε τις παραμέτρους today lastrun, το εργαλείο θα βρει τις σελίδες που δημιουργήθηκαν από την προηγούμενη φορά που τρέξατε το εργαλείο μέχρι σήμερα.

Προϋποθέσεις:

  • περιβάλλον unix/linux ή περιβάλλον με τις εντολές date, sed, awk, cat, grep
  • η εντολή curl


→ Πίσω στα Εργαλεία

getnewpages.sh επεξεργασία

#!/bin/bash

usage() {
  echo "Usage: $0 startdate  endate"
  echo "where startdate is latest date from which to get new pages"
  echo "and enddate is the earliest date, in the local timezone."
  echo "The base date may be specified as either today, or lastrun,"
  echo "where lastrun is the lastest date you got new pages from"
  echo "during the previous run."
  echo 
  echo "For example:"
  echo "$0 today today-3d"
  echo "$0 today-1h today-5h"
  echo "$0 today lastrun"
  echo "If you omit the d or h the increment is interpreted as days"
  echo
  echo "Alternatively you an specify absolute timestamps."
  echo "These must be in the format yyyy-mm-ddThh:mm:ssZ"
  echo "For example:"
  echo "$0 2008-02-06T08:54:06Z  2008-01-23T08:00:00Z"
  echo "In this case the times are interpreted as UTC times."
  echo
  exit 1
}

if [ -z "$1"  ] || [ -z "$2" ]; then
  usage
fi

usage_lastrun() {
    echo "In order to use lastrun+-(d|h), you need to have the timestamp of the last run"
    echo "stored in the file last_run_newpages in the current directory.  To get the appropriate"
    echo "timestamp, run"
    echo 'date +%s -d "yyyy-mm-dd hh:mm:ss +0000" > last_run_newpages' 
    echo "Then run this script again."
    exit 1
}

checkformat() {
    local d

    d="$1"

    if [ -z "$d" ]; then
	secs=`date +%s`
        return $secs
    fi	

    hasZ=`echo $1 | grep Z`
    if [ ! -z "$hasZ" ]; then
       # μορφή ως: 2008-01-23T08:00:00Z
       # μετατροπή σε: 2008-01-23 08:00:00 +0000
       reformatted=`echo $1 | sed -e 's/T/ /; s/Z/ +0000/;'`
       secs=`date --date="$reformatted" +%s`
       return $secs
    fi

    minus=`echo "$d" | grep -e '-'`
    plus=`echo "$d" | grep -e '+'`
    if [ ! -z "$minus" ]; then 
	op="-"
    elif [ ! -z "$plus" ]; then
	op="+"
    else
	op=""
    fi
    if [ -z "$op" ]; then
	basedate=$d
	incr=0
	incrtype="d"
    else
        basedate=`echo $d | awk -F"$op" '{ print $1 }'`
        incr=`echo $d | awk -F"$op" '{ print $2 }'`
	incrtype="d"
    fi
    if [ ! -z "$incr" ]; then
	day=`echo "$incr" | grep 'd'`
	hour=`echo "$incr" | grep 'h'`
	if [ ! -z "$day" ]; then 
	    incrtype="d"
	elif [ ! -z "$hour" ]; then
	    incrtype="h"
	fi
	incr=`echo $incr | sed -e "s/$incrtype//"`
	if [ -z "$incr" ]; then
	    incr='0'
	fi
    fi
    case $basedate in
	'today')
            today=`date -u +"%Y-%m-%d %H:%M:%S +0000"`
            secs=`date +%s -d "$today"`
	    ;;
	'lastrun')
	    if [ ! -e "$lastrun" ]; then
		usage_lastrun
		exit 1
	    fi
	    lastdaterun=`cat "$lastrun`
	    testdate=`date -d @"$lastdaterun"`
	    if [ $? -ne 0 ]; then
		usage_lastrun
	    fi
            secs=`date +%s -d @"$lastdaterun"`
	    ;;
	*)
	    usage
	    ;;
    esac
    case $incrtype in
	'd')
	    incr=$(( $incr*86400 ))
	;;
	'h')
	    incr=$(( $incr*3600 ))
	;;
	*)
	;;
    esac
    case $op in 
	'-')
	    secs=$(( $secs-$incr ))
	    ;;
	'+')
	    secs=$(( $secs+$incr ))
	    ;;
	'')
	    ;;
	*)
	    usage
    esac
    return 0
}

tmp="./tmp_np"
lastrun="last_run_np"
checkformat "$1"
startdatesecs=$secs
checkformat "$2"
enddatesecs=$secs

ext=`date +%m-%d-%Y -d @$startdatesecs`

globstartdate=`date -u -d @$startdatesecs +"%Y-%m-%dT%H:%M:%SZ"`
globenddate=`date -u -d @$enddatesecs +"%Y-%m-%dT%H:%M:%SZ"`

lastdaterun="$startdatesecs"
me=`basename $0`


mkdir -p $tmp
changes="$tmp/changes.$ext"
pages="$tmp/pages.$ext"
titles="$tmp/titles.$ext"

rm -f  $titles.* $pages.* $changes.* 

# πρόσφατες αλλαγές
npstartdate=$globstartdate
npenddate=$globenddate

while [ 1 ]; do

  echo getting new pages $npstartdate to $npenddate
  # παίρνουμε τις επόμενες πληροφορίες από το αρχείο καταγραφών δημιουργίας σελίδων
  curl --retry 10 -f "http://el.wiktionary.org/w/api.php?action=query&list=recentchanges&rclimit=500&rctype=new&format=xml&rcstart=$npstartdate&rcend=$npenddate&rcnamespace=0&rcprop=timestamp|title|comment"  >  $changes.raw

  if [ $? -ne 0 ]; then
      echo "Error $? from curl, unable to get new pages, bailing"
      exit 1
  fi
  if [ -e "$changes.cmp" ]; then
      aredone=`cmp $changes.raw $changes.cmp`
      if [ -z "$aredone" ]; then
          break;
      fi
  fi
  cp $changes.raw $changes.cmp
  cat $changes.raw >> $changes.raw.save

  # παίρνουμε τους τίτλους
  # προσωρινή λύση για σφάλμα στον κώδικα του MediaWiki, μπορεί να κοπεί ένας 
  # χαρακτήρας στη μέση (στο τέλος της σύνοψης) που δημιουργεί δυσκολίες
  # για εφαρμογές (gedit, emacs) που διαβάζουν την έξοδο μετά..
  # γι αυτό βάζουμε sed xCE
  cat  $changes.raw | sed -e 's/>/>\n/g;' | grep '<rc type=' | awk -F\" '{ print $6 " " $8 " " $10 }' | sed -e "s/\xCE$//g;"  >> $titles.txt

  # παίρνουμε τη χρονοσφραγίδα από την τελευταία γραμμή
  nextstartdate=`cat $changes.raw |   sed -e 's/>/>\n/g;' | grep '<rc type=' | awk -F\" '{ print $8 }' | tail -n 1`
  # αν είναι κενό... τελειώσαμε
  if [[ -z "$nextstartdate" ]]; then
    break
  fi

  npstartdate="$nextstartdate"
  sleep 6
done

cp $titles.txt titles_np.txt

echo "$lastdaterun" > $lastrun

# done!
echo "titles are now in $titles.txt and titles_np.txt"
echo "done!"
exit 0