Χρήστης:ArielGlenn/λήψη καινούργιων τίτλων

Από Βικιλεξικό
Μετάβαση στην πλοήγηση Πήδηση στην αναζήτηση

Εγκατάσταση:

  • Προυποθέσεις: λίνουξ, bash, curl
  • Βάλε το script σε κενό κατάλογο με το όνομα που θέλεις, ας πούμε getnewpages.sh
  • chmod 700 getnewpages.sh

Για να τρέξεις το script:

  • ./getnewpages.sh
θα σου δώσει οδηγίες
  • ./getnewpages.sh today today-1
θα σου κατεβάσει τους τίτλους των καινούργιων σελίδων της τελευταίας μέρας

Αφού τελειώσει το script, θα βρεις στον υποκατάλογο tmp το αρχείο titles.xxx.txt με τους τίτλους, χρονοσφραγίδες και συνόψεις μέσα.

Περισσότερες πληροφορίες αν θέλεις να μετατρέψεις το script θα τις βρεις εδώ: http://el.wiktionary.org/w/api.php

#!/bin/bash

usage() {
  echo "Usage: $0 startdate  endate"
  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
  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 $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
  # παίρνουμε τις επόμενες γραμμές από την καταγραφή πρόσφατων αλλαγών
  curl --retry 10 -f "http://el.wiktionary.org/w/api.php?action=query&list=recentchanges&rclimit=500&rctype=new&format=xml&rcstart=$rcstartdate&rcend=$rcenddate&rcnamespace=0&rcprop=timestamp|title|comment"  >  $changes.raw

  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

  # παίρνουμε τους τίτλους
  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`
  # αν είναι κενό... τελειώσαμε
  if [[ -z "$nextstartdate" ]]; then
    break
  fi

  rcstartdate="$nextstartdate"
  sleep 6
done

echo "$lastdaterun" > last_run

# done!
echo "done!"
exit 0