Jump to content

User:Xcbot/source

From Wikipedia, the free encyclopedia
###############################################################
## This rather sad script is released into the public domain ##
## by its author, James Pearson (Xiong Chiamiov)             ##
###############################################################

#! /bin/bash

## get list of images
# $(date +%Y%m%d%H%M%S --date=-7days) is current date minus 7 days
wget http://en.wikipedia.org/w/index.php?title=Special:Imagelist\&offset=$(date +%Y%m%d%H%M%S --date=-7days)\&limit=5\&sort=img_timestamp\&desc=1 -O imagelist -T 15 --waitretry=15

## extract out images from file
# get only the lines that have images in them
sed -n '/TablePager_col_img_name/p' imagelist > imagelist2
# cut the crap before the image address
sed -e 's/.*http/http/g' imagelist2 > imagelist
# and after it
sed -e 's/".*$//g' imagelist > imagelist2
# then remove anything that's not a jpg or png
egrep 'jpg|JPG|png|PNG' imagelist2> imagelist

## put images in an array
Lines=0
exec 3<> imagelist
while read line <&3
do {
  a[$Lines]=$line
  #echo ${a[$Lines]}
  (( Lines++ ));
}
done
exec 3>&-

echo "-----------------------------"
echo "Number of lines read = $Lines"

# this is the minimum difference in file size between old and new to upload new version
mindiff=25600 # 25kB

for i in "${a[@]}"
do {
  echo
  echo $i

  wget $i -P wp_temp -T 15 --waitretry=15 # get the image and put it in temp folder

  oldsize=$(stat -c%s wp_temp/*) # get the size of the image
  declare -i oldsize # make sure it's evaluated as an integer

  if [[ $i =~ .*(jpg|JPG) ]]
  then
    jpegoptim -t wp_temp/*
    
    filesize=$(stat -c%s wp_temp/*) # get the size of the image
    declare -i filesize # make sure it's evaluated as an integer
    if (( ("$oldsize" - "$filesize") > "$mindiff" ))
    then
      python upload.py -log -noverify -keep -putthrottle:15 wp_temp/* "losslessly optimized using [[jpegoptim]]"
    else
      echo
      echo "not optimized"
    fi

  elif [[ $i =~ .*(png|PNG) ]]
  then
    optipng wp_temp/*

    filesize=$(stat -c%s wp_temp/*) # get the size of the image
    declare -i filesize # make sure it's evaluated as an integer
    if (( ("$oldsize" - "$filesize") > "$mindiff" ))
    then
      python upload.py -log -noverify -keep -putthrottle:15 wp_temp/* "losslessly optimized using [[optipng]]"
    else
      echo
      echo "not optimized"
    fi
  fi

  rm wp_temp/* # remove the image we were working on
  echo
}
done

## clean up
rm imagelist*

exit 0