Skip to content

Instantly share code, notes, and snippets.

@coybit
Forked from omarowns/magpi.sh
Last active March 17, 2022 06:28
Show Gist options
  • Select an option

  • Save coybit/f414b7e76aa5ebf620d3 to your computer and use it in GitHub Desktop.

Select an option

Save coybit/f414b7e76aa5ebf620d3 to your computer and use it in GitHub Desktop.
Bash script to download all issues of The MagPi.
#!/bin/bash
#
# Creates a directory named "magpi" on your home folder and all issues are downloaded there.
# It also checks if you already have some of the issues and skips them from downloading.
# Use argument: --log OFF to disable logging. It is there because I have this script on my crontab and I needed to monitor if it running properly. For enabling logging do:
# $ sudo touch /var/log/magpi.log
# $ sudo chmod 666 /var/log/magpi.log
# That's it.
#
# Command execution is:
# magpi.sh --log [ON|OFF]
#
#
#
## GLOBALS
NUMBER=1
MAGPIDIR=`pwd`"/magpi"
LOG="/var/log/magpi.log"
FLOG=$2
URL=""
## FUNCTIONS
function log {
echo "[`date`] $1" >> $LOG
}
function fetch {
if [ ! -f "The-MagPi-issue-$1-en.pdf" ]; then
if [ "$FLOG" == "ON" ]; then
log "Downloading The MagPi Issue $1"
fi
URL="https://www.raspberrypi.org/magpi-issues/MagPi$(printf "%02d" $1).pdf"
echo "Downloading The MagPi Issue $URL"
curl -o "The-MagPi-issue-$1-en.pdf" $URL
else
if [ "$FLOG" == "ON" ]; then
log "Already has MagPi issue $1"
fi
fi
#wget --tries=5 --timeout=20 --wait=1 --waitretry=1
}
## MAIN
if [ ! "$1" == "--log" ]; then
echo "Command syntax is: $0 --log [ON|OFF]"
exit 1
fi
if [ ! $2 ]; then
echo "Command syntax is: $0 --log [ON|OFF]"
exit 1
fi
## Check if the directory for storing exists, if not it creates it
if [ ! -d "$MAGPIDIR" ]; then
mkdir $MAGPIDIR
fi
## Change to that directory
cd $MAGPIDIR
## Calculate number of issues so far (aprox)
ISSUE=0
declare -i MONTH=5
declare -i YEAR=2012
declare -i CMONTH=`date +%m`
declare -i CYEAR=`date +%Y`
while (( $YEAR <= $CYEAR )); do
if [ $MONTH -eq 12 ]; then
let MONTH=0
let YEAR=YEAR+1
fi
if (( $YEAR == $CYEAR )) && (( $MONTH == $CMONTH )); then
let ISSUE=ISSUE+1
break
fi
let MONTH=MONTH+1
let ISSUE=ISSUE+1
done
echo "Downlaod to " + $MAGPIDIR
COUNTER=1
until [ $COUNTER -ge $ISSUE ]; do
fetch $COUNTER
let COUNTER=COUNTER+1
done
@etieg
Copy link

etieg commented Mar 17, 2022

Thanks for the script., but all the issues being downloaded has a 0kb value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment