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


Περιγραφή:

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

cat titles.02-21-2010.txt | grep '{{-grc-}}' | 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


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

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

#!/bin/bash

usage() {
  echo "Usage: $0 startdate  endate user"
  echo "where startdate is latest date from which to get changes"
  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 changes 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
  echo "The last argument, user, is the username of the user for which"
  echo "you want to download changes.  If you specify no user, all changes will"
  echo "be downloaded."
  echo "For example:"
  echo "$0 today today-3d Flubot"

  exit 1
}

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

if [ ! -z "$3" ]; then 
  user="$3"
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 $lastrun in the current directory.  To get the appropriate"
    echo "timestamp, run"
    echo 'date +%s -d "yyyy-mm-dd hh:mm:ss +0000" > $lastrun' 
    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 'last_run' ]; then
		usage_lastrun
		exit 1
	    fi
	    lastdaterun=`cat last_run`
	    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"
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.* 

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

while [ 1 ]; do

  echo getting recent changes $rcstartdate to $rcenddate
  # παίρνουμε τις επόμενες γραμμές από την καταγραφή πρόσφατων αλλαγών
  if [ ! -z "$user" ]; then
      curl --retry 10 -f "http://el.wiktionary.org/w/api.php?action=query&list=usercontribs&uclimit=500&&format=xml&ucstart=$rcstartdate&ucend=$rcenddate&ucnamespace=0&ucprop=timestamp|title|comment&ucuser=$user"  >  $changes.raw
  else
      curl --retry 10 -f "http://el.wiktionary.org/w/api.php?action=query&list=recentchanges&rclimit=500&rctype=new|edit&format=xml&rcstart=$rcstartdate&rcend=$rcenddate&rcnamespace=0&rcprop=timestamp|title|comment"  >  $changes.raw
  fi

  if [ $? -ne 0 ]; then
      echo "Error $? from curl, unable to get recent changes, 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

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

  # αν είναι κενό... τελειώσαμε
  if [[ -z "$nextstartdate" ]]; then
    break
  fi

  rcstartdate="$nextstartdate"
  sleep 6
done

cp $titles.txt "titles.$ext.txt"

echo "$lastdaterun" > last_run

# done!
echo "Titles of changed/new articles are now in titles.$ext.  Done!"
exit 0